-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecode String.java
More file actions
44 lines (41 loc) · 1.21 KB
/
Copy pathDecode String.java
File metadata and controls
44 lines (41 loc) · 1.21 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
40
41
42
43
44
class Solution {
public String decodeString(String s) {
String res = "";
Stack<Integer> countStack = new Stack<>();
Stack<String> resStack = new Stack<>();
int index = 0;
while(index < s.length())
{
if(Character.isDigit(s.charAt(index)))
{
int count = 0;
while(Character.isDigit(s.charAt(index)))
{
count = 10 * count + (s.charAt(index) - '0');
index++;
}
countStack.push(count);
}
else if(s.charAt(index) == '[')
{
resStack.push(res);
res = "";
index++;
}
else if(s.charAt(index) == ']')
{
StringBuilder temp = new StringBuilder(resStack.pop());
int x = countStack.pop();
for(int i = 0; i<x; i++)
temp.append(res);
res = temp.toString();
index++;
}
else{
res += s.charAt(index);
index++;
}
}
return res;
}
}