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

前端 未结 12 1781
野性不改
野性不改 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:12

    There are couple of excellent answers already provided above. So I don't want to repeat what's everything already said. But did want to add couple of things to help with the above program as I just worked through the same program and had couple of questions but after spending some time, I have more clarity on this program.

    First of all "checker" is used to track the character which is already traversed in the String in order to see if there are any characters are getting repeated.

    Now "checker" is an int data type so it can only have 32 bits or 4 bytes (depending upon platform) so this program can only work correctly for a character set within a range of 32 characters. That's the reason, this program subtracts 'a' from each character in order to make this program run for only lower case characters. However if you mix lower and upper case characters then it would not work.

    By the way, if you do not subtract 'a' from each character (see below statement) then this program will work correctly for only String with upper case characters or String with only lower case characters. So the scope of above program increases from just lower case characters to upper case characters too but they can't be mixed together.

    int val = str.charAt(i) - 'a'; 
    

    However I wanted to write a generic program using Bitwise Operation which should work for any ASCII characters without worrying about upper case, lower case, numbers or any special character. In order to do this, our "checker" should be large enough to store 256 characters (ASCII Character Set size). But an int in Java would not work as it can only store 32 bits. Hence in below program, I am using BitSet class available in JDK which can have any user defined size passed while instantiating a BitSet object.

    Here is a program which does the same thing as above program written using Bitwise operator but this program will work for a String with any character from ASCII character set.

    public static boolean isUniqueStringUsingBitVectorClass(String s) {
    
        final int ASCII_CHARACTER_SET_SIZE = 256;
    
        final BitSet tracker = new BitSet(ASCII_CHARACTER_SET_SIZE);
    
        // if more than  256 ASCII characters then there can't be unique characters
        if(s.length() > 256) {
            return false;
        }
    
        //this will be used to keep the location of each character in String
        final BitSet charBitLocation = new BitSet(ASCII_CHARACTER_SET_SIZE);
    
        for(int i = 0; i < s.length(); i++) {
    
            int charVal = s.charAt(i);
            charBitLocation.set(charVal); //set the char location in BitSet
    
            //check if tracker has already bit set with the bit present in charBitLocation
            if(tracker.intersects(charBitLocation)) {
                return false;
            }
    
            //set the tracker with new bit from charBitLocation
            tracker.or(charBitLocation);
    
            charBitLocation.clear(); //clear charBitLocation to store bit for character in the next iteration of the loop
    
        }
    
        return true;
    
    }
    
    0 讨论(0)
  • 2020-12-04 05:13

    I have a sneaking suspicion you got this code from the same book I'm reading...The code itself here isn't nearly as cryptic as the the operators- |=, &, and << which aren't normally used by us layman- the author didn't bother taking the extra time out in explaining the process nor what the actual mechanics involved here are. I was content with the previous answer on this thread in the beginning but only on an abstract level. I came back to it because I felt there needed to be a more concrete explanation- the lack of one always leaves me with an uneasy feeling.

    This operator << is a left bitwise shifter it takes the binary representation of that number or operand and shifts it over however many places specified by the operand or number on the right like in decimal numbers only in binaries. We are multiplying by base 2-when we move up however many places not base 10- so the number on the right is the exponent and the number on the left is a base multiple of 2.

    This operator |= take the operand on the left and or's it with the operand on the right- and this one -'&'and's the bits of both operands to left and right of it.

    So what we have here is a hash table which is being stored in a 32 bit binary number every time the checker gets or'd ( checker |= (1 << val)) with the designated binary value of a letter its corresponding bit it is being set to true. The character's value is and'd with the checker (checker & (1 << val)) > 0)- if it is greater than 0 we know we have a dupe- because two identical bits set to true and'd together will return true or '1''.

    There are 26 binary places each of which corresponds to a lowercase letter-the author did say to assume the string only contains lowercase letters- and this is because we only have 6 more (in 32 bit integer) places left to consume- and than we get a collision

    00000000000000000000000000000001 a 2^0
    
    00000000000000000000000000000010 b 2^1
    
    00000000000000000000000000000100 c 2^2
    
    00000000000000000000000000001000 d 2^3
    
    00000000000000000000000000010000 e 2^4
    
    00000000000000000000000000100000 f 2^5
    
    00000000000000000000000001000000 g 2^6
    
    00000000000000000000000010000000 h 2^7
    
    00000000000000000000000100000000 i 2^8
    
    00000000000000000000001000000000 j 2^9
    
    00000000000000000000010000000000 k 2^10
    
    00000000000000000000100000000000 l 2^11
    
    00000000000000000001000000000000 m 2^12
    
    00000000000000000010000000000000 n 2^13
    
    00000000000000000100000000000000 o 2^14
    
    00000000000000001000000000000000 p 2^15
    
    00000000000000010000000000000000 q 2^16
    
    00000000000000100000000000000000 r 2^17
    
    00000000000001000000000000000000 s 2^18
    
    00000000000010000000000000000000 t 2^19
    
    00000000000100000000000000000000 u 2^20
    
    00000000001000000000000000000000 v 2^21
    
    00000000010000000000000000000000 w 2^22
    
    00000000100000000000000000000000 x 2^23
    
    00000001000000000000000000000000 y 2^24
    
    00000010000000000000000000000000 z 2^25
    

    So, for an input string 'azya', as we move step by step

    string 'a'

    a      =00000000000000000000000000000001
    checker=00000000000000000000000000000000
    
    checker='a' or checker;
    // checker now becomes = 00000000000000000000000000000001
    checker=00000000000000000000000000000001
    
    a and checker=0 no dupes condition
    

    string 'az'

    checker=00000000000000000000000000000001
    z      =00000010000000000000000000000000
    
    z and checker=0 no dupes 
    
    checker=z or checker;
    // checker now becomes 00000010000000000000000000000001  
    

    string 'azy'

    checker= 00000010000000000000000000000001    
    y      = 00000001000000000000000000000000 
    
    checker and y=0 no dupes condition 
    
    checker= checker or y;
    // checker now becomes = 00000011000000000000000000000001
    

    string 'azya'

    checker= 00000011000000000000000000000001
    a      = 00000000000000000000000000000001
    
    a and checker=1 we have a dupe
    

    Now, it declares a duplicate

    0 讨论(0)
  • 2020-12-04 05:18

    int checker is used here as a storage for bits. Every bit in integer value can be treated as a flag, so eventually int is an array of bits (flag). Each bit in your code states whether the character with bit's index was found in string or not. You could use bit vector for the same reason instead of int. There are two differences between them:

    • Size. int has fixed size, usually 4 bytes which means 8*4=32 bits (flags). Bit vector usually can be of different size or you should specify the size in constructor.

    • API. With bit vectors you will have easier to read code, probably something like this:

      vector.SetFlag(4, true); // set flag at index 4 as true

      for int you will have lower-level bit logic code:

      checker |= (1 << 5); // set flag at index 5 to true

    Also probably int may be a little bit faster, because operations with bits are very low level and can be executed as-is by CPU. BitVector allows writing a little bit less cryptic code instead plus it can store more flags.

    For future reference: bit vector is also known as bitSet or bitArray. Here are some links to this data structure for different languages/platforms:

    • CPP: BitSet
    • Java: BitSet
    • C#: BitVector32 and BitArray
    0 讨论(0)
  • 2020-12-04 05:18

    Just in case if anyone is looking for kotlin equivalent of unique characters in a string using bit vector

    fun isUnique(str: String): Boolean {
        var checker = 0
        for (i in str.indices) {
            val bit = str.get(i) - 'a'
            if (checker.and(1 shl bit) > 0) return false
            checker = checker.or(1 shl bit)
        }
        return true
    }
    

    Ref: https://www.programiz.com/kotlin-programming/bitwise

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-04 05:25

    Reading Ivan's answer above really helped me, although I would phrase it somewhat differently.

    The << in (1 << val) is a bit shifting operator. It takes 1 (which in binary is represented as 000000001, with as many preceding zeroes as you like / are allocated by memory) and shifts it to the left by val spaces. Since we're assuming a-z only and subtracting a each time, each letter will have a value of 0-25, which will be that letter's index from the right in the checker integer's boolean representation, since we will move the 1 to the left in checker val times.

    At the end of each check, we see the |= operator. This merges two binary numbers, replacing all 0's with 1's if a 1 exists in either operand at that index. Here, that means that wherever a 1 exists in (1 << val), that 1 will be copied over into checker, while all of checker's existing 1's will be preserved.

    As you can probably guess, a 1 functions here as a boolean flag for true. When we check to see if a character is already represented in the string, we compare checker, which at this point is essentially an array of boolean flags (1 values) at the indexes of characters that have already been represented, with what is essentially an array of boolean values with a 1 flag at the index of the current character.

    The & operator accomplishes this check. Similar to the |=, the & operator will copy over a 1 only if both operands have a 1 at that index. So, essentially, only flags already present in checker that are also represented in (1 << val) will be copied over. In this case, that means only if the current character has already been represented will there be a 1 present anywhere in the result of checker & (1 << val). And if a 1 is present anywhere in the result of that operation, then the value of the returned boolean is > 0, and the method returns false.

    This is, I'm guessing, why bit vectors are also called bit arrays. Because, even though they aren't of the array data type, they can be used similar to the way arrays are used in order to store boolean flags.

    0 讨论(0)
提交回复
热议问题