Is there some problem using the hash with xor?


  • 0
    H

    i just xor the hash of character. it pass, but i think it may have some collision.

    class Solution(object):
        def findAnagrams(self, s, p)
            if not s:
                return []
            n = len(p)
            hp = reduce(lambda a,b:a^b, iter(hash(c) for c in p))
            hs = [hash(c) for c in s]
            hash_s = reduce(lambda a,b:a^b, hs[:n])
            result = []
            for i in range(len(s)-n):
                if hash_s == hp:
                    result.append(i)
                hash_s = hash_s ^ hs[i] ^ hs[i+n]
            if hash_s == hp:
                result.append(len(s)-n)
            # print(hp)
            # print(s[9:9+n], p)
            return result
    

  • 0

    Yes, fails for example s = "aa", p = "bb".

    @1337c0d3r can you add it?


  • 1

    @StefanPochmann Done, just added this test case.


Log in to reply
 

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