물음표 살인마의 개발블로그

알고리즘 문제/Leetcode

844. Backspace String Compare

BEstyle 2022. 12. 28. 00:48

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.

 

Example 1:

Input: s = "ab#c", t = "ad#c"
Output: true
Explanation: Both s and t become "ac".

Example 2:

Input: s = "ab##", t = "c#d#"
Output: true
Explanation: Both s and t become "".

Example 3:

Input: s = "a#c", t = "b"
Output: false
Explanation: s becomes "c" while t becomes "b".

 

Constraints:

  • 1 <= s.length, t.length <= 200
  • s and t only contain lowercase letters and '#' characters.

 

Follow up: Can you solve it in O(n) time and O(1) space?

 


class Solution:
    def backspaceCompare(self, s: str, t: str) -> bool:
        def delsha(s):
            while "#" in s:
                idx=s.index("#")
                if idx!=0:
                    s=s[:idx-1]+s[idx+1:]
                else:
                    s=s[1:]
                print(s)
            return s
        
        s=delsha(s)
        t=delsha(t)
        return s==t

'알고리즘 문제 > Leetcode' 카테고리의 다른 글

20. Valid Parentheses  (0) 2022.12.28
682. Baseball Game  (0) 2022.12.28
3. Longest Substring Without Repeating Characters  (0) 2022.12.28
283. Move Zeroes  (0) 2022.12.28
88. Merge Sorted Array  (0) 2022.12.27