From 91de100b1bc1ebe23ec7089570ed53fcbe12ad51 Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Fri, 5 Sep 2025 21:56:32 +0900 Subject: [PATCH 1/9] Implement valid parentheses checker --- Valid Parentheses/step1.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Valid Parentheses/step1.py diff --git a/Valid Parentheses/step1.py b/Valid Parentheses/step1.py new file mode 100644 index 0000000..3db4a6e --- /dev/null +++ b/Valid Parentheses/step1.py @@ -0,0 +1,17 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + bracket_pairs = { ")": "(", "]": "[", "}": "{"} + + for char in s: + if char in bracket_pairs: + if len(stack) > 0 and stack[-1] == bracket_pairs[char]: + stack.pop() + else: + return False + else: + stack.append(char) + + return len(stack) == 0 + + From 3fbad1928646d27f932dd9bbf4e52ac05bf3f1e4 Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Mon, 8 Sep 2025 00:09:55 +0900 Subject: [PATCH 2/9] Create step2-using-stack.py --- Valid Parentheses/step2-using-stack.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Valid Parentheses/step2-using-stack.py diff --git a/Valid Parentheses/step2-using-stack.py b/Valid Parentheses/step2-using-stack.py new file mode 100644 index 0000000..fc4cc81 --- /dev/null +++ b/Valid Parentheses/step2-using-stack.py @@ -0,0 +1,15 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + bracket_pairs = { ")": "(", "]": "[", "}": "{"} + + for char in s: + if char in bracket_pairs: + if len(stack) > 0 and stack[-1] == bracket_pairs[char]: + stack.pop() + else: + return False + else: + stack.append(char) + + return len(stack) == 0 From 51b1d98921e37f3a1fca2faaf2e7f34f5b4281fe Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Mon, 8 Sep 2025 00:12:43 +0900 Subject: [PATCH 3/9] Add brute force method to check valid parentheses --- Valid Parentheses/step2-brute-force.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Valid Parentheses/step2-brute-force.py diff --git a/Valid Parentheses/step2-brute-force.py b/Valid Parentheses/step2-brute-force.py new file mode 100644 index 0000000..aec7783 --- /dev/null +++ b/Valid Parentheses/step2-brute-force.py @@ -0,0 +1,9 @@ +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 == "" + From 9beaf9b12397a330e23e82ff3a8f3e488356b9fb Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Mon, 8 Sep 2025 23:40:33 +0900 Subject: [PATCH 4/9] Create step3-brute-force.py --- Valid Parentheses/step3-brute-force.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Valid Parentheses/step3-brute-force.py diff --git a/Valid Parentheses/step3-brute-force.py b/Valid Parentheses/step3-brute-force.py new file mode 100644 index 0000000..fe1340c --- /dev/null +++ b/Valid Parentheses/step3-brute-force.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 == "" From 0855e06d2a657bb32305b6d88579b0f7fc901497 Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Mon, 8 Sep 2025 23:48:16 +0900 Subject: [PATCH 5/9] Create step3-using-stack.py --- Valid Parentheses/step3-using-stack.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Valid Parentheses/step3-using-stack.py diff --git a/Valid Parentheses/step3-using-stack.py b/Valid Parentheses/step3-using-stack.py new file mode 100644 index 0000000..4853a15 --- /dev/null +++ b/Valid Parentheses/step3-using-stack.py @@ -0,0 +1,16 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + bracket_pairs = { "}": "{", "]": "[", ")": "("} + + for char in s: + if char in bracket_pairs: + if len(stack) > 0 and stack[-1] == bracket_pairs[char]: + stack.pop() + else: + return False + else: + stack.append(char) + + return stack == [] + From 379f1146c7f376f0a5e97702b35bb015d623b854 Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Mon, 8 Sep 2025 23:52:08 +0900 Subject: [PATCH 6/9] Create memo.md with problem-solving steps --- Valid Parentheses/memo.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Valid Parentheses/memo.md diff --git a/Valid Parentheses/memo.md b/Valid Parentheses/memo.md new file mode 100644 index 0000000..13f9575 --- /dev/null +++ b/Valid Parentheses/memo.md @@ -0,0 +1,14 @@ +# step1 + +どこかで同じ問題をやっていた? +記憶があって stack に積まれた最後の閉じ括弧を対応するペアと比較する方法がすっと出てきた。 + +# step2 + +せっかくなのでナイーブな方法を考えてみた。 +全部 replace すればいいかという安易な考えでコーディングしてみた。 +Brute Force で解いてみても計算量は O(N) なので悪くはないかなとは思った。(正規表現でマッチングしているのでもちろん計算量がアルゴリズムの選択肢になるとは思っていないです。) + +## step3 + +同じようにコーディングをした。step1から変わっていない。改善を考えるのが苦手かもしれない。 From addc28c9a4abc45fd1c1c1277d47193a16b827fc Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Wed, 15 Oct 2025 23:36:19 +0900 Subject: [PATCH 7/9] Create step4-using-stack.py --- Valid Parentheses/step4-using-stack.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Valid Parentheses/step4-using-stack.py diff --git a/Valid Parentheses/step4-using-stack.py b/Valid Parentheses/step4-using-stack.py new file mode 100644 index 0000000..2599de7 --- /dev/null +++ b/Valid Parentheses/step4-using-stack.py @@ -0,0 +1,12 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + opening_to_closing = { "(": ")", "{": "}", "[": "]"} + for bracket in s: + if bracket in opening_to_closing: + stack.append(opening_to_closing[bracket]) + continue + else: + if not stack or bracket != stack.pop(): + return False + return not stack From c865bcad5c479e59f07c2172feea06c0a43f4c65 Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Thu, 16 Oct 2025 22:12:52 +0900 Subject: [PATCH 8/9] Create step4-using-bruteforce.py --- Valid Parentheses/step4-using-bruteforce.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Valid Parentheses/step4-using-bruteforce.py diff --git a/Valid Parentheses/step4-using-bruteforce.py b/Valid Parentheses/step4-using-bruteforce.py new file mode 100644 index 0000000..67f7a32 --- /dev/null +++ b/Valid Parentheses/step4-using-bruteforce.py @@ -0,0 +1,7 @@ +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 not s From 6c2940c06d1e5288f7a8c5917d9b53611403613e Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Thu, 16 Oct 2025 22:26:53 +0900 Subject: [PATCH 9/9] Update memo.md --- Valid Parentheses/memo.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Valid Parentheses/memo.md b/Valid Parentheses/memo.md index 13f9575..e2c9382 100644 --- a/Valid Parentheses/memo.md +++ b/Valid Parentheses/memo.md @@ -7,7 +7,9 @@ せっかくなのでナイーブな方法を考えてみた。 全部 replace すればいいかという安易な考えでコーディングしてみた。 -Brute Force で解いてみても計算量は O(N) なので悪くはないかなとは思った。(正規表現でマッチングしているのでもちろん計算量がアルゴリズムの選択肢になるとは思っていないです。) +~~Brute Force で解いてみても計算量は O(N) なので悪くはないかなとは思った。~~(正規表現でマッチングしているのでもちろん計算量がアルゴリズムの選択肢になるとは思っていないです。) + +追記: 時間計算量は、最悪で O(N) の処理が O(N/2) 回ループするので、 O(N^2)になりそうと指摘いただきました。 ## step3