Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4fe5c13

Browse files
D. J.:
- Added the leetcode problem and solution for 394
1 parent 3632917 commit 4fe5c13

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@
226226
- [380 Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/description/)
227227
- [383 Ransom Note](https://leetcode.com/problems/ransom-note/description/)
228228
- [392 Is Subsequence](https://leetcode.com/problems/is-subsequence/description/)
229+
- [394 Decode String](https://leetcode.com/problems/decode-string/description/)
229230
- [399 Evaluate Division](https://leetcode.com/problems/evaluate-division/description/)
230231
- [412 Fizz Buzz](https://leetcode.com/problems/fizz-buzz/description/)
231232
- [415 Add Strings](https://leetcode.com/problems/add-strings/description/)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def decodeString(self, s: str) -> str:
5+
"""
6+
Given an encoded string, return its decoded string.
7+
8+
The encoding rule is: k[encoded_string], where the encoded_string inside the
9+
square brackets is being repeated exactly k times. Note that k is guaranteed to
10+
be a positive integer.
11+
12+
You may assume that the input string is always valid; there are no extra white
13+
spaces, square brackets are well-formed, etc. Furthermore, you may assume that
14+
the original data does not contain any digits and that digits are only for those
15+
repeat numbers, k. For example, there will not be input like 3a or 2[4].
16+
17+
The test cases are generated so that the length of the output will never exceed
18+
10^5.
19+
"""
20+
decode = ""
21+
i = 0
22+
while i < len(s):
23+
# Check if character is a string
24+
if s[i] >= "a" and s[i] <= "z":
25+
decode += s[i]
26+
i += 1
27+
continue
28+
# Get the occurence for the open bracket [
29+
j = i
30+
while j < len(s) and s[j] != "[":
31+
j += 1
32+
number = int(str(s[i:j]))
33+
34+
# Get the last occurence for the closed bracket ]
35+
openBrackets = 1
36+
k = j + 1
37+
while k < len(s) and openBrackets > 0:
38+
if s[k] == "[":
39+
openBrackets += 1
40+
if s[k] == "]":
41+
openBrackets -= 1
42+
k += 1
43+
substr = self.decodeString(s[j + 1 : k - 1])
44+
decode += number * substr
45+
i = k
46+
return decode

‎tests/test_394_decode_string.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._394_decode_string import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["s", "expected"],
8+
argvalues=[
9+
("3[a]2[bc]", "aaabcbc"),
10+
("3[a2[c]]", "accaccacc"),
11+
("2[abc]3[cd]ef", "abcabccdcdcdef"),
12+
],
13+
)
14+
def test_func(s: str, expected: str):
15+
"""Tests the solution of a LeetCode problem."""
16+
decoded_s = Solution().decodeString(s)
17+
assert decoded_s == expected

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /