2

The code is taken from career cup book

public static boolean isUniqueChars(String str)  {

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

    int checker = 0;
    for (int i = 0; i < str.length(); i++) {
        int val = str.charAt(i) - 'a';
        if ((checker & (1 << val)) > 0) return false;
        checker |= (1 << val);
    }
    return true;
}

Thank you for explanation and I am not sure what do I get. Lets look at the following code-

public class ConvertAscii {

    public static void main(String args[]){
        String str ="Hello How are you";
        int i =0;
        for(i=0;i<str.length();i++){
            System.out.println(str.charAt(i)-'a');
        }
    }

}

It gives me following output-

-24 12 32 34

etc

Also as in the above example we have

For example if str is "fbhsdsbfid" and i is 4 then val is equal to 3. What does subtracting ascii value of character 'a' from another character results in? Please explain more

Danh
  • 5,916
  • 7
  • 30
  • 45
user3412252
  • 31
  • 1
  • 2
  • Please add a tag indicating what language this is. (Is it Java?) Also, CareerCup is a publisher; you might want to specify which of their books you're referring to. (The question is 4 years old and the OP hasn't been on the site recently. Perhaps someone else who's sure what language this is can add a tag.) – Keith Thompson Jun 15 '18 at 18:14

2 Answers2

0

It takes the character which is at index i in str and substracts the ASCII value of the character 'a'.

For example if str is "fbhsdsbfid" and i is 4 then val is equal to 3.

Biduleohm
  • 734
  • 2
  • 10
  • 19
  • int val = str.charAt(i) - 'a'; Its basically performing subtraction of '**ascii number**' of char at index **i** in String '**str**' **with** ascii number of char '**a**' – Vishal Jagtap May 05 '16 at 10:40
0

To answer your question for index i = 4, the character at index 4 is 'd' and it's corresponding ASCII value is 64.

The ASCII value of 'a' is 61.Therefore, str.charAt(i) - 'a' gives 64 - 61 = 3.

Shashank
  • 82
  • 1
  • 1
  • 9