In the Leetcode Count and Say problem solution The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
countAndSay(1) = “1”
countAndSay(n) is the way you would “say” the digit string from countAndSay(n-1), which is then converted into a different digit string.
To determine how you “say” a digit string, split it into the minimal number of substrings such that each substring contains exactly one unique digit. Then for each substring, say the number of digits, then say the digit. Finally, concatenate every said digit.
For example, the saying and conversion for digit string “3322251”:
Given a positive integer n, return the nth term of the count-and-say sequence.
Example 1:
Input: n = 1
Output: “1”
Explanation: This is the base case.
Example 2:
Input: n = 4
Output: “1211”
Explanation:
countAndSay(1) = “1”
countAndSay(2) = say “1” = one 1 = “11”
countAndSay(3) = say “11” = two 1’s = “21”
countAndSay(4) = say “21” = one 2 + one 1 = “12” + “11” = “1211”
Constraints:
- 1 <= n <= 30
Solution in C Programming
static char *baseCases[] = {
0,
"1",
"11",
"21",
"1211",
"111221"
};
static char res[5000];
char* countAndSay(int n) {
char *s, *local;
int k, len;
if (n <= 5) return baseCases[n];
s = countAndSay(n-1);
len = strlen(s);
local = (char *)malloc(sizeof(char) * len + 1);
strcpy(local, s);
local[len] = 0;
k = 0;
for (int j = 0; j < len; j++) {
int sum;
sum = 1;
while(local[j] == local[j+1]) {
sum++;
j++;
}
res[k++] = sum + '0';
res[k++] = local[j];
}
res[k] = 0;
free(local);
return res;
}
Solution in C++ Programming
class Solution {
public:
string countAndSay(int n) {
string ans="1", prev="";
for(int i=1; i<n; ++i){
for(int j=0; j<ans.size(); ++j){
int c=1;
while(j+c<ans.size() and ans[j]==ans[j+c]) ++c;
j += (c-1);
prev += to_string(c);
prev += ans[j];
}
ans = prev;
prev = "";
}
return ans;
}
};
Solution in Java Programming
class Solution {
public static String countAndSay(int n) {
if (n == 1) {
return "1";
}
if (n == 2) {
return "11";
}
String result = "11";
for (int i = 2; i < n; i++) {
String inner = "";
int count = 0;
for (int j = 0; j < result.length() - 1; j++) {
if (result.charAt(j) != result.charAt(j + 1)) {
count++;
inner += (count) + String.valueOf(result.charAt(j));
count = 0;
} else {
count++;
}
}
if (result.charAt(result.length()-2) != result.charAt(result.length()-1)) {
count=0;
}
inner += (count + 1) + String.valueOf(result.charAt(result.length() - 1));
// inner+=(count+1)+String.valueOf(result.charAt(result.length()-1));
result = inner;
}
return result;
}
}
Solution in Python Programming
class Solution(object):
def countAndSay(self, n):
if n == 1: return '1'
ans = '1'
for i in range(1, n):
count, currStr, nextAns = 1, ans[0], ''
for j in range(1, len(ans)):
if currStr == ans[j]:
count += 1
else:
nextAns += (str(count) + currStr)
currStr, count = ans[j], 1
nextAns += (str(count) + currStr)
ans = nextAns
return ans
Solution in C# Programming
public class Solution {
public string CountAndSay(int n) {
string s = "1";
return CountAndSay(n-1, s);
}
private string CountAndSay(int n, string s) {
if (n == 0)
return s;
StringBuilder sb = new StringBuilder();
char pc = '0';
char c = '0';
int count = 0;
for (int i = 0; i < s.Length; i++) {
c = s[i];
if (c != pc) {
if (count > 0)
sb.Append($"{count}{pc}");
pc = c;
count = 1;
}
else {
count++;
}
}
sb.Append($"{count}{c}");
return CountAndSay(n-1, sb.ToString());
}
}
Also read,