-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1.1 Is Unique #1
Comments
https://repl.it/@megantaylor/IsUnique //with data structure
function uniqueChars(str) {
let charObj = {};
for(let i = 0; i < str.length; i++) {
let char = str[i];
if(charObj[char]) return false; else charObj[char] = true;
}
return true;
}
// no data structures
function uniqueCharsNoDataStructure(string) {
for (let i = 0; i < string.length; i++) {
for (let j = i + 1; j < string.length; j++) {
if (string[i] === string[j]) {
return false;
}
}
}
return true;
}; |
https://repl.it/@rmorabia/CalmInsidiousAddons function cool(string) {
for (let i = 0; i < string.length; i++) {
let count = 0
for (let z = 0; z < string.length; z++) {
if (string[z] === string[i]) {
count++
if (count >= 2) {
return false
}
}
}
}
return true
}
cool('straddle')
cool('stradle') |
https://repl.it/@lpatmo/FussyMiserlyMedia // Implement an algorithm to determine if a string has all unique characters. What if you
// cannot use additional data structures?
/****** SOLUTION 1 *******/
function isUnique1(str) {
//loop through string
//check if each letter is unique
console.log(str)
let holder = {};
for (var i = 0; i < str.length; i++) {
if (holder[str[i]]) {
console.log('false')
return false;
} else {
holder[str[i]] = 1;
}
}
console.log(holder);
return true;
}
//console.log(isUnique2('helo'));
/****** SOLUTION 2 *******/
function isUnique2(str) {
//loop through string
//check if each letter is unique
if (str.length < 2) {
return true;
}
for (var left=0; left< str.length; left++) {
console.log(left)
for (var right = left+1; right < str.length; right++) {
if (str[left] === str[right]) {
return false;
}
}
}
return true;
}
//console.log(isUnique2('helo'));
Time complexity: nlogn?
/****** SOLUTION 3 *******/
function isUnique3(str) {
//use Sets
//check if each letter is unique
return new Set(str).length !== str.length;
}
console.log(isUnique3('helo')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: