Python 99th percentile

  • 1

    This fails all cases immediately that don't have the same length or don't share the same letters. Follow on by looping through using the .count() approach to check if each has the same number of each type of letters, immediate failure if they don't match counts.

    def isAnagram(self, s, t):
            if len(s) != len(t) or len(list(set(s)^set(t))) != 0:
                return False
                for l in list(set(s)):
                    if s.count(l) != t.count(l):
                        return False
                    return True

Log in to reply

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