859. Buddy Strings (wrong 4 times so many cases to test and consider) if else**

Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B.

Example 1:

Input: A = "ab", B = "ba"
Output: true

Example 2:

Input: A = "ab", B = "ab"
Output: false

Example 3:

Input: A = "aa", B = "aa"
Output: true

Example 4:

Input: A = "aaaaaaabc", B = "aaaaaaacb"
Output: true

Example 5:

Input: A = "", B = "aa"
Output: false

wrong case: aa aa, ab cd, abc dew,

What a shame!

class Solution {
    public boolean buddyStrings(String A, String B) {
        int a1[] =new int[2];
        int a2[] =new int[2];
        if(A.length()!=B.length() || A.length()==0 || B.length()==0) return false;
        int count = 0;
        for(int i = 0; i<A.length(); i++){
            if(A.charAt(i) != B.charAt(i)) {
                count++;
                if(count > 2) return false;
                a1[count-1] = A.charAt(i);
                a2[count-1] = B.charAt(i);
            }
        }
        if(count == 2 &&a1[0]==a2[1]&&a1[1]==a2[0]) return true;
        //case for aa (A==B and duplicate elements in the String)
        int index[] = new int[26];
        if(A.equals(B)){
            for(int i = 0; i<A.length(); i++){
                index[A.charAt(i)-‘a‘]++;
                if(index[A.charAt(i)-‘a‘]>=2) return true;
            }
            return false;
        }else return false;

    }
}

So many if else case to care about! Traps

原文地址:https://www.cnblogs.com/stiles/p/Leetcode859.html

时间: 2024-10-21 07:36:28

859. Buddy Strings (wrong 4 times so many cases to test and consider) if else**的相关文章

【Leetcode_easy】859. Buddy Strings

problem 859. Buddy Strings 参考 1. Leetcode_easy_859. Buddy Strings; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11215029.html

leetcode 859. 亲密字符串(Buddy Strings)

目录 题目描述: 示例 1: 示例 2: 示例 3: 示例 4: 示例 5: 解法: 题目描述: 给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回 false . 示例 1: 输入: A = "ab", B = "ba" 输出: true 示例 2: 输入: A = "ab", B = "ab" 输出: false 示例 3: 输入: A

[Swift]LeetCode859. 亲密字符串 | Buddy Strings

Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B. Example 1: Input: A = "ab", B = "ba" Output: true Example 2: Input: A = "ab", B = "ab&q

LeetCode.859-伙伴字符串(Buddy Strings)

这是悦乐书的第330次更新,第354篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第200题(顺位题号是859).给定两个字母A和B的小写字母,当且仅当我们可以在A中交换两个字母以使结果等于B时返回true.例如: 输入:A ="ab",B ="ba" 输出:true 输入:A ="ab",B ="ab" 输出:false 输入:A ="aa",B ="aa"

arts-week6

Algorithm 830. Positions of Large Groups - LeetCode 75. Sort Colors - LeetCode 859. Buddy Strings - LeetCode 791. Custom Sort String - LeetCode 804. Unique Morse Code Words - LeetCode 877. Stone Game - LeetCode 890. Find and Replace Pattern - LeetCod

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【Go】strings.Replace 与 bytes.Replace 调优

原文链接:https://blog.thinkeridea.com/201902/go/replcae_you_hua.html 标准库中函数大多数情况下更通用,性能并非最好的,还是不能过于迷信标准库,最近又有了新发现,strings.Replace 这个函数自身的效率已经很好了,但是在特定情况下效率并不是最好的,分享一下我如何优化的吧. 我的服务中有部分代码使用 strings.Replace 把一个固定的字符串删除或者替换成另一个字符串,它们有几个特点: 旧的字符串大于或等于新字符串 (le

43. Multiply Strings

1. 问题描述 Given two numbers represented as strings, return multiplication of the numbers as a string.Note:The numbers can be arbitrarily large and are non-negative.Converting the input string to integer is NOT allowed.You should NOT use internal librar

Multiply Strings

package cn.edu.xidian.sselab;/** * title:Multiply Strings * content: * Given two numbers represented as strings, return multiplication of the numbers as a string. * Note: The numbers can be arbitrarily large and are non-negative. * 读已知条件可以,两个String转换