classSolution { public String countAndSay(int n) { StringBuildersb=newStringBuilder(); sb.append(1); for (inti=2; i <= n; i++) { // dummy char, the flag which represents the char array is finish // and it is never equal to any actual useful char in array sb.append('#'); char[] prevChars = sb.toString().toCharArray(); sb = newStringBuilder(); intcount=1; for (intj=0; j < prevChars.length - 1; j++) { if (prevChars[j] == prevChars[j + 1]) { count++; } else { sb.append(count).append(prevChars[j]); count = 1; } } } return sb.toString(); } }