Unique Characters

Unique Characters

Write a method to check if the string has unique characters.

Here is the code



/**
* @author Gaurav Gupta
*
*/
public class UniqueCharacters {
public static boolean checkUniqueness(String str) {
boolean[] bs = new boolean[128];
for (int i = 0; i < str.length(); i++) {
int val = str.charAt(i);
if (bs[val]) {
return false;
}
bs[val] = true;
}
return true;
}
public static void main(String[] args) {
System.out.println(checkUniqueness("Gaurav"));
System.out.println(checkUniqueness("Gaurv"));
}
}


 
Output:






1 Comment

Leave a Reply

Your email address will not be published.

Lorem ipsum dolor sit amet, consectetur a dipiscing elit. Vivamus leo ante,

FOLLOW US ON