I was looking through cracked solutions for interview books and noticed the following problem:
Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?
This was one of the solutions:
public static boolean isUniqueChars2(String str) { boolean[] char_set = new boolean[256]; for (int i = 0; i < str.length(); i++) { int val = str.charAt(i); if (char_set[val]) return false; char_set[val] = true; } return true; }
Why is the char_set array initialized to 256? I thought this was because there are 128 ascii characters, but I'm not sure. Also, this solution is similar to Java, but should there be an initial size if it was done in C ++?
source share