844. Backspace String Compare
https://leetcode.com/problems/backspace-string-compare/
Python
class Solution:
def backspaceCompare(self, s: str, t: str) -> bool:
return self.convert(s) == self.convert(t)
@staticmethod
def convert(s: str):
stack = []
for char in s:
if char == '#':
if stack:
stack.pop()
continue
stack.append(char)
return ''.join(stack)
Go
func backspaceCompare(s string, t string) bool {
return convert(s) == convert(t)
}
func convert(s string) string {
stack := []byte{}
for i := range s {
if s[i] == '#' {
if len(stack) > 0 {
stack = stack[:len(stack)-1]
}
} else {
stack = append(stack, s[i])
}
}
return string(stack)
}