Skip to content

20. Valid Parentheses.md #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions 20. Valid Parentheses.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
20. Valid Parentheses

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コードの書き忘れでしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

まだ完成していないので終わり次第レビュー依頼に投げさせていただきます。
その際はよろしくおねがいいたします

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

失礼しました!!

思考順に書いていく
引用文は「」

step1 方針立たず不正解
```python
class Solution:
def isValid(self, s: str) -> bool:
if s[0] == "(":
for i in range(1,len(s)):
if s[i] == ")":
return True
if s[0] == "{":
for i in range(1,len(s)):
if s[i] == "}":
return True
if s[0] == "[":
for i in range(1,len(s)):
if s[i] == "]":
return True
return False
```
まあ不正解になるとは最初から感じていたが一応不正解のコードを書き残しておく。
stackは知っていたがどうやって、それを使って解くのかわからなかった。
また、pythonでスタックをどう実装するのかもわかっていなかった。

https://github.com/yus-yus/leetcode/pull/6
かなり勉強になった。
特に問題文にはないが、brackets以外が入力として与えられた場合も考えているのがすごくためになった。
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは凄く大切ですね。
この練習では動くかどうかはそれほど関係ないという話があったと思いますが、実際コード書く時は動くのはスタートラインで、こういう本来の機能以外で考える事の方が多かったりはします。

以下のコードを参考に書くことにする。
また余力があればdequeの解法も真似する。

```Python
class Solution:
def isValid(self, s: str) -> bool:
valid_input = ['(', ')', '{', '}', '[', ']']
open_to_close = {'(':')', '{':'}', '[':']'}
stack = []

for char in s:
# 括弧以外の入力が来た場合の処理 無視する
if not char in valid_input:
continue
if char in open_to_close:
stack.append(char)
continue
if not stack:
return False
if char != open_to_close[stack.pop()]:
return False

return not stack
```
step2
```python
class Solution:
def isValid(self, s: str) -> bool:
valied_input = ["(",")","{","}","[","]"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valid_input = "(){}[]"
というのでも in で判定できます。

open_to_close = {"(":")","{":"}","[":"]"}
stack = []

for char in s:
#かっこ以外の入力が来た場合は無視する処理
if not char in valied_input:
continue
if char in open_to_close:
stack.append(char)
continue
if not stack:
return False
if char != open_to_close[stack.pop()]:
return False
return not stack
```
この解法が書きやすかったから、これでstep3いく。

かっこの英語名が気になったので調べた。米語違いすぎだろ
() 米語はparentheses 英語はbrackets
[] square brackets
{} curly brackets
<> angle brackets
(https://www.rarejob.com/englishlab/column/20210928_02/)

「valid parentheses は、チョムスキー階層、タイプ-2、文脈自由文法だから、プッシュダウンオートマトンで書ける、を多分連想します。」by(keita odaさん)
(https://discord.com/channels/1084280443945353267/1201211204547383386/1202541275115425822)
上の用語わからなすぎて調べる。
おそらく形式言語理論の概念を用いてこの問題を解釈している?
うちの学校のカリキュラムにはないが、形式言語理論の勉強はSWEとして必須?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多くの人が一応知っている感じはありますが、あまり聞かれないやつです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうだったんですね。余力があれば勉強してみます!


step3
1回目4分41
2回目3分38
3回目2分48

```python
class Solution:
def isValid(self, s: str) -> bool:
valid_input = ["(",")","{","}","[","]"]
open_to_close = {"(":")","{":"}","[":"]"}
stack = []

for char in s:
#かっこ以外がきたら無視する処理
if not char in valid_input:
continue
if char in open_to_close:
stack.append(char)
continue
if not stack:
return False
if char != open_to_close[stack.pop()]:
return False
return not stack
```