X. Backspace String Compare

Easy

Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.

😇 Solution

class Solution:
    def backspaceCompare(self, S: str, T: str) -> bool:
        
        temp_s = ''
        temp_t = ''
        
        for i,s in enumerate(S):
            if S[i] == '#':
                temp_s = temp_s[:-1]
            else:
                temp_s += s
                
        for j,t in enumerate(T):
            if T[j] == '#':
                temp_t = temp_t[:-1]
            else:
                temp_t += t

        return temp_s == temp_t

Last updated

Was this helpful?