Skip to content

Instantly share code, notes, and snippets.

@diegozeng
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save diegozeng/1f886a9736e905bce5ca to your computer and use it in GitHub Desktop.

Select an option

Save diegozeng/1f886a9736e905bce5ca to your computer and use it in GitHub Desktop.
/*
* 1.1 Implement an algorithm to determine if a string has all unique characters
* What if you cannot use additional data structures?
*
* 思路:假设输入是ascii字符,创建一个长度为256的array,遍历输入的字符串,若重复返回false,不重复返回true.
* 特殊情况: null和空字符串都认为是unique的输入,即返回true.输入的字符串长度大于256,返回false.
*/
import java.io.*;
class UniqueCharacters {
public static boolean UniqueCharactersFinder(String s) {
if (s == null || s.isEmpty()) {
return true;
}
int len = s.length();
if (len > 256) {
return false;
}
boolean[] ASCII_TABLE = new boolean[256]; //Default values are false
for(int i = 0; i < len; i++) {
char c = s.charAt(i);
if (ASCII_TABLE[c]) {
return false;
}
else {
ASCII_TABLE[c] = true;
}
}
return true;
}
public static void main(String[] args)throws Exception {
System.out.println("Please type the string:");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
if (UniqueCharactersFinder(s)) {
System.out.println("String with unique characters!");
}
else {
System.out.println("String with repeat characters!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment