class Solution {
public String countAndSay(int n) {
if (n == 1)
return "1";
String s = countAndSay(n - 1);
String ans = "";
int cnt = 1;
for (int i = 0; i < s.length(); i++) {
if (i + 1 < s.length() && s.charAt(i) == s.charAt(i + 1)) {
cnt += 1;
} else {
ans += String.valueOf(cnt);
ans += s.charAt(i);
cnt = 1;
}
}
return ans;
}
}