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

added more solution approach with well comments to understand #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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.

None yet

1 participant