-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbraceExpansionII.cpp
More file actions
39 lines (38 loc) · 1.17 KB
/
braceExpansionII.cpp
File metadata and controls
39 lines (38 loc) · 1.17 KB
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
// Source: https://leetcode.com/problems/brace-expansion-ii/
// Author: Miao Zhang
// Date: 2021-04-08
class Solution {
public:
vector<string> braceExpansionII(string expression) {
vector<string> res;
unordered_set<string> visited;
stack<string> st;
st.push(expression);
while (!st.empty()) {
string str = st.top(); st.pop();
if (str.find("{") == string::npos) {
if (!visited.count(str)) {
visited.insert(str);
res.push_back(str);
}
continue;
}
int i = 0, left = 0, right = 0;
while (str[i] != '}') {
if (str[i] == '{') left = i;
i++;
}
right = i;
string before = str.substr(0, left);
string after = str.substr(right + 1);
string mid = str.substr(left + 1, right - left - 1);
istringstream is(mid);
string t;
while (getline(is, t, ',')) {
st.push(before + t + after);
}
}
sort(res.begin(), res.end());
return res;
}
};