Quick actions

cmd+k|ctrl+k

Navigation

Languages

String Compression

Snippet info

Language

JavaScript

Visibility

public

Author

soham57

Created

2022-08-26T17:04:47.48386Z

Updated

2022-08-26T17:04:47.48386Z

function strCompression(str){
    
    let compressed=''
    if(str.length===0){
        return compressed;
    }
    
    
    for(let i=0; i<str.length; i++){
        let count=1;
        while(str[i]===str[i+1]){
            count++;
            i++;
        }
        compressed+=`${str[i]}${count}`
        count=1;
    }
    
    return compressed;
}

console.log(strCompression("aaabccccddeeea"));
INFO