-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathK-SimilarStrings854.cpp
More file actions
30 lines (30 loc) · 953 Bytes
/
Copy pathK-SimilarStrings854.cpp
File metadata and controls
30 lines (30 loc) · 953 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
//good BFS problem, we only need to exchange the valid or efficient character between two character
class Solution {
public:
int kSimilarity(string s1, string s2) {
if (s1 == s2) return 0;
unordered_set<string>used;
queue<string>q;
q.push(s1);
int ans = 0;
string s;
while (!q.empty()) {
ans++;
for (int i = (int)q.size(), j = 0; i > 0; i--) {
s = q.front();
q.pop();
used.insert(s);
j = 0;
while (s[j] == s2[j]) j++;
for (int k = j+1; k < s.size(); k++) {
if (s[k] == s2[k] || s[k]!=s2[j]) continue;
string tmp = s;
swap(tmp[j], tmp[k]);
if (tmp == s2) return ans;
if (!used.count(tmp)) q.push(tmp);
}
}
}
return ans;
}
};