-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy path亲密字符串.js
More file actions
31 lines (31 loc) · 727 Bytes
/
亲密字符串.js
File metadata and controls
31 lines (31 loc) · 727 Bytes
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
/**
* @param {string} s
* @param {string} goal
* @return {boolean}
859. 亲密字符串
*/
var buddyStrings = function(s, goal) {
if (s.length !== goal.length) return false
if (s === goal) {
if ([...new Set(s.split(''))].length < goal.length) return true
return false
}
let i = 0,
j
const s1 = s.split(''),
goal1 = goal.split('')
while (s1[i] === goal[i]) {
i++
}
j = i + 1
while (j < s.length && s1[j] === goal1[j]) {
j++
}
if (j === s.length) return false
if (s1[i] !== goal1[j] || s1[j] !== goal1[i]) return false
j = j + 1
while (j < s.length && s1[j] === goal1[j]) {
j++
}
return j === s.length
};