Explain the use of a bit vector for determining if all characters are unique

前端 未结 12 1779
野性不改
野性不改 2020-12-04 04:23

I am confused about how a bit vector would work to do this (not too familiar with bit vectors). Here is the code given. Could someone please walk me through this?

         


        
12条回答
  •  有刺的猬
    2020-12-04 05:25

    I also assume that your example comes from the book Cracking The Code Interview and my answer is related to this context.

    In order to use this algorithm to solve the problem, we have to admit that we only are going to pass characters from a to z (lowercase).

    As there is only 26 letters and these are properly sorted in the encoding table we use, this guarantees us that all the potential differences str.charAt(i) - 'a' will be inferior to 32 (the size of the int variable checker).

    As explained by Snowbear, we are about to use the checker variable as an array of bits. Lets have an approach by example :

    Let's say str equals "test"

    • First pass (i = t)

    checker == 0 (00000000000000000000000000000000)

    In ASCII, val = str.charAt(i) - 'a' = 116 - 97 = 19
    What about 1 << val ?
    1          == 00000000000000000000000000000001
    1 << 19    == 00000000000010000000000000000000
    checker |= (1 << val) means checker = checker | (1 << val)
    so checker = 00000000000000000000000000000000 | 00000000000010000000000000000000
    checker == 524288 (00000000000010000000000000000000)
    
    • Second pass (i = e)

    checker == 524288 (00000000000010000000000000000000)

    val = 101 - 97 = 4
    1          == 00000000000000000000000000000001
    1 << 4     == 00000000000000000000000000010000
    checker |= (1 << val) 
    so checker = 00000000000010000000000000000000 | 00000000000000000000000000010000
    checker == 524304 (00000000000010000000000000010000)
    

    and so on.. until we find an already set bit in checker for a specific character via the condition

    (checker & (1 << val)) > 0
    

    Hope it helps

提交回复
热议问题