forked from shijbian/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneralized-abbreviation.cpp
More file actions
29 lines (27 loc) · 897 Bytes
/
generalized-abbreviation.cpp
File metadata and controls
29 lines (27 loc) · 897 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
// Time: O(n * 2^n)
// Space: O(n)
class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
string cur;
generateAbbreviationsHelper(word, 0, &cur, &res);
return res;
}
void generateAbbreviationsHelper(const string& word, int i, string *cur, vector<string> *res) {
if (i == word.length()) {
res->emplace_back(*cur);
return;
}
cur->push_back(word[i]);
generateAbbreviationsHelper(word, i + 1, cur, res);
cur->pop_back();
if (cur->empty() || not isdigit(cur->back())) {
for (int l = 1; i + l <= word.length(); ++l) {
cur->append(to_string(l));
generateAbbreviationsHelper(word, i + l, cur, res);
cur->resize(cur->length() - to_string(l).length());
}
}
}
};