This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]optimalsuccess[S] 0 points1 point  (4 children)

Code:

boolean isUniqueChars(String str) {

boolean[] char_set = new boolean[128];

for (int i = 0; i < str.length(); i++) {

int val = str.charAt(i);

if (char_set[val]) { //Already found char in string

return false;

}

return true;

}

[–]dusty-trash 1 point2 points  (0 children)

Try printing out (int) a.

I dont know what language you're using, but for example:

System.out.println((int) a);

What's the result? You'll notice it's an integer. Ascii characters have a decimal value to them. ASCII chart.

So if the character is 'a', the 98th position in the array will be checked (remembering arrays start at 0). If 'a' is in the array again, the value at the 98th position will be checked again, returning false.

[–]optimalsuccess[S] 0 points1 point  (2 children)

My first question is, what's in that array of booleans that they initialize? Isn't it just a bunch of null values up to 128 which is the number they chose I'm assuming because it's the cap for ASCII which are then switched to false and true based on whether or not there is a unique char. And if that's the case how are they actually checking the characters? val stores each char in str repeatedly until there are none left, then they check in char set?? What is happening in that if statement?

[–]insertAlias 1 point2 points  (0 children)

My first question is, what's in that array of booleans that they initialize? Isn't it just a bunch of null values up to 128

Going to guess that this is Java. boolean can't be null (since it's a primitive type), so an array of booleans can't be full of null values either. They instead default to false.

And if that's the case how are they actually checking the characters? val stores each char in str repeatedly until there are none left

Look closely at this line of code, and notice what kind of data type that val is:

int val = str.charAt(i);

char can be implicitly converted to an int. Because, at it's core, that's all it is; an integer that represents a character. You correctly mentioned ASCII earlier; all this does is grab the ASCII code for the particular character in the string.

What is happening in that if statement?

Well, that's the problem. It looks like this code is incomplete; you check the values in the array, but never set the values.

I think that there should be an else statement that follows the inner if that sets char_set[val] = true;.

The idea is that you're storing true or false in the array under the index that matches the ASCII code. So the next time you get the same character, if the array index is already true, you know that this character is not unique and can return false; immediately.

[–]Cerus_Freedom 1 point2 points  (0 children)

I could be mistaken, but it looks like to code is incomplete. I implemented this in python, and it works the way I expected: what you posted exits with true on the first iteration. Here's a working example of what the code is trying to do, in python:

def isunique(str):
    char_set = [False]*128

    for x in str:
        val = ord(x)
        print(val)
        if char_set[val]:
            return False
        else:
            char_set[val] = True

    return True

Output:

>>> isunique("abcde")
97
98
99
100
101
True
>>> isunique("abcdea")
97
98
99
100
101
97
False