careercup/ctci

Question1_1 in java

SmileEye opened this issue · 2 comments

Hey,the code of this maybe wrong:

public static boolean isUniqueChars(String str) {
    if (str.length() > 128) {
        return false;
    }
    int checker = 0;
    for (int i = 0; i < str.length(); i++) {
        int val = str.charAt(i) - 'a';
        System.out.println(val);
        if ((checker & (1 << val)) > 0) return false;
        checker |= (1 << val);
    }
    return true;
}

when i try these :
String[] words = {"0p","1q","2r","3s","4t","5u","6v","7w","8x","9y"};
the result is false.
image

No, it's not incorrect exactly. If you read the book, it makes the assumption for that method that the string contains only the lowercase a to z characters.

the book's code is different from the code on github:

int the book,the code is:
if (str.length() > 26) {
return false;
}
it's contains only the lowercase a to z characters.

but on github,the code is:

if (str.length() > 128) {
return false;
}

it's contains all ASCII.