Skip to content

Instantly share code, notes, and snippets.

@WOLOHAHA
Last active August 29, 2015 14:02
Show Gist options
  • Save WOLOHAHA/1408fb20ad4eaad7447a to your computer and use it in GitHub Desktop.
Save WOLOHAHA/1408fb20ad4eaad7447a to your computer and use it in GitHub Desktop.
Implement an algorithm to determine if a string has all unique characters. Whatif you cannot use additional data structures?
public boolean uniqueChar(String s) {
if (s == null)
throw new IllegalArgumentException();
if (s.length() <= 1)
return true;
if (s.length() > 256)
return false;
boolean[] iFlag = new boolean[256];
for (int i = 0; i < s.length(); i++) {
if (iFlag[s.charAt(i)]) {
return false;
}
iFlag[s.charAt(i)] = true;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment