“This is the 25th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

[B] [C] [D]

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:

Input: s = "ab", Goal = "ba" Output: true Explanation: You can swap s[0] = 'a' and S [1] = 'b' to produce "BA ", where S and goal are equal.Copy the code

Example 2:

Explanation: You can only swap s[0] = 'a' and S [1] = 'b' to produce "BA ", where S and Goal are not equal.Copy the code

Example 3:

Input: s = "AA ", Goal =" AA "Output: true Explanation: You can swap s[0] = 'a' and S [1] = 'a' to produce" AA ", where S and Goal are equal.Copy the code

Example 4:

Input: s = "aaaaAAABC ", goal =" aaAAAAACb "Output: trueCopy the code

Tip:

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

The following is the main idea of the passage:

All we need to do is figure out which ones are definitely not intimate strings, and which ones are definitely intimate strings, and then figure out which of these two strings we put in, and return the corresponding value

The details are as follows:

  1. If the two strings are of different lengths, they must not be intimate stringsfalse
  2. If two strings have the same length, the characters at each position in the string are iterated to determine whether the characters at that position are the same
  3. If there’s only one different position, thensYou can’t get to the sum by swapping letters in two placesgoalThe same result is returned at this timefalse
  4. If there are more than two different places, then it countssIt must be by swapping letters in two different positions that the sum of the letters in those two positionsgoalSame, there will still be a different position, then returnfalse
  5. As in Example 3, ifsTwo positional characters are the same ands === goal, then at this timesSwap the positions of two identical characters and still sumgoalSame, returntrue
  6. If I have two different positions, butsAfter swapping letters in two positionss === goal, returntrue

The next step is to use the code to determine which of the above conditions the current input s and goal matches and return the corresponding value

The code is as follows:

Var buddyStrings = function(s, goal) { ==goal.length) return false; Set = new set (), flag = false; set = new set (), flag = false; for(let i = 0; i<s.length; i++){ if(set.has(s[i])) flag = true; else set.add(s[i]) if(s[i]===goal[i]) continue; if(pre===-1) pre = i; else if(next===-1) next = i; // More than two different positions else return false; If (pre===-1) return flag; if(pre===-1) return flag; If (next===-1) return false; Goal return (s[pre] === = goal[next]) && (goal[pre] === s[next])};Copy the code

At this point we are done with leetcode-859- intimate string

If you have any questions or suggestions, please leave a comment!