From be22d6ba8809afb3103c04c7205b5d8d1f344c24 Mon Sep 17 00:00:00 2001 From: mayota Date: Sat, 6 Jun 2026 16:02:54 +0900 Subject: [PATCH 1/4] step1 --- 20_Valid_Parentheses/note.md | 16 ++++++++++++++++ 20_Valid_Parentheses/step1.py | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 20_Valid_Parentheses/note.md create mode 100644 20_Valid_Parentheses/step1.py diff --git a/20_Valid_Parentheses/note.md b/20_Valid_Parentheses/note.md new file mode 100644 index 0000000..cabb897 --- /dev/null +++ b/20_Valid_Parentheses/note.md @@ -0,0 +1,16 @@ +# 20. Valid Parentheses + + + +## step1(まず通す) + +文字列を先頭から見ていって + +- 開きカッコ→スタックに積む +- とじカッコ→スタックが空じゃなくて、topが対応する開きカッコならそれをpopする。対応してなかったら即NG + +最後にスタックが空になってたらOK + +## step2(整形&他の人のコードを読む) + +## step3(10分以内にさっとかける * 3回) diff --git a/20_Valid_Parentheses/step1.py b/20_Valid_Parentheses/step1.py new file mode 100644 index 0000000..8a96c11 --- /dev/null +++ b/20_Valid_Parentheses/step1.py @@ -0,0 +1,20 @@ +class Solution: + def isValid(self, s: str) -> bool: + opens = ["(", "[", "{"] + close_to_open = {")": "(", "]": "[", "}": "{"} + stack = [] + for parenthesis in s: + if parenthesis in opens: + stack.append(parenthesis) + continue + + if stack and stack[-1] == close_to_open[parenthesis]: + stack.pop() + continue + + return False + + if len(stack) != 0: + return False + + return True From ad5bfc0881df1019a3b13e6d4d094d8ce00ae5c8 Mon Sep 17 00:00:00 2001 From: mayota Date: Mon, 8 Jun 2026 21:32:46 +0900 Subject: [PATCH 2/4] step2 --- 20_Valid_Parentheses/note.md | 10 ++++++++++ 20_Valid_Parentheses/step2.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 20_Valid_Parentheses/step2.py diff --git a/20_Valid_Parentheses/note.md b/20_Valid_Parentheses/note.md index cabb897..8d1646f 100644 --- a/20_Valid_Parentheses/note.md +++ b/20_Valid_Parentheses/note.md @@ -13,4 +13,14 @@ ## step2(整形&他の人のコードを読む) +- opens は文字列で書いてもよかったかも(文字数が少なくてタイプが楽だし見やすそう) +- コメント集: 「"[aiu](eo)" が入力としてきたときに、プログラムの挙動として好ましいのは何だと考えますか?」 + - →カッコに着目して妥当かを判断するプログラムであるのが良いか、あるいは要件によってはカッコ以外が入っていたらエラーを出して止まって欲しい、となるかもしれない。 + - 他の文字を許容して括弧に着目した妥当性を判断する場合は、opens の他に closes も用意して、どちらでもない場合はスルーするようにする、というのが良さそう + - そうすると、for で回す変数は parenthesis じゃなくて character とかになりそう +- スタックが空になってたら True, そうじゃないなら False → not stack を返すことで見た目がスッキリする + - + - 取り組んでいる間にいただいたコメントで教えていただいたスタイルガイドでも、list が空かどうかは "implicit" な boolean で書けと指定されている + - + ## step3(10分以内にさっとかける * 3回) diff --git a/20_Valid_Parentheses/step2.py b/20_Valid_Parentheses/step2.py new file mode 100644 index 0000000..fef08fb --- /dev/null +++ b/20_Valid_Parentheses/step2.py @@ -0,0 +1,18 @@ +class Solution: + def isValid(self, s: str) -> bool: + opens = "([{" + closes = ")]}" + close_to_open = {")": "(", "]": "[", "}": "{"} + stack = [] + for character in s: + if character in opens: + stack.append(character) + continue + + if character in closes: + if stack and stack[-1] == close_to_open[character]: + stack.pop() + else: + return False + + return not stack From 92c5783794855e4fa5583ae3cd6a95efd2101959 Mon Sep 17 00:00:00 2001 From: mayota Date: Mon, 8 Jun 2026 21:33:03 +0900 Subject: [PATCH 3/4] step3 --- 20_Valid_Parentheses/note.md | 2 ++ 20_Valid_Parentheses/step3.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 20_Valid_Parentheses/step3.py diff --git a/20_Valid_Parentheses/note.md b/20_Valid_Parentheses/note.md index 8d1646f..081cde2 100644 --- a/20_Valid_Parentheses/note.md +++ b/20_Valid_Parentheses/note.md @@ -24,3 +24,5 @@ - ## step3(10分以内にさっとかける * 3回) + +書いていて気づいたが、step2.py だと括弧の種類が増減した時に opens/closes/close_to_open の3つを書き換えないといけないので、opens/closes を close_to_open.values() close_to_open.keys() にした方がいいかも。ただしその場合でも、読み書きする時に .keys(), .values() だとどっちがどっちか混乱するので、変数に格納しといた方が読みやすそう。 diff --git a/20_Valid_Parentheses/step3.py b/20_Valid_Parentheses/step3.py new file mode 100644 index 0000000..2728e10 --- /dev/null +++ b/20_Valid_Parentheses/step3.py @@ -0,0 +1,18 @@ +class Solution: + def isValid(self, s: str) -> bool: + close_to_open = {")": "(", "]": "[", "}": "{"} + closes = close_to_open.keys() + opens = close_to_open.values() + stack = [] + for character in s: + if character in opens: + stack.append(character) + continue + + if character in closes: + if stack and stack[-1] == close_to_open[character]: + stack.pop() + else: + return False + + return not stack From 171e0aaa1dfa82ca670637599c62c4308e77c00a Mon Sep 17 00:00:00 2001 From: mayota Date: Thu, 11 Jun 2026 18:22:23 +0900 Subject: [PATCH 4/4] =?UTF-8?q?step4=20=E3=83=AC=E3=83=93=E3=83=A5?= =?UTF-8?q?=E3=83=BC=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E3=81=AB=E6=B2=BF?= =?UTF-8?q?=E3=81=A3=E3=81=A6=E6=9B=B8=E3=81=8D=E7=9B=B4=E3=81=97=E3=81=A6?= =?UTF-8?q?=E3=81=BF=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20_Valid_Parentheses/note.md | 4 ++++ 20_Valid_Parentheses/step4.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 20_Valid_Parentheses/step4.py diff --git a/20_Valid_Parentheses/note.md b/20_Valid_Parentheses/note.md index 081cde2..90d4872 100644 --- a/20_Valid_Parentheses/note.md +++ b/20_Valid_Parentheses/note.md @@ -26,3 +26,7 @@ ## step3(10分以内にさっとかける * 3回) 書いていて気づいたが、step2.py だと括弧の種類が増減した時に opens/closes/close_to_open の3つを書き換えないといけないので、opens/closes を close_to_open.values() close_to_open.keys() にした方がいいかも。ただしその場合でも、読み書きする時に .keys(), .values() だとどっちがどっちか混乱するので、変数に格納しといた方が読みやすそう。 + +## step4 + +いただいたコメントに沿って書き直してみる diff --git a/20_Valid_Parentheses/step4.py b/20_Valid_Parentheses/step4.py new file mode 100644 index 0000000..8d79b4f --- /dev/null +++ b/20_Valid_Parentheses/step4.py @@ -0,0 +1,22 @@ +class Solution: + def isValid(self, s: str) -> bool: + open_to_close = {"(": ")", "[": "]", "{": "}"} + open_brackets = [] + for character in s: + if ( + character not in open_to_close.keys() + and character not in open_to_close.values() + ): + continue + + if character in open_to_close.keys(): + open_brackets.append(character) + continue + + if open_brackets and character == open_to_close[open_brackets[-1]]: + open_brackets.pop() + continue + + return False + + return not open_brackets