-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.cpp
46 lines (45 loc) · 920 Bytes
/
13.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
string decodedString(string str) {
stack<int> numbers;
stack<string> words;
int n = str.size();
for (int i = 0; i < n; i++) {
char c = str[i];
// deb(c);
if (c >= '0' && c <= '9') {
string number = "";
while (c >= '0' && c <= '9') {
number += c;
c = str[++i];
}
--i;
numbers.push(stoi(number));
} else if (c == '[') {
words.push("[");
} else if (c >= 'a' && c <= 'z') {
string word = "";
while (c >= 'a' && c <= 'z') {
word += c;
c = str[++i];
}
--i;
words.push(word);
} else if (c == ']') {
int number = numbers.top(); numbers.pop();
string word = "";
// deb(number);
while (words.top() != "[") {
word = words.top() + word;
words.pop();
}
words.pop();
// deb(word);
string numWord = "";
while (number--)
numWord += word;
// deb(numWord);
words.push(numWord);
// cout << endl;
}
}
return words.top();
}