-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy paths1.cpp
More file actions
29 lines (29 loc) · 792 Bytes
/
Copy paths1.cpp
File metadata and controls
29 lines (29 loc) · 792 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
// OJ: https://leetcode.com/problems/generalized-abbreviation/
// Author: github.com/lzl124631x
// Time: O(2^W * W)
// Space: O(W)
class Solution {
private:
string encode(string word, int mask) {
string ans;
int cnt = 0;
for (int i = 0; i < word.size(); ++i) {
if (mask & (1 << i)) ++cnt;
else {
if (cnt) {
ans += to_string(cnt);
cnt = 0;
}
ans += word[i];
}
}
if (cnt) ans += to_string(cnt);
return ans;
}
public:
vector<string> generateAbbreviations(string word) {
vector<string> ans;
for (int i = 0; i < (1 << word.size()); ++i) ans.push_back(encode(word, i));
return ans;
}
};