Last active
August 29, 2015 14:02
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
commented
Jun 16, 2014
- Please use HashSet instead of HashTable.
- 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