Python 11 lines dict+set, beats 95%+


  • 0
    S
            char_maps, targets = {}, set()
            for c1, c2 in zip(s, t):
                if c1 in char_maps:
                    # c1 is already mapped to a target character, 
                    # which must be c2 if s and t are isomorphic
                    if char_maps[c1] != c2:
                        return False
                else:
                    # c1 must be mapped to c2 (and c2 must be a unique target)
                    if c2 in targets:
                        return False
                    char_maps[c1] = c2
                    targets.add(c2)
            return True
    

Log in to reply
 

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