diff --git a/20_Valid_Parentheses/note.md b/20_Valid_Parentheses/note.md new file mode 100644 index 0000000..90d4872 --- /dev/null +++ b/20_Valid_Parentheses/note.md @@ -0,0 +1,32 @@ +# 20. Valid Parentheses + + + +## step1(まず通す) + +文字列を先頭から見ていって + +- 開きカッコ→スタックに積む +- とじカッコ→スタックが空じゃなくて、topが対応する開きカッコならそれをpopする。対応してなかったら即NG + +最後にスタックが空になってたらOK + +## step2(整形&他の人のコードを読む) + +- opens は文字列で書いてもよかったかも(文字数が少なくてタイプが楽だし見やすそう) +- コメント集: 「"[aiu](eo)" が入力としてきたときに、プログラムの挙動として好ましいのは何だと考えますか?」 + - →カッコに着目して妥当かを判断するプログラムであるのが良いか、あるいは要件によってはカッコ以外が入っていたらエラーを出して止まって欲しい、となるかもしれない。 + - 他の文字を許容して括弧に着目した妥当性を判断する場合は、opens の他に closes も用意して、どちらでもない場合はスルーするようにする、というのが良さそう + - そうすると、for で回す変数は parenthesis じゃなくて character とかになりそう +- スタックが空になってたら True, そうじゃないなら False → not stack を返すことで見た目がスッキリする + - + - 取り組んでいる間にいただいたコメントで教えていただいたスタイルガイドでも、list が空かどうかは "implicit" な boolean で書けと指定されている + - + +## 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/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 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 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 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