From 593c0ddee9cbe4d6b49026dc460a8b632ebc03f7 Mon Sep 17 00:00:00 2001 From: Rytxxx Date: Mon, 5 Jan 2026 00:54:22 -0800 Subject: [PATCH 1/2] 020-valid-parentheses --- 020-valid-parentheses/memo.md | 22 ++++++++++++++++++++++ 020-valid-parentheses/step1.py | 29 +++++++++++++++++++++++++++++ 020-valid-parentheses/step2.py | 8 ++++++++ 020-valid-parentheses/step3.py | 15 +++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 020-valid-parentheses/memo.md create mode 100644 020-valid-parentheses/step1.py create mode 100644 020-valid-parentheses/step2.py create mode 100644 020-valid-parentheses/step3.py diff --git a/020-valid-parentheses/memo.md b/020-valid-parentheses/memo.md new file mode 100644 index 0000000..d33e94a --- /dev/null +++ b/020-valid-parentheses/memo.md @@ -0,0 +1,22 @@ +## step1で考えたこと + +それぞれの括弧に対応するものを取り除いていき、最後に残っていればFalse, そうでないならTrue + +=> whileの終了条件をどうするか? +無限ループになってしまう +さらにStep1のコードだと本来FalseであるべきものもTrueになってしまう + +カッコを分けて取り除くのではなく、セットで取り除けば良いのでは?? + +## step2 +コードは適切に求められたことを処理できているがRuntimeが若干遅いのが気になる +Time Complexity: O(n^2). +Space Complexity: O(n). +=> Brute Force + + +もっと早くするにはどうすべきか +- whileを使わないで書く +=> Using stack + +## step3 diff --git a/020-valid-parentheses/step1.py b/020-valid-parentheses/step1.py new file mode 100644 index 0000000..4c4e947 --- /dev/null +++ b/020-valid-parentheses/step1.py @@ -0,0 +1,29 @@ +# 動かないコード + +class Solution: + def isValid(self, s: str) -> bool: + while s: + if "(" in s: + s.pop("(") + s.pop(")") + elif "[" in s: + s.pop("[") + s.pop("]") + elif "{" in s: + s.pop("{") + s.pop("}") + + return True + +class Solution: + def isValid(self, s: str) -> bool: + while s: + if "()" in s: + s = s.replace("()", "") + elif "[]" in s: + s = s.replace("[]", "") + elif "{}" in s: + s = s.replace("{}", "") + + if s: return True + else: return False diff --git a/020-valid-parentheses/step2.py b/020-valid-parentheses/step2.py new file mode 100644 index 0000000..cf40042 --- /dev/null +++ b/020-valid-parentheses/step2.py @@ -0,0 +1,8 @@ +# 以前見つけた回答を一度見てから自力で書いたコード +class Solution: + def isValid(self, s: str) -> bool: + while "()" in s or "[]" in s or "{}" in s: + s = s.replace("()", "") + s = s.replace("[]", "") + s = s.replace("{}", "") + return s == "" \ No newline at end of file diff --git a/020-valid-parentheses/step3.py b/020-valid-parentheses/step3.py new file mode 100644 index 0000000..b70e451 --- /dev/null +++ b/020-valid-parentheses/step3.py @@ -0,0 +1,15 @@ +## 別解 +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + closeToOpen = {")": "(", "]": "[", "}": "{"} + + for c in s: + if c in closeToOpen: + if stack and stack[-1] == closeToOpen[c]: + stack.pop() + else: + return False + else: + stack.append(c) + return True if not stack else False \ No newline at end of file From baadf67b4d978c86efdefce19151c9f6868c8954 Mon Sep 17 00:00:00 2001 From: Rytxxx Date: Tue, 6 Jan 2026 02:01:30 -0800 Subject: [PATCH 2/2] refactoring --- 020-valid-parentheses/step1.py | 3 ++- 020-valid-parentheses/step3.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/020-valid-parentheses/step1.py b/020-valid-parentheses/step1.py index 4c4e947..ef172e0 100644 --- a/020-valid-parentheses/step1.py +++ b/020-valid-parentheses/step1.py @@ -24,6 +24,7 @@ def isValid(self, s: str) -> bool: s = s.replace("[]", "") elif "{}" in s: s = s.replace("{}", "") - + + # return not s と書くのが簡潔でわかりやすい。 if s: return True else: return False diff --git a/020-valid-parentheses/step3.py b/020-valid-parentheses/step3.py index b70e451..5c275fa 100644 --- a/020-valid-parentheses/step3.py +++ b/020-valid-parentheses/step3.py @@ -2,11 +2,11 @@ class Solution: def isValid(self, s: str) -> bool: stack = [] - closeToOpen = {")": "(", "]": "[", "}": "{"} + open_to_close = {"()": ")", "[]": "]", "{": "}"} for c in s: - if c in closeToOpen: - if stack and stack[-1] == closeToOpen[c]: + if c in open_to_close: + if stack and stack[-1] == open_to_close[c]: stack.pop() else: return False