Backspace String Compare (Leetcode844)

Using stacks

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

TC: O(N) SC: O(N)

class Solution:
    def backspaceCompare(self, s: str, t: str) -> bool:
        
        stack1=[]
        for i in range(len(s)):
            if(s[i]=='#'):
                if(len(stack1)>0):
                    stack1.pop()
            else:
                stack1.append(s[i])
                
        stack2=[]
        for i in range(len(t)):
            if(t[i]=='#'):
                if(len(stack2)>0):
                    stack2.pop()
            else:
                stack2.append(t[i])
        
        if(stack1==stack2):
            return True
        else:
            return False      

Last updated