Skip to content

Conversation

AnishMandal939
Copy link

// So we have two methods to one by creating extra space annd without creating extra space
// with extra spaces
let uniqueChars = (str) =>{
// creating a empty variable to map
let characters = {};
// iterate through all characters of the string and start inserting them one by one characters tabole
for(var i = 0; i < str.length; i++){
// check if string already present in characters variable
if(characters[str[i]] != null) {
// if present repetation of character than return false
characters[str[i]] = 1;
return false;
}else{
// !present duplicate characters than return true
characters[str[i]] = 0;

  }

  }
  return true;

}
// Since we are iterating trough each element of string only once string of length 1 average and worst case time complexity is O(n)
// without extra spaces

let uniqueChar = (str) => {
// iterate through string
for(let i =0; i< str.length; i++){
// checking against the subsequent characters & iterating through them
for(let j = i+1; j <=str.length-1; j++){
if(str[i] == str[j]){
return false;
}
}
}
return true;
}

/* TESTS */
console.log(everyCharUnique('abcd'), 'true');
console.log(everyCharUnique('abccd'), 'false');
console.log(everyCharUnique('bhjjb'), 'false');
console.log(everyCharUnique('mdjq'), 'true');
console.log(everyCharUniques('mdjq'), 'true');
console.log(uniqueChars('hello'), 'false');
console.log(uniqueChars('anish'), 'true');
console.log(uniqueChar('hello'), 'false');
console.log(uniqueChar('anish'), 'true');

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

Successfully merging this pull request may close these issues.

1 participant