forked from kaidul/LeetCode_problems_solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generalized_Abbreviation.cpp
73 lines (71 loc) · 2.34 KB
/
Generalized_Abbreviation.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
// bit manipulation
class Solution {
string getAbbr(string& word, int mask) {
int n = word.length();
string abbr = "";
int right = 0;
while(right < n) {
if(mask & (1 << right)) {
int left = right;
while(mask & (1 << right)) {
++right;
}
int len = right - left;
abbr += to_string(len);
} else {
abbr += word[right++];
}
}
return abbr;
}
public:
vector<string> generateAbbreviations(string word) {
vector<string> result;
int n = word.length();
int Max = (1 << n);
for(int i = 0; i < Max; ++i) {
string abbr = getAbbr(word, i);
result.push_back(abbr);
}
return result;
}
};
// backtracking
class Solution {
void generateAbbreviationsRecur(string word, unordered_set<string>& visited, vector<string>& result) {
if(visited.find(word) != visited.end()) {
return;
}
visited.insert(word);
result.push_back(word);
bool proceed = false; // variable for early prunning
for(int i = 1, n = word.size(); i <= n; ++i) {
string sDigit = to_string(i);
for(int j = 0; j + i - 1 < n; ++j) {
bool isValidPrefix = (j == 0 or (j > 0 and !isdigit(word[j - 1])));
bool isValidSuffix = (j + i - 1 == n - 1 or (j + i - 1 < n - 1 and !isdigit(word[j + i])));
bool isValidMid = true;
for(int k = j; k < j + i; ++k) {
if(isdigit(word[k])) {
isValidMid = false;
break;
}
}
if(isValidMid and isValidPrefix and isValidSuffix) {
string abbr = word.substr(0, j) + sDigit + word.substr(j + i);
generateAbbreviationsRecur(abbr, visited, result);
proceed = true;
}
}
if(!proceed) break;
proceed = false;
}
}
public:
vector<string> generateAbbreviations(string word) {
vector<string> result;
unordered_set<string> visited;
generateAbbreviationsRecur(word, visited, result);
return result;
}
};