forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.cpp
92 lines (71 loc) · 2.04 KB
/
main3.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/// Source : https://leetcode.com/problems/k-similar-strings/description/
/// Author : liuyubobobo
/// Time : 2018-06-21
#include <iostream>
#include <queue>
#include <cassert>
#include <unordered_set>
using namespace std;
/// Greedy + BFS
/// Time Complexity: O(2^n)
/// Space Complexity: O(2^n)
class Solution {
public:
int kSimilarity(string A, string B) {
assert(A.size() == B.size());
assert(A.size() > 0);
if(A.size() == 1){
assert(A[0] == B[0]);
return 0;
}
queue<pair<string, int>> q;
q.push(make_pair(A, 0));
unordered_set<string> visited;
visited.insert(A);
while(!q.empty()){
string curS = q.front().first;
int curStep = q.front().second;
if(curS == B)
return curStep;
q.pop();
int start = 0;
for( ; start < curS.size() ; start ++)
if(curS[start] != B[start])
break;
for(int i = start + 1 ; i < curS.size() ; i ++)
if(curS[i] == B[start]){
swap(curS[start], curS[i]);
if(visited.find(curS) == visited.end()){
visited.insert(curS);
q.push(make_pair(curS, curStep + 1));
}
swap(curS[start], curS[i]);
}
}
assert(false);
return -1;
}
};
int main() {
string A1 = "ab";
string B1 = "ba";
cout << Solution().kSimilarity(A1, B1) << endl;
// 1
string A2 = "abc";
string B2 = "bca";
cout << Solution().kSimilarity(A2, B2) << endl;
// 2
string A3 = "abac";
string B3 = "baca";
cout << Solution().kSimilarity(A3, B3) << endl;
// 2
string A4 = "aabc";
string B4 = "abca";
cout << Solution().kSimilarity(A4, B4) << endl;
// 2
string A5 = "abcdefabcdefabcdef";
string B5 = "edcfbebceafcfdabad";
cout << Solution().kSimilarity(A5, B5) << endl;
// 10
return 0;
}