-
Notifications
You must be signed in to change notification settings - Fork 24
/
0859-buddy-strings.js
89 lines (78 loc) · 2.32 KB
/
0859-buddy-strings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 859. Buddy Strings
// Easy 28%
// Given two strings A and B of lowercase letters, return true if and only if we
// can swap two letters in A so that the result equals B.
// Example 1:
// Input: A = "ab", B = "ba"
// Output: true
// Example 2:
// Input: A = "ab", B = "ab"
// Output: false
// Example 3:
// Input: A = "aa", B = "aa"
// Output: true
// Example 4:
// Input: A = "aaaaaaabc", B = "aaaaaaacb"
// Output: true
// Example 5:
// Input: A = "", B = "aa"
// Output: false
// Note:
// 0 <= A.length <= 20000
// 0 <= B.length <= 20000
// A and B consist only of lowercase letters.
/**
* @param {string} A
* @param {string} B
* @return {boolean}
*/
const buddyStrings = function(A, B) {
if (A.length !== B.length) return false
let a = '', b = '', count = {}, swap = 0
for (let i = 0; i < A.length; i++) {
count[A[i]] = (count[A[i]] || 0) + 1
if (A[i] !== B[i]) {
if (swap === 0) {
a = A[i]
b = B[i]
swap++
} else if (swap === 1) {
if (a !== B[i] || b !== A[i]) return false
swap++
} else return false
}
}
for (let key in count) if (count[key] > 1) return true
return swap === 2
}
const better = function(A, B) {
if (A.length !== B.length) return false
if (A === B && (new Set(A)).size < A.length) return true
const dif = []
for (let i = 0; i < A.length; i++) {
if (A[i] !== B[i]) dif.push(i)
}
return dif.length === 2 && A[dif[0]] === B[dif[1]] && A[dif[1]] === B[dif[0]]
}
;[
['ab', 'ba'], // true
['ab', 'ab'], // false
['aa', 'aa'], // true
['aaaaaaabc', 'aaaaaaacb'], // true
['', 'aa'], // false
['abab', 'abab'], // true
['acbb', 'abcc'], // false
].forEach(([A, B]) => {
console.log(buddyStrings(A, B))
// console.log(better(A, B))
})
// Solution:
// 记录字符 `a` 和 `b`,记录交换的次数 `swap`,并记录每个字符出现的次数。
// a 和 b 用于记录交换的字符
// swap 不能大于2,否则返回 false
// 记录字符次数,是为了在两个字符串相同时,看看有没有可交换的两个相同的字符。
// 更好的方法
// 当字符串相同时,用 Set 来判断,是否有重复字符
// 遍历字符串时,使用数组记录要交换的字符的下标
// 最后判断数组的长度是否等于2,并判断交换后是否相同。
// Submission Result: Accepted