-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathk-SimilarStrings.cpp
More file actions
35 lines (34 loc) · 1014 Bytes
/
k-SimilarStrings.cpp
File metadata and controls
35 lines (34 loc) · 1014 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
32
33
34
35
// Source: https://leetcode.com/problems/k-similar-strings/
// Author: Miao Zhang
// Date: 2021-03-18
class Solution {
public:
int kSimilarity(string s1, string s2) {
if (s1 == s2) return 0;
queue<string> q;
q.push(s1);
int step = 0;
unordered_set<string> visited;
visited.insert(s1);
while (!q.empty()) {
int len = q.size();
while (len--) {
string s3 = q.front();
q.pop();
int i = 0;
while (s3[i] == s2[i]) i++;
for (int j = i + 1; j < s3.size(); j++) {
if (s3[j] != s2[i]) continue;
string s4 = s3;
swap(s4[i], s4[j]);
if (s4 == s2) return step + 1;
if (visited.count(s4)) continue;
visited.insert(s4);
q.push(s4);
}
}
step++;
}
return -1;
}
};