Skip to content
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

Open
rmorabia opened this issue Nov 28, 2018 · 3 comments
Open

1.1 Is Unique #1

rmorabia opened this issue Nov 28, 2018 · 3 comments

Comments

@rmorabia
Copy link
Owner

Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?

@megantaylor
Copy link

megantaylor commented Nov 28, 2018

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;
};

@rmorabia
Copy link
Owner Author

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')

@lpatmo
Copy link

lpatmo commented Nov 28, 2018

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants