I've got string interview question regarding run-length-encoding and my solution is O(n). Is there any way to improve it:
- input is AABBBCAAEE
- output suppose to be A2B3C1A2E2
const firstString=(str)=>{
const arr = str.split('');
let counter = 1;
let result ='';
for (let i=0; i<arr.length; i++){
if(arr[i] === arr[i+1]){
counter++;
} else {
result +=arr[i]+counter;
counter = 1;
}
} return result
};
firstString('AABBBCAAEE');