Skip to content

Commit

Permalink
Create All Unique Characters II.java
Browse files Browse the repository at this point in the history
  • Loading branch information
ly16 authored Mar 14, 2018
1 parent 503eb93 commit 5e8bb76
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions All Unique Characters II.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Determine if the characters of a given string are all unique.
Assumptions
We are using ASCII charset, the value of valid characters are from 0 to 255
The given string is not null
Examples
all the characters in "abA+\8" are unique
"abA+\a88" contains duplicate characters
time = O(n)
space = O(n)
*/

public class Solution {
public boolean allUnique(String word) {
// Write your solution here
int[] vec = new int[8];
char[] array = word.toCharArray();
for (char c : array) {
if ((vec[c / 32] >>> (c % 32) & 1) != 0) {
return false;
}
vec[c / 32] |= (1 << (c % 32));
}
return true;
}
}

0 comments on commit 5e8bb76

Please sign in to comment.