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?
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"
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)
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