Skip to content

Commit c568e3b

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.6 MB (67.82%)
1 parent 9489dc6 commit c568e3b

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

0394-decode-string/README.md

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

0394-decode-string/solution.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Approach: Stack
2+
3+
# n = length of decoded string, m = number of nested brackets
4+
# Time: O(n)
5+
# Space: O(m)
6+
7+
class Solution:
8+
def decodeString(self, s: str) -> str:
9+
stack = [] # to keep track of nested brackets
10+
curr_str = ''
11+
curr_num = 0
12+
13+
for char in s:
14+
if char.isdigit():
15+
# Build the number
16+
curr_num = curr_num * 10 + int(char)
17+
elif char == '[':
18+
# Push the curr num and str to stacj
19+
stack.append((curr_str, curr_num))
20+
# Reset current values
21+
curr_str = ''
22+
curr_num = 0
23+
elif char == ']':
24+
prev_str, num = stack.pop()
25+
curr_str = prev_str + curr_str * num
26+
else:
27+
# Add characters to the current str
28+
curr_str += char
29+
30+
return curr_str

0 commit comments

Comments
 (0)