From 1cf4f1c9fe0e7f5ae653722493c18bff57b7aed8 Mon Sep 17 00:00:00 2001 From: chayan das Date: Fri, 18 Apr 2025 14:01:22 +0530 Subject: [PATCH] Create 38. Count and Say --- 38. Count and Say | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 38. Count and Say 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; + } +};