Skip to content

Commit 18a8af4

Browse files
authored
Create 320.Generalized-Abbreviation.cpp
1 parent 3c538f9 commit 18a8af4

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<string> generateAbbreviations(string word)
4+
{
5+
int n = word.size();
6+
vector<string>rets;
7+
for (int state = 0; state < (1<<n); state++)
8+
{
9+
string s;
10+
for (int i=0; i<n; i++)
11+
{
12+
if ((state>>i)&1)
13+
{
14+
s.push_back(word[i]);
15+
}
16+
else
17+
{
18+
int j = i;
19+
while (j<n && ((state>>j)&1)==0)
20+
j++;
21+
s += to_string(j-i);
22+
i = j-1;
23+
}
24+
}
25+
rets.push_back(s);
26+
}
27+
return rets;
28+
}
29+
};

0 commit comments

Comments
 (0)