Skip to content

Latest commit

 

History

History
25 lines (23 loc) · 556 Bytes

038.md

File metadata and controls

25 lines (23 loc) · 556 Bytes

38. Count and Say

Solution 1

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;
    }
}