Created
July 22, 2014 21:35
-
-
Save arcesino/afe6745d6607c39ea3f2 to your computer and use it in GitHub Desktop.
Check unique ASCII chars using bit flags
This file contains hidden or 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
public class CrackingTheCode1 { | |
public static void main(String[] args) { | |
CrackingTheCode1 app = new CrackingTheCode1(); | |
String[] tests = new String[] { | |
"abc123", "abc12a", "abc122", "s3cr3t0", "!#%&/()", "#abc123#" | |
}; | |
for (int i = 0; i < tests.length; i++) { | |
String s = tests[i]; | |
System.out.printf("%s : %b\n", s, app.isUniqueChars(s)); | |
} | |
} | |
public boolean isUniqueChars(String s) { | |
long[] checkers = new long[4]; | |
int length = s.length(); | |
for (int i = 0; i < length; i++) { | |
int val = s.charAt(i); | |
int index = val / 256; | |
long offset = (1L << val); | |
if ((checkers[index] & offset) > 0) return false; | |
checkers[index] |= offset; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment