-
Notifications
You must be signed in to change notification settings - Fork 160
/
Find the replace rule.cpp
49 lines (35 loc) · 1.07 KB
/
Find the replace rule.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
/*
Given a list of strings l1, the string follow the lexicographical order.
Now we do a one to one mapping for the chars, such as a->e, p->d, l->f, e->o,
b->...We get another list of strings l2.
Suppose we only know l2, find the mapping rule.
*/
/*
solution:
O(n*len) time, O(26) space, n is the number of strings, and len is the average length of each string.
*/
#include<iostream>
#include<unordered_map>
using namespace std;
void FindMapRule(string s[], int len) {
unordered_map<char, char> rule;
char begin = s[0][0];
for (int i = 0; i < len - 1; ++i) {
int j = i + 1;
size_t k = 0;
while (k < s[i].length() && k < s[j].length() && s[i][k] == s[j][k]) {
k++;
}
rule[s[i][k]] = s[j][k];
if(s[j][k] == begin) begin = s[i][k];
}
while (rule.find( begin ) != rule.end()) {
cout<< begin << " ->" << rule[begin] <<endl;
begin = rule[begin];
}
}
int main() {
string s[] = {"erg", "wrf", "er", "ett", "rftt","te","ba","bw","fw"};
FindMapRule(s, 9);
return 0;
}