Likou-Analysis of Intimate String Ideas

Topic analysis

Given two strings A and B composed of lowercase letters, as long as we can get the same result as B by exchanging the two letters in A, return true; otherwise, return false.

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

Problem-solving idea 1 : This is a simple body shape in Likou practice, but when I first saw him, I didn’t have any ideas. What I wanted to do was to constantly judge and screen the answers, but at first I thought there were only the most conventional ones. Judgment is to judge whether the two strings have two letters transposed, I use a loop to traverse, as long as the difference recorded by the end temp is 2 and it is indeed the same after transposition, everything else is not qualified.
Self-defeating: Obviously, the difficulty of this question is not here, but it requires you to judge whether two strings are exactly the same, whether they can be intimate strings, this is more troublesome, because the strings are the same in time. It may also be an intimate string, or it may not.
Problem-solving idea 2 : In fact, the idea of ​​judging is that if the two strings are the same, then as long as the string contains two or more identical letters, then it can definitely be transposed, and the original string is still the same after transposition. It will be an intimate string. But how to implement it is a bit difficult. I initially thought that an array should be used for marking. There are only 26 letters in total. It traverses the string and adds 1 to the corresponding serial number of the array. In the end, as long as there is a letter that accumulates more than 2, it is an intimate character. string. But I look at the question, the maximum string can reach 20000, if it accumulates like this, it is estimated that it will time out. So I sort the strings first, and then subtract the previous letter from the current letter from the beginning. If the value is 0, it means that they are equal, that is, there are two identical letters. You can directly judge this string as an intimate string.

public boolean buddyStrings(String A, String B) {
    
    
        if (A.length() != B.length()) {
    
    
            return false;
        } else if (A.length() < 2 || B.length() < 2) {
    
    
            return false;
        } else {
    
    
            int temp = 0;
            int k = 0;
            boolean flag = true;
            char[] arr = new char[5];
            for (int i = 0; i < A.length(); i++) {
    
    
                if (A.charAt(i) != B.charAt(i)) {
    
    
                    temp++;
                    if (temp > 2) {
    
    
                        flag = false;
                        break;
                    }
                    arr[k++] = A.charAt(i);
                    arr[k++] = B.charAt(i);
                }
            }
            if (temp == 0) {
    
    
                int t = 1;
                char[] brr = A.toCharArray();
                Arrays.sort(brr);
                for (; t < brr.length; t++) {
    
    
                    if (brr[t] - brr[t-1] == 0) {
    
    
                        break;
                    }
                }
                if (t == A.length())
                    flag = false;
            } else if (temp == 2) {
    
    
                if (arr[0] != arr[3] || arr[1] != arr[2])
                    flag = false;
            } else
                flag = false;
            return flag;
        }
    }

Guess you like

Origin blog.csdn.net/baldicoot_/article/details/107698643