-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathK-Similar Strings.js
50 lines (45 loc) · 1.17 KB
/
K-Similar 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
var kSimilarity = function(s1, s2) {
// abc --> bca
// swap from 0: a !== b, find next b, swap(0,1) --> bac
// swap from 1: a !== c, find next c, swap(1,2) --> bca
return bfs(s1, s2);
};
const bfs = (a,b)=>{
if(a===b)
return 0;
const visited = new Set();
const queue = [];
queue.push([a,0,0]); // str, idx, swapCount
while(queue.length>0)
{
let [s, idx, cnt] = queue.shift();
while(s[idx]===b[idx])
{
idx++;
}
for(let j = idx+1; j<s.length; j++)
{
if(s[j]===b[idx]) {
s = swap(s, idx, j);
if(s===b) {
return cnt+1;
}
if(!visited.has(s))
{
queue.push([s.slice(), idx, cnt+1]);
visited.add(s.slice());
}
// swap back for later index
s = swap(s, idx, j);
}
}
}
return -1;
}
const swap = (s, i, j)=>{
let arr = s.split('');
let tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
return arr.join('');
}