Skip to content
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
87 changes: 87 additions & 0 deletions 844. Backspace String Compare.md
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 = []
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 = []

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):
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.

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

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 != '#':
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いまだに使い慣れてないので、論理の構造の幅のところを見直してみます。

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):
Copy link

Choose a reason for hiding this comment

The 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

Copy link

Choose a reason for hiding this comment

The 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)
```