Skip to content

Commit 259b943

Browse files
author
Tushar Borole
committed
394. Decode String
1 parent c861441 commit 259b943

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed

.idea/workspace.xml

Lines changed: 16 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
| 36 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [Itterative](valid_sudoku.js) | 68 ms | 37.7 MB | Medium | O(1) | O(1) | [:link:](https://leetcode.com/problems/valid-sudoku/solution/) |
9999
| 146 | [ LRU Cache](https://leetcode.com/problems/lru-cache/) | [Map](lru_cache.js) | 216 ms | 58.8 MB | Medium | O(1) | O(1) | |
100100
| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [Dynamic Programming](max_product.js) | 60 ms | 35 MB | Medium | O(N) | O(1) | [:link:](https://www.youtube.com/watch?v=-rUBh45rugs) |
101+
| 394 | [Decode String](https://leetcode.com/problems/decode-string/) | [Decode String](decode_string.js) | 68 ms | 33.7 MB | Medium | | | |
101102
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) |
102103
| 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [DFS Recursion](serialize_and_deserialize_binary_tree.js) | 80 ms | 43.6 MB | Hard | | | [:link:](https://www.youtube.com/watch?v=suj1ro8TIVY) |
103104

decode_string.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//https://leetcode.com/problems/decode-string/
2+
3+
/**
4+
* @param {string} s
5+
* @return {string}
6+
*/
7+
var decodeString = function(s) {
8+
let arr = [];
9+
for (let i = 0; i < s.length; i++) {
10+
let isInteger = Number.isInteger(parseInt(s[i]));
11+
let numberString = "";
12+
while (isInteger) {
13+
isInteger = Number.isInteger(parseInt(s[i + 1]));
14+
numberString = numberString + s[i];
15+
i++;
16+
if (!isInteger) {
17+
arr.push(numberString);
18+
}
19+
}
20+
arr.push(s[i]);
21+
}
22+
let stack = [];
23+
for (let i = 0; i < arr.length; i++) {
24+
const current = arr[i];
25+
26+
if (current === "]") {
27+
let lastElement = stack.shift();
28+
let tempString = lastElement;
29+
while (lastElement !== "[") {
30+
lastElement = stack.shift();
31+
if (lastElement !== "[") {
32+
tempString = lastElement + tempString;
33+
}
34+
}
35+
const number = stack.shift();
36+
const tempOut = tempString
37+
.split("")
38+
.reverse()
39+
.join("")
40+
.repeat(number)
41+
.split(""); //?
42+
stack.unshift(...tempOut);
43+
} else {
44+
stack.unshift(current);
45+
}
46+
}
47+
48+
return stack.reverse().join("");
49+
};
50+
51+
decodeString("100[abc]3[cd]ef"); //?

0 commit comments

Comments
 (0)