diff --git a/38. Count and Say b/38. Count and Say new file mode 100644 index 0000000..2b656f1 --- /dev/null +++ b/38. Count and Say @@ -0,0 +1,24 @@ +class Solution { +public: + string countAndSay(int n) { + string curr = "1"; + if (n == 1) return curr; + for (int i = 2; i <= n; i++) { + string next = ""; + int cnt = 1; + char ele = curr[0]; + for (int j = 1; j < curr.size(); j++) { + if (curr[j] == ele) { + cnt++; + } else { + next += to_string(cnt) + ele; + ele = curr[j]; + cnt = 1; + } + } + next += to_string(cnt) + ele; + curr = next; + } + return curr; + } +};