Problem
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
and2
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
andgoal
consist of lowercase letters.
Solution
/**
* @param {string} s
* @param {string} goal
* @return {boolean}
*/
var buddyStrings = function(s, goal) {
if (s.length !== goal.length) return false;
if (s === goal) {
const map = {};
return !!Array.from(s).find(char => {
map[char] = (map[char] || 0) + 1;
return map[char] === 2;
});
}
var diff = [];
for (var i = 0; i < s.length; i++) {
if (s[i] !== goal[i]) {
if (diff.length >= 2) return false;
diff.push(i);
}
}
return diff.length === 2 && s[diff[0]] === goal[diff[1]] && s[diff[1]] === goal[diff[0]];
};
Explain:
nope.
Complexity:
- Time complexity : O(n).
- Space complexity : O(1).