diff --git a/README.md b/README.md index 42f572900c..f7223d9730 100644 --- a/README.md +++ b/README.md @@ -371,6 +371,7 @@ Your ideas/fixes/algorithms are more than welcome! |397|[Integer Replacement](https://leetcode.com/problems/integer-replacement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_397.java)| ? | ? | |Easy| BFS |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| |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 +|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 |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 |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 |391|[Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_391.java)| O(n)|O(1) | |Hard| diff --git a/src/main/java/com/fishercoder/solutions/_394.java b/src/main/java/com/fishercoder/solutions/_394.java new file mode 100644 index 0000000000..24870a6135 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/_394.java @@ -0,0 +1,45 @@ +package com.fishercoder.solutions; + +import java.util.Stack; + +public class _394 { + + public static class Solution1 { + public String decodeString(String s) { + Stack count = new Stack<>(); + Stack str = new Stack<>(); + + int idx = 0; + str.push(""); + + while(idx < s.length()) { + if (s.charAt(idx) >= '0' && s.charAt(idx) <= '9') { + int start = idx; + while (s.charAt(idx + 1) >= '0' && s.charAt(idx + 1) <= '9') { + idx++; + } + + count.push(Integer.parseInt(s.substring(start, idx + 1))); + } else if (s.charAt(idx) == '[') { + str.push(""); + } else if (s.charAt(idx) == ']') { + String st = str.pop(); + StringBuilder sb = new StringBuilder(); + int n = count.pop(); + + for (int j = 0; j < n; j++) { + sb.append(st); + } + + str.push(str.pop() + sb.toString()); + } else { + str.push(str.pop() + s.charAt(idx)); + } + + idx++; + } + + return str.pop(); + } + } +} diff --git a/src/test/java/com/fishercoder/_394Test.java b/src/test/java/com/fishercoder/_394Test.java new file mode 100644 index 0000000000..df43c52cfd --- /dev/null +++ b/src/test/java/com/fishercoder/_394Test.java @@ -0,0 +1,33 @@ +package com.fishercoder; + +import com.fishercoder.solutions._394; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by varunu28 on 1/08/19. + */ + +public class _394Test { + private static _394.Solution1 test; + + public static void setUp() { + test = new _394.Solution1(); + } + + @Test + public void test1() { + assertEquals("aaabcbc", test.decodeString("3[a]2[bc]")); + } + + @Test + public void test2() { + assertEquals("accaccacc", test.decodeString("3[a2[c]]")); + } + + @Test + public void test3() { + assertEquals("abcabccdcdcdef", test.decodeString("2[abc]3[cd]ef")); + } +}