Skip to content

Latest commit

 

History

History

859

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Given two strings s and goal, return true if you can swap two letters in s so the result is equal to goal, otherwise, return false.

Swapping letters is defined as taking two indices i and j (0-indexed) such that i != j and swapping the characters at s[i] and s[j].

  • For example, swapping at indices 0 and 2 in "abcd" results in "cbad".

 

Example 1:

Input: s = "ab", goal = "ba"
Output: true
Explanation: You can swap s[0] = 'a' and s[1] = 'b' to get "ba", which is equal to goal.

Example 2:

Input: s = "ab", goal = "ab"
Output: false
Explanation: The only letters you can swap are s[0] = 'a' and s[1] = 'b', which results in "ba" != goal.

Example 3:

Input: s = "aa", goal = "aa"
Output: true
Explanation: You can swap s[0] = 'a' and s[1] = 'a' to get "aa", which is equal to goal.

 

Constraints:

  • 1 <= s.length, goal.length <= 2 * 104
  • s and goal consist of lowercase letters.

Related Topics:
Hash Table, String

Similar Questions:

Solution 1.

// OJ: https://leetcode.com/problems/buddy-strings/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
    bool buddyStrings(string A, string B) {
        if (A.size() != B.size()) return false;
        int cnts[26] = {0}, first = -1, second = -1;
        bool hasDup = false;
        for (int i = 0; i < A.size(); ++i) {
            if (++cnts[A[i] - 'a'] == 2) hasDup = true;
            if (A[i] == B[i]) continue;
            if (first == -1) first = i;
            else if (second == -1) second = i;
            else return false;
        }
        return (first != -1 && second != -1 && A[first] == B[second] && A[second] == B[first])
            || (first == -1 && second == -1 && hasDup);
    }
};