Easy to understand Python approach beats 95%


  • 1
    W

    This approach tries to compare the length of the two strings after replacing the same character with ''

    class Solution(object):
        def isAnagram(self, s, t):
    
            if len(s) != len(t)
                return False
    
            while len(s)>0:
                t = t.replace(s[0],'')
                s = s.replace(s[0],'')
                if len(s) != len(t):
                    return False
                
            return True
                
    

  • 0
    P

    Returns Truefor isAnagram('a', 'b'). It doesn't compare the length of the strings after the first while loop, since len(s) is already 0. You need to compare the lengths at the end of the loop.

    Apart from that, great solution with even better performance.


  • 0
    W

    @petroll Thanks, you are right. That's such a stupid mistake. :)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.