String has all unique characters

Run Settings
LanguageJava
Language Version
Run Command
//Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures? import java.util.Arrays; class Main { public static void main(String[] args) { System.out.println(isUnique("ABCDEFGIJKLMNOPQRSTWXYZ")); /* */ } public static boolean isUnique(String str){ if(str == null && str.length() == 0){ return false; } char tempArray[] = str.toCharArray(); Arrays.sort(tempArray); for(int i=1;i<tempArray.length;i++){ if(tempArray[i] == tempArray[i-1]){ return false; } } return true; } }
//Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures? import java.util.Arrays; import java.util.Scanner; class Main2 { public static void main(String[] args) { Scanner s = new Scanner(System.in); String input = s.nextLine(); System.out.println(isUnique(input)); /* */ } public static boolean isUnique(String str){ if(str == null){ return false; } if(str.length() == 0 || str.length() >128){ return false; } int[] temp = new int[128]; for(int i=0;i< str.length(); i++){ int key = str.charAt(i); if(temp[key] != 0){ return false; }else{ temp[key]++; } } return true; } }
Editor Settings
Theme
Key bindings
Full width
Lines