Subject to introduce

Given two strings s and goal, return true as long as we can get the same result as goal by swapping two letters in S; Otherwise return false.

Swap letters are defined by taking two subscripts I and j (subscripts starting at 0) that satisfy I! = j, then swap the characters at s[I] and s[j].

  • For example, in the"abcd"Swap subscripts in0And the subscript2The element can be generated"cbad"

Example 1

Enter: s ="ab", goal = "ba"Output:trueExplanation: You can swap s[0] = 'a'And s [1] = 'b'generate"ba"When s and goal are equal.Copy the code

Example 2

Enter: s ="ab", goal = "ab"Output:falseExplanation: You can only swap s[0] = 'a'And s [1] = 'b'generate"ba"S and goal are not equal.Copy the code

Example 3

Enter: s ="aa", goal = "aa"Output:trueExplanation: You can swap s[0] = 'a'And s [1] = 'a'generate"aa"When s and goal are equal. ,Copy the code

Example 4

Enter: s ="aaaaaaabc", goal = "aaaaaaacb"Output:true
Copy the code

Tip:

  • 1 <= s.length, goal.length <= 2 * 10^4
  • s 和 goalConsists of lowercase English letters

Leetcode -859 intimate string B station video

Their thinking

Let’s analyze this problem in five cases

2. Two strings are exactly the same, but no letter is repeated and cannot be swapped, nor are they intimate strings 3. Two strings that have inconsistent letters in two positions and cannot swap letters between two positions are not intimate strings 4. Two strings that exist in more than two positions where letters are inconsistent are not intimate strings 5. Two strings have only two inconsistent letters, and the two letters swap positions to make the two strings equal and close

The problem solving code

var buddyStrings = function(s, goal) {
    // If the string length is not equal, it is not an intimate string
    if(s.length ! == goal.length)return false
    if (s === goal) {
        // Strings of equal length that do not contain at least two identical letters are not intimate strings
        if ([...new Set(s.split(' '))].length < goal.length) return true
        return false
    }
    let i = 0, j = 0
    const s1 = s.split(' '), goal1 = goal.split(' ')
    // Compare letters in the same position and record the first different position
    while (s1[i] === goal1[i]) {
        i++
    }
    j = i + 1
    // Compare letters in the same position and record the second different position
    while (j < s1.length && s1[j] === goal1[j]) {
        j++
    }
    // If the second different letter is not found, it is not the intimate string
    if (j === s1.length) return false
    // If two different letters cannot be exchanged, it is not an intimate string
    if(s1[i] ! == goal1[j] || s1[j] ! == goal1[i])return false
    j = j + 1
    // Continue to compare the remaining letters. If different letter positions occur, it is not an intimate string
    while (j < s1.length && s1[j] === goal1[j]) {
        j++
    }
    return j === s1.length
};
Copy the code

So that’s the answer to this question, Welcome to see my other article [luffy]_ ring list [Luffy]_ happy number [Luffy]_ reverse list [Luffy]_ reverse list [Luffy]_K group of flipped list [Luffy]_ rotation list [Luffy]_ pairwise swap list of nodes [Luffy]_ recent requests [Luffy]_ the KTH number