Backspace String Compare (Leetcode 844)

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

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

class Solution:
    def backspaceCompare(self, s: str, t: str) -> bool:
        # Traverse from the back and
        # keep the count of #
        def func(string):
            
            m=len(string)
            c=0
            result=""
            for i in range(m-1,-1,-1):
                if(string[i]=='#'):
                    c+=1
                else:
                    if(c>0):
                        c-=1
                    elif(c==0):
                        result=result+string[i]
            return result
        
        if(func(s)==func(t)):
            return True
        else:
            return False
                        
                    
                        
                
            
        

Last updated