Skip to content

Instantly share code, notes, and snippets.

@chouclee
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chouclee/837102ddb0273f428cb5 to your computer and use it in GitHub Desktop.
Save chouclee/837102ddb0273f428cb5 to your computer and use it in GitHub Desktop.
[CC150][1.1] Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures? (applicable for Unicode, but use HashSet)
import java.util.Scanner;
import java.util.HashSet;
public class UniqueStringHashSet {
public static boolean unique(String s) {
HashSet<Character> set = new HashSet<Character>();
for (int i = 0; i < s.length(); i++) {
if (!set.add(s.charAt(i)))
return false;
}
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String s = in.nextLine();
if (unique(s))
System.out.println("Unique");
else
System.out.println("Not Unique") ;
}
in.close();
}
}
@jason51122
Copy link

  1. Please use HashSet instead of HashTable.
  2. Try not to use variable names like ht, temp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment