From 9f61dccf2c3e6de113e88407f543e01168381211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=92=D0=B0=D1=81?= =?UTF-8?q?=D0=B8=D0=BB=D1=8C=D0=B5=D0=B2?= Date: Fri, 12 Dec 2025 09:20:41 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20[LeetCode=20#394]=20Decode=20String=20?= =?UTF-8?q?=D0=A2=D0=B8=D0=BF:=20Stack=20=D0=A1=D0=BB=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D1=8C:=20medium=20=D0=92=D1=80=D0=B5=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=BD=D0=B0=D1=8F=20=D1=81=D0=BB=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D1=8C:=20O(n)=20=D0=9F=D1=80=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D1=80=D0=B0=D0=BD=D1=81=D1=82=D0=B2=D0=B5=D0=BD=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C:=20O(n)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Ссылка: https://leetcode.com/problems/decode-string/ --- src/stack/decode_string/__init__.py | 0 src/stack/decode_string/solution.py | 20 ++++++++++++++++++++ tests/test_decode_string.py | 17 +++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 src/stack/decode_string/__init__.py create mode 100644 src/stack/decode_string/solution.py create mode 100644 tests/test_decode_string.py diff --git a/src/stack/decode_string/__init__.py b/src/stack/decode_string/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/stack/decode_string/solution.py b/src/stack/decode_string/solution.py new file mode 100644 index 0000000..6ac71ff --- /dev/null +++ b/src/stack/decode_string/solution.py @@ -0,0 +1,20 @@ +class Solution: + def decodeString(self, s: str) -> str: + stack = [] + number = [] + + for char in s: + if char.isdigit(): + number.append(char) + elif char == '[': + stack.append("".join(number)) + number = [] + elif char == ']': + alphas = [] + while stack[-1].isalpha(): + alphas.append(stack.pop()) + stack.extend(reversed(alphas * int(stack.pop()))) + else: + stack.append(char) + + return "".join(stack) diff --git a/tests/test_decode_string.py b/tests/test_decode_string.py new file mode 100644 index 0000000..1fe9e85 --- /dev/null +++ b/tests/test_decode_string.py @@ -0,0 +1,17 @@ +import pytest +from src.stack.decode_string.solution import ( + Solution, +) + + +@pytest.mark.parametrize( + "s, expected", + [ + ("3[a]2[bc]", "aaabcbc"), + ("3[a2[c]]", "accaccacc"), + ("2[abc]3[cd]ef", "abcabccdcdcdef"), + ], +) +def test_decode_string(s, expected): + solution = Solution() + assert solution.decodeString(s) == expected