1
+ #include < algorithm>
2
+ #include < vector>
3
+ #include < queue>
4
+ #include < set>
5
+ #include < limits>
6
+ #include < map>
7
+ #include < unordered_set>
8
+ #include < unordered_map>
9
+ #include < iterator>
10
+ #include < sstream>
11
+ #include < iostream> // includes cin to read from stdin and cout to write to stdout
12
+ using namespace std ; // since cin and cout are both in namespace std, this saves some text
13
+
14
+ class Solution {
15
+ public:
16
+ vector<string> generateAbbreviations (string word) {
17
+ if (word.size () == 0 ) {
18
+ // TODO: confirm this
19
+ return {};
20
+ }
21
+
22
+ auto answer = generate (word, 0 );
23
+ return vector<string>(begin (answer), end (answer));
24
+ }
25
+
26
+ private:
27
+ unordered_set<string> generate (string &word, int i) {
28
+ if (i >= word.size ()) {
29
+ return {" " };
30
+ }
31
+
32
+ unordered_set<string> answer;
33
+
34
+ int start = i;
35
+ while (start < word.size ()) {
36
+ int headSize = start - i;
37
+ int abbreviationSize = 1 ;
38
+ while (start + abbreviationSize <= word.size ()) {
39
+ string head = word.substr (i, headSize) + to_string (abbreviationSize);
40
+ if (start + abbreviationSize < word.size ()) {
41
+ head += word[start + abbreviationSize];
42
+ }
43
+
44
+ for (auto & tail: generate (word, start + abbreviationSize + 1 )) {
45
+
46
+ if (head + tail == " 1o" || head + tail == " " || i == 2 ) {
47
+ cout << start << " : " << head << " _" << abbreviationSize << " _" << tail << " :" << (start + abbreviationSize + 1 ) << endl;
48
+ }
49
+
50
+ answer.insert (head + tail);
51
+ }
52
+
53
+ abbreviationSize++;
54
+ }
55
+
56
+ start++;
57
+ headSize = start - i;
58
+ string head = word.substr (i, headSize);
59
+ for (auto & tail: generate (word, start)) {
60
+
61
+ if (head + tail == " 1o" || head + tail == " " || i == 2 ) {
62
+ cout << start << " : " << head << " _0_" << tail << endl;
63
+ }
64
+
65
+
66
+ answer.insert (head + tail);
67
+ }
68
+ }
69
+
70
+
71
+ return answer;
72
+ }
73
+ };
0 commit comments