-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
38 lines (37 loc) · 1022 Bytes
/
Solution.cpp
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
class Solution {
public:
int kSimilarity(string s1, string s2) {
queue<string> q{{s1}};
unordered_set<string> vis{{s1}};
int ans = 0;
while (1) {
for (int i = q.size(); i; --i) {
auto s = q.front();
q.pop();
if (s == s2) {
return ans;
}
for (auto& nxt : next(s, s2)) {
if (!vis.count(nxt)) {
vis.insert(nxt);
q.push(nxt);
}
}
}
++ans;
}
}
vector<string> next(string& s, string& s2) {
int i = 0, n = s.size();
for (; s[i] == s2[i]; ++i) {}
vector<string> res;
for (int j = i + 1; j < n; ++j) {
if (s[j] == s2[i] && s[j] != s2[j]) {
swap(s[i], s[j]);
res.push_back(s);
swap(s[i], s[j]);
}
}
return res;
}
};