-
Notifications
You must be signed in to change notification settings - Fork 0
/
1087-Brace Expansion.cpp
51 lines (50 loc) · 1.13 KB
/
1087-Brace Expansion.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
class Solution {
public:
vector<string> result;
string temp;
void expandFromList(string &s,int i,bool isSelected)
{
int next=s.find_first_of(",}",i);
if(!isSelected)
{
string word=s.substr(i,next-i);
temp+=word;
if(s[next]=='}')
expand(s,next+1);
else
expandFromList(s,next+1,true);
temp.pop_back();
if(s[next]!='}')
expandFromList(s,next+1,false);
}
else
{
if(s[next]=='}')
expand(s,next+1);
else
expandFromList(s,next+1,true);
}
}
void expand(string &s,int i)
{
if(i==s.length())
{
result.push_back(temp);
return;
}
if(s[i]=='{')
expandFromList(s,i+1,false);
else
{
temp+=s[i];
expand(s,i+1);
temp.pop_back();
}
}
vector<string> expand(string S)
{
expand(S,0);
sort(result.begin(),result.end());
return result;
}
};