-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathk similar strings
More file actions
62 lines (54 loc) · 1.54 KB
/
Copy pathk similar strings
File metadata and controls
62 lines (54 loc) · 1.54 KB
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
/*
Strings A and B are K-similar (for some non-negative integer K) if we can swap the positions of two letters in A exactly K times so that
the resulting string equals B.
Given two anagrams A and B, return the smallest K for which A and B are K-similar.
Example 1:
Input: A = "ab", B = "ba"
Output: 1
Example 2:
Input: A = "abc", B = "bca"
Output: 2
Example 3:
Input: A = "abac", B = "baca"
Output: 2
Example 4:
Input: A = "aabc", B = "abca"
Output: 2
Note:
1 <= A.length == B.length <= 20
A and B contain only lowercase letters from the set {'a', 'b', 'c', 'd', 'e', 'f'}
*/
class Solution {
public:
int kSimilarity(string A, string B) {
unordered_set<string> v;
queue<string> q;
q.push(A);
v.insert(A);
int step=0;
while(q.size()){
int size = q.size();
while(size-- > 0){
auto s = q.front();
q.pop();
if(s==B) return step;
for(int i=0; i<s.size(); ++i){
if(s[i] == B[i]) continue;
for(int j=i+1; j<B.size(); ++j){
if(s[i] == B[j]){
swap(s[i], s[j]);
if(v.count(s) == 0){
q.push(s);
v.insert(s);
}
swap(s[i], s[j]);
}
}
break;
}
}
++step;
}
return step;
}
};