Unique characters | write a function to find out if a string is unique or not

unique characters


 Write a function which will find out if a given string has unique characters in it and  the string should be case sensitive means either the input will be in lowercase  or  in a uppercase. you have to find out unique string.

let's assume you have a string `good`    so here the `o` is more than one so it's not a valid string or unique string  you have to return a value false.

Let's assume you have a string `pwsxy` so here each character is unique and so the output will be true 



Approach 

In this first we have to create a function in  javascript. so you can use any  `IDE` like vscode , sublime and atom etc .  Create a function   validUnique  and through this function we are going to find out wether a string is unique or not based on that we have to return the result. I am using vscode for the `IDE` you can use whatever  you like.


Lets write first some pseudo-code

we have to write a function the name would be  uniqueCharactes() which will be taking parameter string 

fun uniqueCharactes()
we have to take an empyt obj so that we can map our characters
obj={}  empty object
than we have to run a loop over the string and chect their repetitions of characters

 for(i=0; i<length)

than we have to check the values of the charactes in the object 
If my chractes count is greater than one return false else return true


Solution





function uniqueCharacters(string){

let obj={} //* taking an empty object for storing
chracters with their count
for(let i=0; i<=string.length-1; i++){ //* running a for loop
let char= string[i]
if(obj[char]==undefined){ //* checking if character is
not present in my obj
than character stores
//* with count one
obj[char]=1
}else{
obj[char]++ //* if my character is present in obj
that it will increment its value
}
}


for(let key in obj){
if(obj[key]>1){
return false; //* checking the count of character
if its more than one return false
}
}
return true
}


let result0 = uniqueCharacters("psdre")
let result1 = uniqueCharacters("pssdre")


console.log(result0) // output will be true
console.log(result1) // ouput will be false


 Time complexity of above solution

1. The first for loop is  iterating over  the length of the string and if the length of the string will be n  than it iterates till n  so the time complexity of the first loop is  o(n), where n is the length of the string 

2. The second loop is  iterating over the each  key of the object as  if all the characters of the string are going to be unique than the keys would be equal to n  so therefore the time complexity of the second loop is o(n)


 overall time complexity of the solution is :

 o(n) + o(n) = o(2n) which simplifies to the o(n)


Space complexity

We are taking an empty object for storing the characters  and if my all characters are unique than the space complexity would be o(n) where n is the length of string



We can optimize  this to  further increase the performance using the  `set` approach we can optimize a little bit more  below is the optimize solution



function uniqueCharacters(string) {
let charSet = new Set(); // Here we are using the set for storing
the unique characters

for (let i = 0; i <= string.length-1; i++) {
let char = string[i];
if (charSet.has(char)) {
// If character is already in Set, it's a duplicate
return false;
}
charSet.add(char); // Add the character to the Set
}
// If we reach this point, all characters are unique
return true;
}

let result0 = uniqueCharacters("psdre");
let result1 = uniqueCharacters("pssdre");

console.log(result0); // true
console.log(result1); // false



In above solution we are returning the result in the first  `if` statement if the char is present in charSet it will return false 
 but the time and space  complexity  will be o(n ) but in case of set the set is more efficient that the taking an empty object approach but above solution increase the performance of the solution


Post a Comment

Previous Post Next Post