diff --git a/arai60/valid-parentheses/README.md b/arai60/valid-parentheses/README.md new file mode 100644 index 0000000..7009261 --- /dev/null +++ b/arai60/valid-parentheses/README.md @@ -0,0 +1,33 @@ +## 考察 +- 過去に解いたことあり +- 方針 + - stackを使って対応を見ていく + - 前から文字を見ていって、open bracketが来たらstackに積む + - close bracketが来たら、stackから要素をpopして対応をチェックする(stackが空じゃないかだけ注意) + - 最後にstackが空になっていたらOK + - time: O(n), space: O(n) +- あとは実装 + +## Step1 +- 上のアルゴリズムを実装 +- time: O(n), space: O(n) + +## Step2 +- 手続き的にbracketの対応関係を判定していた箇所を、辞書を使って宣言的な記述に変更 + - 結果、余計なif文がかなり減った + +## Step3 +- 1回目: 1m51s +- 2回目: 1m53s +- 3回目: 1m28s + +## Step4 +- レビューを元にコードを修正 +- 変数名の見直し + - stack -> bracket_stack + - 何を入れるstackなのかを明確に + - bracket_pairs -> open_to_close + - 例えば、open_to_close[open_bracket] という記述を見たときにclose_bracketが返ってくることが一目でわかる + - より具体的になって、わかりやすくなった感覚 +- `if not open_to_close[open_bracket] == c:` と書いていたところを `if open_to_close[open_bracket] != c:`に +- early continueしてネストを浅く diff --git a/arai60/valid-parentheses/step1.py b/arai60/valid-parentheses/step1.py new file mode 100644 index 0000000..b99671b --- /dev/null +++ b/arai60/valid-parentheses/step1.py @@ -0,0 +1,22 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + for c in s: + if c in ['(', '{', '[']: + stack.append(c) + else: + if not stack: + return False + + open_bracket = stack.pop() + if c == ')': + if not open_bracket == '(': + return False + elif c == '}': + if not open_bracket == '{': + return False + else: + if not open_bracket == '[': + return False + + return not stack diff --git a/arai60/valid-parentheses/step2.py b/arai60/valid-parentheses/step2.py new file mode 100644 index 0000000..e5787a1 --- /dev/null +++ b/arai60/valid-parentheses/step2.py @@ -0,0 +1,20 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + bracket_pairs = { + '(': ')', + '{': '}', + '[': ']', + } + for c in s: + if c in bracket_pairs: + stack.append(c) + else: + if not stack: + return False + + open_bracket = stack.pop() + if not bracket_pairs[open_bracket] == c: + return False + + return not stack diff --git a/arai60/valid-parentheses/step3.py b/arai60/valid-parentheses/step3.py new file mode 100644 index 0000000..e5787a1 --- /dev/null +++ b/arai60/valid-parentheses/step3.py @@ -0,0 +1,20 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + bracket_pairs = { + '(': ')', + '{': '}', + '[': ']', + } + for c in s: + if c in bracket_pairs: + stack.append(c) + else: + if not stack: + return False + + open_bracket = stack.pop() + if not bracket_pairs[open_bracket] == c: + return False + + return not stack diff --git a/arai60/valid-parentheses/step4.py b/arai60/valid-parentheses/step4.py new file mode 100644 index 0000000..4fa6a73 --- /dev/null +++ b/arai60/valid-parentheses/step4.py @@ -0,0 +1,21 @@ +class Solution: + def isValid(self, s: str) -> bool: + bracket_stack = [] + open_to_close = { + '(': ')', + '{': '}', + '[': ']', + } + for c in s: + if c in open_to_close: + bracket_stack.append(c) + continue + + if not bracket_stack: + return False + + open_bracket = bracket_stack.pop() + if open_to_close[open_bracket] != c: + return False + + return not bracket_stack