-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdecodeString.java
More file actions
77 lines (69 loc) · 1.53 KB
/
decodeString.java
File metadata and controls
77 lines (69 loc) · 1.53 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//s = "3[a]2[bc]", return "aaabcbc".
// s = "3[a2[c]]", return "accaccacc".
// s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
class Node {
int num;
ArrayList<Node> list;
char symbol;
boolean isList;
public Node(char s) {
symbol=s;
}
public Node(int n){
list = new ArrayList<Node>();
isList=true;
num=n;
}
public String toString(){
String s = "";
if(isList) {
s += num + ":" + list.toString();
} else {
s += symbol;
}
return s;
}
}
public class Solution {
public String decodeString(String s) {
int i = 0;
Stack<Node> stack = new Stack<Node>();
stack.push(new Node(1));
String t = "";
while (i < s.length()) {
char c = s.charAt(i);
// new Node
if (c >= '0' && c <= '9') { // number
t += c;
} else if (c == '[') {
if (t.length() > 0) {
int num = Integer.parseInt(t);
stack.push(new Node(num));
t = "";
}
} else if (c == ']') {
Node top = stack.pop();
if (stack.isEmpty()) {
} else {
stack.peek().list.add(top);
}
} else {
stack.peek().list.add(new Node(c));
}
i++;
}
return getString(stack.peek());
}
public String getString(Node node) {
String s= "";
if (node.isList) {
for(int i=0; i<node.num; i++){
for(Node t: node.list)
s+= getString(t);
}
} else {
s+=node.symbol;
}
return s;
}
}