forked from jmcilhargey/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchpt1-is-unique.js
More file actions
25 lines (23 loc) · 767 Bytes
/
chpt1-is-unique.js
File metadata and controls
25 lines (23 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function isStringUniqueCharacters(string) {
// Ask if string is ASCII or Unicode -- ASCII is limited to 128 chars
var asciiChars = 128;
if (string.length > asciiChars) {
return false;
}
var characterArray = new Int8Array(128);
// Initialize array with 0s
for (var i = 1; i <= asciiChars; i++) {
characterArray[i] = 0;
}
// Iterate array at index for character code
for (var j = 0; j < string.length; j++) {
characterArray[string.charCodeAt(j)]++;
if (characterArray[string.charCodeAt(j)] > 1) {
return false;
}
}
return true;
}
// Can use object as well!
// Note solution with typed Javascript array can reduce memory cost since values are 0(false) or 1(true)
console.log(isStringUniqueCharacters("AaBbCc!@#$zZyYxX.,"));