38 - Count and Say

 JAVA

  • class Solution {
        public String countAndSay(int n) {
            String s = "1";
            while (--n > 0) {
                StringBuilder t = new StringBuilder();
                for (int i = 0; i < s.length();) {
                    int j = i;
                    while (j < s.length() && s.charAt(j) == s.charAt(i)) {
                        ++j;
                    }
                    t.append((j - i) + "");
                    t.append(s.charAt(i));
                    i = j;
                }
                s = t.toString();
            }
            return s;
        }
    }
C++
  • class Solution {
    public:
        string countAndSay(int n) {
            string s = "1";
            while (--n) {
                string t = "";
                for (int i = 0; i < s.size();) {
                    int j = i;
                    while (j < s.size() && s[j] == s[i]) ++j;
                    t += to_string(j - i);
                    t += s[i];
                    i = j;
                }
                s = t;
            }
            return s;
        }
    };

Comments