Skip to content

Create 844. Backspace String Compare.md#21

Open
Kitaken0107 wants to merge 2 commits intomainfrom
Kitaken0107-patch-23
Open

Create 844. Backspace String Compare.md#21
Kitaken0107 wants to merge 2 commits intomainfrom
Kitaken0107-patch-23

Conversation

@Kitaken0107
Copy link
Owner

```python
class Solution:
def backspaceCompare(self, s: str, t: str) -> bool:
s_stack = []
Copy link

Choose a reason for hiding this comment

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

同じ処理が 2 回登場しています。関数化するとシンプルになると思います。

t_stack.pop()


if len(s_stack) != len(t_stack):
Copy link

Choose a reason for hiding this comment

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

list の == は内容まで比較しますので、 return s_stack == t_stack でよいと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

こちら知らなかったです。発見でした。
ありがとうございます。

loop_cnt -= 1

return True

Copy link

Choose a reason for hiding this comment

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

末尾の空行は削除しましょう。

t_stack = []

for char in s:
if char != '#':
Copy link

Choose a reason for hiding this comment

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

char は言語によっては予約語なので、私は避けますが趣味の問題です。

同じ処理は二度書かずに関数にしましょう。

if 文字が '#' でないとき:
elif : # すなわち '#' なとき

と二重否定にしているのひねくれていませんか。

if char == '#':
  if s_stack:
    s_stack.pop()
  continue
s_stack.append(char)

のほうが素直でしょう。

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかに自分のコード、二重否定なのと無駄に否定系使ってるのが読みづらかったです。
頂いたコード読みやすかったです。
continueいまだに使い慣れてないので、論理の構造の幅のところを見直してみます。

t_stack.pop()


if len(s_stack) != len(t_stack):
Copy link

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants