Created
June 18, 2021 11:09
-
-
Save thecoder8890/f684a0b399860cae735604accdac6f37 to your computer and use it in GitHub Desktop.
IsUnique is the question from cracking the coding interview
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 IsUnique { | |
/** | |
* O(N) O(1) | |
*/ | |
public boolean isUnique1(String s) { | |
if (s == null || s.length() > 256) { | |
return false; | |
} | |
boolean[] map = new boolean[256]; | |
char[] chars = s.toCharArray(); | |
for (char c : chars) { | |
if (map[c]) { | |
return false; | |
} | |
map[c] = true; | |
} | |
return true; | |
} | |
/** | |
* Assume the string only uses lowercase letters. Use just a single | |
* int(32bits) to save more space complexity | |
*/ | |
public boolean isUnique2(String s) { | |
if (s == null || s.length() > 26) { | |
return false; | |
} | |
int checker = 0; | |
char[] chars = s.toCharArray(); | |
for (char c : chars) { | |
int val = c - 'a'; | |
if ((checker & (1 << val)) > 0) { | |
return false; | |
} | |
checker |= (1 << val); | |
} | |
return true; | |
} | |
public static void main(String[] args) { | |
IsUnique iu = new IsUnique(); | |
String s = "abcb"; | |
System.out.println(iu.isUnique1(s)); | |
System.out.println(iu.isUnique2(s)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment