Traverse in the string
for ( i = 1 ; i < st . length ; i ++ ) { if ( st [ i ] == st [ i - 1 ] ) cnt ++ ; else { st2 += String . fromCharCode ( ' ' . charCodeAt ( ) + cnt ) ; st2 += st [ i - 1 ] ; cnt = 1 ; } }