-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecode_String.java
More file actions
90 lines (77 loc) · 2.62 KB
/
Decode_String.java
File metadata and controls
90 lines (77 loc) · 2.62 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
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.leet_code;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;
public class Decode_String {
public static void main(String[] args) {
String str="10[a]2[bc3[d]]";
System.out.println(decodeString(str));
}
public static String decodeString(String s) {// not yours
Stack<Integer> numStack = new Stack<>();
Stack<String> stringStack = new Stack<>();
int k = 0;
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) {
k = (k * 10) + (c - '0');
continue;
}
if (c == '[') {
numStack.push(k);
k = 0;
stringStack.push(String.valueOf(c));
continue;
}
if (c != ']') {
stringStack.push(String.valueOf(c));
continue;
}
StringBuilder temp = new StringBuilder();
while (!stringStack.peek().equals("["))
temp.insert(0, stringStack.pop());
// remove the "["
stringStack.pop();
// Get the new string
StringBuilder replacement = new StringBuilder();
int count = numStack.pop();
for (int i = 0; i < count; i++)
replacement.append(temp);
// Add it to the stack
stringStack.push(replacement.toString());
}
StringBuilder result = new StringBuilder();
while (!stringStack.empty()) {
result.insert(0, stringStack.pop());
}
return result.toString();
}
public static String decodeString1(String s) {//more efficent
Deque<Character> queue=new LinkedList<Character>();
for(char sb:s.toCharArray())queue.offer(sb);
return helper(queue);
}
public static String helper(Deque<Character> queue){
StringBuilder sb=new StringBuilder();
int num=0;
while(!queue.isEmpty()){
char temp=queue.poll();
if(temp-'0'>=0 && temp-'0'<=9){
num=num*10+temp-'0';
}
else if(temp=='['){
String sub=helper(queue);
while(num>0){
sb.append(sub);
num--;
}
}
else if(temp==']'){
break;
}
else{
sb.append(temp);
}
}
return sb.toString();
}
}