-
Notifications
You must be signed in to change notification settings - Fork 0
Create 844. Backspace String Compare.md #21
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| # 844. Backspace String Compare | ||
| https://leetcode.com/problems/backspace-string-compare/ | ||
|
|
||
| # 1st | ||
| #があると一つ前の文字を消去、最終的に残ったものを比較ということで、 | ||
| 最後に入れたものを削除しやすいスタックを選択 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def backspaceCompare(self, s: str, t: str) -> bool: | ||
| s_stack = [] | ||
| t_stack = [] | ||
|
|
||
| for s_1 in s: | ||
| if s_1 != '#': | ||
| s_stack.append(s_1) | ||
| elif len(s_stack) != 0: | ||
| s_stack.pop() | ||
| for t_1 in t: | ||
| if t_1 != '#': | ||
| t_stack.append(t_1) | ||
| elif len(t_stack) != 0: | ||
| t_stack.pop() | ||
|
|
||
|
|
||
| if len(s_stack) != len(t_stack): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. list の == は内容まで比較しますので、
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちら知らなかったです。発見でした。 |
||
| return False | ||
|
|
||
| loop_cnt = len(s_stack) | ||
| while loop_cnt > 0: | ||
| if s_stack.pop() != t_stack.pop(): | ||
| return False | ||
| loop_cnt -= 1 | ||
|
|
||
| return True | ||
| ``` | ||
|
|
||
| # 2nd・3rd | ||
| s1・t1をcharに変更しました | ||
| ```python | ||
| class Solution: | ||
| def backspaceCompare(self, s: str, t: str) -> bool: | ||
| s_stack = [] | ||
| t_stack = [] | ||
|
|
||
| for char in s: | ||
| if char != '#': | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. char は言語によっては予約語なので、私は避けますが趣味の問題です。 同じ処理は二度書かずに関数にしましょう。 if 文字が '#' でないとき: と二重否定にしているのひねくれていませんか。 のほうが素直でしょう。
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. たしかに自分のコード、二重否定なのと無駄に否定系使ってるのが読みづらかったです。 |
||
| s_stack.append(char) | ||
| elif len(s_stack) != 0: | ||
| s_stack.pop() | ||
| for char in t: | ||
| if char != '#': | ||
| t_stack.append(char) | ||
| elif len(t_stack) != 0: | ||
| t_stack.pop() | ||
|
|
||
|
|
||
| if len(s_stack) != len(t_stack): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return False | ||
|
|
||
| loop_cnt = len(s_stack) | ||
| while loop_cnt > 0: | ||
| if s_stack.pop() != t_stack.pop(): | ||
| return False | ||
| loop_cnt -= 1 | ||
|
|
||
| return True | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 末尾の空行は削除しましょう。 |
||
|
|
||
| ``` | ||
|
|
||
| # 4th レビューを基に再構築 | ||
| ```python | ||
| class Solution: | ||
| def backspaceCompare(self, s: str, t: str) -> bool: | ||
| def backspace_string(x): | ||
| x_stack = [] | ||
| for c in x: | ||
| if c == '#': | ||
| if x_stack: | ||
| x_stack.pop() | ||
| continue | ||
| x_stack.append(c) | ||
| return x_stack | ||
|
|
||
| return backspace_string(s) == backspace_string(t) | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同じ処理が 2 回登場しています。関数化するとシンプルになると思います。