alert('AAAADEEESSQQQQQQ'.split('').reduce(function(m, c, i, a) { if(c == a[i+1]) m[m.length-1]++; else { m[m.length-1] = c+m[m.length-1]; if(a[i+1]) m.push(1); } return m }, [1]).join(''));