Approach 3 – Use of Extra Data Structure: This approach assumes ASCII char set(8 bits). The idea is to maintain a boolean array for the characters. The 256 indices represent 256 characters. All the array elements are initially set to false. As we iterate over the string, set true at the index equal to the int value of the character. If at any time, we encounter that the array value is already true, it means the character with that int value is repeated.

Approach 4 – Without Extra Data Structure: The approach is valid for strings having alphabet as a-z. This approach is little tricky. Instead of maintaining a boolean array, we maintain an integer value called checker(32 bits). As we iterate over the string, we find the int value of the character with respect to ‘a’ with the statement int bitAtIndex = str.charAt(i)-‘a’;
Then the bit at that int value is set to 1 with the statement 1<<bitAtIndex .
Now, if this bit is already set in the checker, the bit AND operation would make checker > 0. Return false in this case.
Else Update checker to make the bit 1 at that index with the statement checker = checker | (1<<bitAtIndex);

Exercise: Above program is case sensitive, you can try making same program which is case insensitive i.e Geeks and GEeks both give similar output.

Reference:
Cracking the Coding Interview by Gayle

This article is contributed by Saloni Baweja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.