Detecting if a string has unique characters: comparing my solution to “Cracking the Coding Interview?”

后端 未结 7 1837
孤街浪徒
孤街浪徒 2020-12-22 16:43

I am working through the book \"Cracking the Coding Interview\" and I have come across questions here asking for answers, but I need help comparing my answer to the solution

7条回答
  •  自闭症患者
    2020-12-22 17:45

    The solution from the book is case insensitive. 'A' and 'a' is considered duplicate as per the implementation.

    Explanation: for input string with char 'A', 'A' - 'a' is -32 so '1 << val' will be evaluated as 1 << -32. shift on any negative number will shift the bits in opposite direction. thus 1 << -32 will be 1 >> 32. Which will set the first bit to 1. This is also the case with char 'a'. Thus 'A' and 'a' are considered duplicate characters. Similarly for 'B' and 'b' second bit is set to 1 and so on.

提交回复
热议问题