From d05290973f5595cac906d78dca220093f7bec2de Mon Sep 17 00:00:00 2001 From: pro100ton Date: Fri, 27 Dec 2024 16:40:45 +0300 Subject: [PATCH] Add leetcode 20 task solution --- .../1929_concatenation_of_array.py | 17 ++++++ leetcode/stacks/20_valid_parentheses.py | 58 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 leetcode/dynamic_arrays/1929_concatenation_of_array.py create mode 100644 leetcode/stacks/20_valid_parentheses.py diff --git a/leetcode/dynamic_arrays/1929_concatenation_of_array.py b/leetcode/dynamic_arrays/1929_concatenation_of_array.py new file mode 100644 index 0000000..0a8ccc6 --- /dev/null +++ b/leetcode/dynamic_arrays/1929_concatenation_of_array.py @@ -0,0 +1,17 @@ +# Link: https://leetcode.com/problems/concatenation-of-array/ +from typing import List + + +class Solution: + def getConcatenation(self, nums: List[int]) -> List[int]: + # ans = [] * (len(nums) * 2) + ans = nums + nums + return ans + + +if __name__ == "__main__": + nums = [2, 5, 1, 3, 4, 7] + sol = Solution + print(nums) + print(sol().getConcatenation(nums)) + print("-" * 60) diff --git a/leetcode/stacks/20_valid_parentheses.py b/leetcode/stacks/20_valid_parentheses.py new file mode 100644 index 0000000..f59780b --- /dev/null +++ b/leetcode/stacks/20_valid_parentheses.py @@ -0,0 +1,58 @@ +# Link: https://leetcode.com/problems/valid-parentheses/description/ + + +class Solution: + def isValid(self, s: str) -> bool: + c = [] # Checker stack + m = { + ")": "(", + "}": "{", + "]": "[", + } + cb = m.keys() + op = m.values() + for char in s: + if len(c) == 0: + c.append(char) + continue + if char in op: + c.append(char) + continue + if char in cb: + if len(c) > 0 and c[-1] == m[char]: + c.pop() + continue + if len(c) > 0 and c[-1] != m[char]: + return False + if len(c) == 0: + return False + print(f"c = {c} (len={len(c)})") + return False if len(c) > 0 else True + + +if __name__ == "__main__": + tt = "()[]{}" + sol = Solution + print(f"Initial: {tt}") + print(sol().isValid(tt)) + print("-" * 60) + tt = "()" + print(f"Initial: {tt}") + print(sol().isValid(tt)) + print("-" * 60) + tt = "())" + print(f"Initial: {tt}") + print(sol().isValid(tt)) + print("-" * 60) + tt = "([])" + print(f"Initial: {tt}") + print(sol().isValid(tt)) + print("-" * 60) + tt = "(" + print(f"Initial: {tt}") + print(sol().isValid(tt)) + print("-" * 60) + tt = "(])" + print(f"Initial: {tt}") + print(sol().isValid(tt)) + print("-" * 60)