-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create All Unique Characters II.java
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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; | ||
} | ||
} |