class Solution {
public:
bool buddyStrings(string s, string goal) {
}
};
859. 亲密字符串
给你两个字符串 s
和 goal
,只要我们可以通过交换 s
中的两个字母得到与 goal
相等的结果,就返回 true
;否则返回 false
。
交换字母的定义是:取两个下标 i
和 j
(下标从 0
开始)且满足 i != j
,接着交换 s[i]
和 s[j]
处的字符。
"abcd"
中交换下标 0
和下标 2
的元素可以生成 "cbad"
。
示例 1:
输入:s = "ab", goal = "ba" 输出:true 解释:你可以交换 s[0] = 'a' 和 s[1] = 'b' 生成 "ba",此时 s 和 goal 相等。
示例 2:
输入:s = "ab", goal = "ab" 输出:false 解释:你只能交换 s[0] = 'a' 和 s[1] = 'b' 生成 "ba",此时 s 和 goal 不相等。
示例 3:
输入:s = "aa", goal = "aa" 输出:true 解释:你可以交换 s[0] = 'a' 和 s[1] = 'a' 生成 "aa",此时 s 和 goal 相等。
提示:
1 <= s.length, goal.length <= 2 * 104
s
和 goal
由小写英文字母组成原站题解
javascript 解法, 执行用时: 64 ms, 内存消耗: 41.9 MB, 提交时间: 2023-09-27 14:50:28
/** * @param {string} s * @param {string} goal * @return {boolean} */ var buddyStrings = function(s, goal) { if (s.length != goal.length) { return false; } if (s === goal) { const count = new Array(26).fill(0); for (let i = 0; i < s.length; i++) { count[s[i].charCodeAt() - 'a'.charCodeAt()]++; if (count[s[i].charCodeAt() - 'a'.charCodeAt()] > 1) { return true; } } return false; } else { let first = -1, second = -1; for (let i = 0; i < s.length; i++) { if (s[i] !== goal[i]) { if (first === -1) first = i; else if (second === -1) second = i; else return false; } } return (second !== -1 && s[first] === goal[second] && s[second] === goal[first]); } };
python3 解法, 执行用时: 32 ms, 内存消耗: 16.1 MB, 提交时间: 2023-09-27 14:50:12
class Solution: def buddyStrings(self, s: str, goal: str) -> bool: if len(s) != len(goal): return False if s == goal: if len(set(s)) < len(goal): return True else: return False diff = [(a, b) for a, b in zip(s, goal) if a != b] return len(diff) == 2 and diff[0][0] == diff[1][1] and diff[0][1] == diff[1][0]
java 解法, 执行用时: 1 ms, 内存消耗: 40.5 MB, 提交时间: 2023-09-27 14:49:57
class Solution { public boolean buddyStrings(String s, String goal) { if (s.length() != goal.length()) { return false; } if (s.equals(goal)) { int[] count = new int[26]; for (int i = 0; i < s.length(); i++) { count[s.charAt(i) - 'a']++; if (count[s.charAt(i) - 'a'] > 1) { return true; } } return false; } else { int first = -1, second = -1; for (int i = 0; i < goal.length(); i++) { if (s.charAt(i) != goal.charAt(i)) { if (first == -1) first = i; else if (second == -1) second = i; else return false; } } return (second != -1 && s.charAt(first) == goal.charAt(second) && s.charAt(second) == goal.charAt(first)); } } }
cpp 解法, 执行用时: 4 ms, 内存消耗: 7.2 MB, 提交时间: 2023-09-27 14:49:43
class Solution { public: bool buddyStrings(string s, string goal) { if (s.size() != goal.size()) { return false; } if (s == goal) { vector<int> count(26); for (int i = 0; i < s.size(); i++) { count[s[i] - 'a']++; if (count[s[i] - 'a'] > 1) { return true; } } return false; } else { int first = -1, second = -1; for (int i = 0; i < s.size(); i++) { if (s[i] != goal[i]) { if (first == -1) first = i; else if (second == -1) second = i; else return false; } } return (second != -1 && s[first] == goal[second] && s[second] == goal[first]); } } };
golang 解法, 执行用时: 0 ms, 内存消耗: 2.2 MB, 提交时间: 2023-09-27 14:49:19
func buddyStrings(s, goal string) bool { if len(s) != len(goal) { return false } if s == goal { seen := [26]bool{} for _, ch := range s { if seen[ch-'a'] { return true } seen[ch-'a'] = true } return false } first, second := -1, -1 for i := range s { if s[i] != goal[i] { if first == -1 { first = i } else if second == -1 { second = i } else { return false } } } return second != -1 && s[first] == goal[second] && s[second] == goal[first] }
golang 解法, 执行用时: 0 ms, 内存消耗: 2.4 MB, 提交时间: 2021-06-07 16:55:35
func buddyStrings(s string, goal string) bool { if len(s) != len(goal) { return false } if s == goal { mp := map[byte]int{} for i := 0; i < len(s); i++ { if _, ok := mp[s[i]]; ok { return true } else { mp[s[i]]++ } } return false } var pair [][]byte for i := 0; i < len(s); i++ { if s[i] != goal[i] { pair = append(pair, []byte{s[i], goal[i]}) } if len(pair) > 2 { return false } } if len(pair) != 2 { return false } return pair[0][0] == pair[1][1] && pair[0][1] == pair[1][0] }