I tried this approach but I was not getting the first element of the string. I tried to convert in the opposite way which was way too easy. But in this one somehow I am missing something. Question is, we need to decipher a string from "aabbcdd" to "a2 b2c1d2"
function check(len, string){
// var obj = {};
var new_string = "";
var count =1;
//console.log(string);
for(var i=0; i < string.length; i++){
let str = string[i];
if(string[i]===string[i+1] ){
count++;
new_string = new_string + string[i+1] + count;
}
if(string[i]!== string[i-1] && string[i]!== string[i+1] && string[i-1] !==undefined){
count = 1;
new_string = new_string + string[i-1] + count;
}
// console.log(string[i],string[i+1], count)
}
console.log(new_string);
}
Thank you
You'll need to handle the first character of the string differently than the other characters, and you'll also need to update the string again at the end after you've handled the final character.
Here's a heavily commented example demonstrating how you can do so. If something is unclear, feel free to ask for clarification in a comment.
TS Playground