Cracking the coding interview Flashcards
What did we learn abount ASCII and unicode encoding in isUnique (1.1) exercise?
“Implement an algorithm to determine if a string has all unique characters. What if you
cannot use additional data structures?”
the problem can be solved using bit wise operation as the input can either be ASCII or unicode encoded we could use bit mask for storing ‘1’ in the position relative to each string’s character represented in ASCII or unicode. This is done for each char, if a specific position already contains a 1 that can be easily detected using bitwise operations
how do you integer code for a character in JS
with charCodeAt, for example => ‘a’.charCodeAt(0)
which one is better? Exponential or Factorial time complexity?
Exponential
Ho would I check if an integer has exactly a single 1 in it’s binary representation?
You would simply subtract 1 to the value and do a Boolean AND between the number and the number minus 1, example
(4) 0100 (3) 0011
what we have with 3 is a series of zeros followed by 1s we can use this property to do a bit wise AND and check that the result is equal to zero