Loading...

10-19 leetcode 0844

链接 844. Backspace String Compare

题目

Given two strings s and t, return true if they are equal when both are typed into empty text editors. ‘#’ means a backspace character.

Note that after backspacing an empty text, the text will continue empty.

题解

没啥好说的,直接栈的使用(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
def backspaceCompare(self, s: str, t: str) -> bool:
stack_s = []
stack_t = []
for i in s:
if i == "#":
if stack_s:
stack_s.pop()
else:
stack_s.append(i)
for i in t:
if i == "#":
if stack_t:
stack_t.pop()
else:
stack_t.append(i)
return stack_s == stack_t

Comment