-1

I want to understand the working of if block from the below code. For example: if my string is rock, then the ASCII would be char_set[r] = 114 char_set[o] = 111,char_set[c] = 99,char_set[k] = 107. So for the first time when char_set[r] = 114 will come to if block then what is going to be checked? Since char_set is boolean type so it could be either true or false but according to my understanding initially char_set[r] = 114 is not set to either true or false. So how can we check if it is either true or false inside the if block. I have looked at the previous related questions but my question was different and couldn't find anything. This question is taken from the book Cracking the coding interview.

public boolean isuniqueChars2(String str) {

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

    boolean[] char_set = new boolean[256];
    for (int i = 1; i < str.length(); i++)
    {
        int val = str.charAt(i);
        if (char_set[val]) {
            return false;
      }
        char_set[val] = true;
    }
    return true;
}
2
  • 3
    How can char_set[r] = 114 when char_set is a boolean[]? Commented Jun 16, 2015 at 21:48
  • In Java, you are not using ASCII. charAt can return 0 to 65535, which is a Unicode/UTF-16 code unit.Please take some time later to understand that. Commented Jun 16, 2015 at 23:11

3 Answers 3

1

Arrays are initialized with default values upon creation. Default value for boolean type is false.

Sign up to request clarification or add additional context in comments.

5 Comments

What does this have to do with the question?
@Jashaszun, I assume that it answers it, I understood char_set[r] = 114 notation as char_set[r] "is the same as" char_set[114], and concluded that the author does not know about default value for array items.
But the default value for booleans is false, which is intended for this sample of code. Whether or not the asker understands this is irrelevant, since there's no problem with this.
@Jashaszun, "So for the first time when char_set[r] = 114 will come to if block then what is going to be checked?", do you still think that the problem is not in understanding the default value?
I believe that the misunderstanding is in the usage of characters for indexing into the boolean array, not in the default value of booleans.
0

What char_set does is for every ASCII value, it stores a boolean value for whether you've seen that character before or not.

So essentially, it is saying this (in somewhat pseudocode):

for (every character in the string) {
    int val = str.charAt(i); //get the current character;
    if (char_set[val]) // if (we've seen the current character already)
        return false; // not made of unique characters
    char_set[val] = true; //store that we've seen the current character;
}
return true; // we got through all the characters, so it must now be unique.

What is really happening is that if your input string is rock, then char_set['r'] is true, and so is char_set['o'], char_set['c'], and char_set['k'].

Comments

0

The elements of char_set are booleans. They are all initialized to the default value for that type (false), when you create the array. The values of some of those elements may be tested in

        if (char_set[val]) //...

(yielding one of the two possible values of type boolean, whichever was most recently set) and may be set to true in

        char_set[val] = true;

in each case, val is the index of the element involved, not the value to set or read.

Note, however, that this code has at least two significant problems:

  1. String.charAt() returns values of type char, whose numeric range goes up to 65535. Therefore, the code presented can easily generate an ArrayIndexOutOfBoundsException.

  2. Strings are indexed starting from 0, so the first character in the string is not included in the analysis. In a general sense, this could be intentional, but in an "analyze this code" sense it is important to recognize.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.