Skip to content

Commit c3e4271

Browse files
committed
Add: Exercise 394. Decode String
1 parent 14515cc commit c3e4271

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

394-decode-string/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 394. Decode String
2+
Given an encoded string, return its decoded string.
3+
4+
The encoding rule is: `k[encoded_string]`, where the `encoded_string` inside the square brackets is
5+
being repeated exactly `k` times. Note that `k` is guaranteed to be a positive integer.
6+
7+
You may assume that the input string is always valid; there are no extra white spaces, square
8+
brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain
9+
any digits and that digits are only for those repeat numbers, `k`. For example, there will not be
10+
input like `3a` or `2[4]`.
11+
12+
The test cases are generated so that the length of the output will never exceed 10^5.
13+
14+
## Examples
15+
16+
### Example 1:
17+
18+
Input: `s` = "3[a]2[bc]"
19+
Output: "aaabcbc"
20+
Explanation: The string "3[a]2[bc]" is decoded as "aaabcbc".
21+
22+
### Example 2:
23+
24+
Input: `s` = "3[a2[c]]"
25+
Output: "accaccacc"
26+
Explanation: The string "3[a2[c]]" is decoded as "accaccacc".
27+
28+
### Example 3:
29+
30+
Input: `s` = "2[abc]3[cd]ef"
31+
Output: "abcabccdcdcdef"
32+
Explanation: The string "2[abc]3[cd]ef" is decoded as "abcabccdcdcdef".
33+
34+
## Constraints
35+
36+
- 1 <= `s.length` <= 30
37+
- `s` consists of lowercase English letters, digits, and square brackets `'[]'`.
38+
- `s` is guaranteed to be a valid input.
39+
- All the integers in `s` are in the range [1, 300].

394-decode-string/index.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
*
3+
* @param {*} s
4+
* @returns
5+
*/
6+
const decodeString = s => {
7+
const stack = []
8+
stack.peek = () => stack[stack.length - 1]
9+
10+
for (let c of s) {
11+
// Everything will be pushed to stack, until a ']' char is found
12+
if (c !== ']') {
13+
stack.push(c)
14+
continue
15+
}
16+
17+
// ']' found, let's obtain the substring that will be multiplied and added to the stack
18+
let subStr = ''
19+
while (stack.peek() !== '[') {
20+
subStr = stack.pop() + subStr
21+
}
22+
23+
// Eliminate '[' from the stack before to proceed
24+
stack.pop()
25+
26+
// '[' found, let's obtain the multiplier
27+
let multiplierStr = ''
28+
while (!Number.isNaN(Number(stack.peek()))) {
29+
multiplierStr = stack.pop() + multiplierStr
30+
}
31+
32+
const multiplier = Number(multiplierStr)
33+
34+
// Now let's muliply add the str to the stack
35+
for (let i = 0; i < multiplier; i++) {
36+
stack.push(subStr)
37+
}
38+
}
39+
40+
delete stack.peek
41+
42+
return stack.join('')
43+
};
44+
45+
var output = decodeString('3[a]2[bc]')
46+
47+
module.exports = decodeString

394-decode-string/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "394-decode-string",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"debug": "node --inspect-brk index.js",
9+
"test": "node test.js"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC"
14+
}

394-decode-string/test.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const assert = require('assert')
2+
const decodeString = require('./index')
3+
4+
const tests = [
5+
{
6+
s: '3[a]2[bc]',
7+
output: 'aaabcbc'
8+
},
9+
{
10+
s: '3[a2[c]]',
11+
output: 'accaccacc'
12+
},
13+
{
14+
s: '2[abc]3[cd]ef',
15+
output: 'abcabccdcdcdef'
16+
}
17+
]
18+
19+
for (const t of tests) {
20+
assert(decodeString(t.s), t.output)
21+
}

0 commit comments

Comments
 (0)