Problem Solving

45. Backspace String Compare

굥깡 2022. 12. 31. 20:49
728x90

https://leetcode.com/problems/backspace-string-compare/

 

Backspace String Compare - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

입력받은 두 개의 문자열에서 #을 backspace로 치환하여 문자열을 새로 만든 후 서로 일치하는지 비교하는 문제

class Solution:
    def backspaceCompare(self, s: str, t: str) -> bool:
        s1 = ""
        for i in s:
            if i != "#":
                s1 = s1 + i
            else:
                s1 = s1[:-1]
        t1 = ""
        for i in t:
            if i != "#":
                t1 = t1 + i
            else:
                t1 = t1[:-1]
        if s1 == t1:
            return True
        return False

파이썬의 최대 장점 중 하나인 문자열 파싱을 이용하면 쉽게 풀 수 있다.