Here' i have a solution of a problem of removing consecutive duplicate characters. But i need to know the time and space complexity of this solution.
Here's the function:
function removeAdjacentDuplicates(str){
for (let i = 0; i< str.length -1; i++){
if(str[i] === str[i+1] && i+2==str.length){
return str.substr(0,i);
} else if(i+2==str.length ||str=="" || str.length==0){
return str;
} else if (str[i] === str[i+1]){
return removeAdjacentDuplicates(str.substr(0,i)+str.substr(i+2,(str.length)-(i+2)));
}
}
};
//removeAdjacentDuplicates('abbc') returns 'ac'
I'm guessing it should be O(n) for both because for each input the function needs to loop over the whole. Also suggestion for making the function better would be appreciated too.