Skip to content

Commit d35b3b7

Browse files
varunu28fishercoder1534
authored andcommitted
Added _394.java (fishercoder1534#31)
Added _394.java
1 parent bbf56ba commit d35b3b7

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ Your ideas/fixes/algorithms are more than welcome!
371371
|397|[Integer Replacement](https://leetcode.com/problems/integer-replacement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_397.java)| ? | ? | |Easy| BFS
372372
|396|[Rotate Function](https://leetcode.com/problems/rotate-function/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_396.java)| O(n^2) could be optimized to O(n) | O(1) | |Easy|
373373
|395|[Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_395.java)| O(n^2) | O(1) | |Medium| Recursion
374+
|394|[Decode String](https://leetcode.com/problems/decode-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_394.java)| O(n) | O(n) | |Medium| Stack Depth-first-search
374375
|393|[UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_393.java)| O(?)|O(?) | |Medium| Bit Manipulation
375376
|392|[Is Subsequence](https://leetcode.com/problems/is-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_392.java)| O(m*n)|O(1) | |Medium| Array, String
376377
|391|[Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_391.java)| O(n)|O(1) | |Hard|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Stack;
4+
5+
public class _394 {
6+
7+
public static class Solution1 {
8+
public String decodeString(String s) {
9+
Stack<Integer> count = new Stack<>();
10+
Stack<String> str = new Stack<>();
11+
12+
int idx = 0;
13+
str.push("");
14+
15+
while(idx < s.length()) {
16+
if (s.charAt(idx) >= '0' && s.charAt(idx) <= '9') {
17+
int start = idx;
18+
while (s.charAt(idx + 1) >= '0' && s.charAt(idx + 1) <= '9') {
19+
idx++;
20+
}
21+
22+
count.push(Integer.parseInt(s.substring(start, idx + 1)));
23+
} else if (s.charAt(idx) == '[') {
24+
str.push("");
25+
} else if (s.charAt(idx) == ']') {
26+
String st = str.pop();
27+
StringBuilder sb = new StringBuilder();
28+
int n = count.pop();
29+
30+
for (int j = 0; j < n; j++) {
31+
sb.append(st);
32+
}
33+
34+
str.push(str.pop() + sb.toString());
35+
} else {
36+
str.push(str.pop() + s.charAt(idx));
37+
}
38+
39+
idx++;
40+
}
41+
42+
return str.pop();
43+
}
44+
}
45+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._394;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
/**
9+
* Created by varunu28 on 1/08/19.
10+
*/
11+
12+
public class _394Test {
13+
private static _394.Solution1 test;
14+
15+
public static void setUp() {
16+
test = new _394.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals("aaabcbc", test.decodeString("3[a]2[bc]"));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
assertEquals("accaccacc", test.decodeString("3[a2[c]]"));
27+
}
28+
29+
@Test
30+
public void test3() {
31+
assertEquals("abcabccdcdcdef", test.decodeString("2[abc]3[cd]ef"));
32+
}
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy