Count and Say — LeetCode #38

Norman Aranez
3 min readDec 25, 2022

--

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

Solutions:

Python

class Solution:
def countAndSay(self, n: int) -> str:
s = "1"
for _ in range(n - 1):
count = 1
new_s = ""
for i in range(1, len(s)):
if s[i] == s[i - 1]:
count += 1
else:
new_s += str(count) + s[i - 1]
count = 1
new_s += str(count) + s[-1]
s = new_s
return s

C#

public class Solution
{
public string CountAndSay(int n)
{
string s = "1";
for (int i = 0; i < n - 1; i++)
{
int count = 1;
string newS = "";
for (int j = 1; j < s.Length; j++)
{
if (s[j] == s[j - 1])
{
count++;
}
else
{
newS += count.ToString() + s[j - 1];
count = 1;
}
}
newS += count.ToString() + s[s.Length - 1];
s = newS;
}
return s;
}
}

Java

public class Solution {
public String countAndSay(int n) {
String s = "1";
for (int i = 0; i < n - 1; i++) {
int count = 1;
StringBuilder newS = new StringBuilder();
for (int j = 1; j < s.length(); j++) {
if (s.charAt(j) == s.charAt(j - 1)) {
count++;
} else {
newS.append(count).append(s.charAt(j - 1));
count = 1;
}
}
newS.append(count).append(s.charAt(s.length() - 1));
s = newS.toString();
}
return s;
}
}

Javascript

/**
* @param {number} n
* @return {string}
*/
var countAndSay = function(n) {
let s = "1";
for (let i = 0; i < n - 1; i++) {
let count = 1;
let newS = "";
for (let j = 1; j < s.length; j++) {
if (s[j] === s[j - 1]) {
count++;
} else {
newS += count + s[j - 1];
count = 1;
}
}
newS += count + s[s.length - 1];
s = newS;
}
return s;
};

Typescript

function countAndSay(n: number): string {
let s = "1";
for (let i = 0; i < n - 1; i++) {
let count = 1;
let newS = "";
for (let j = 1; j < s.length; j++) {
if (s[j] === s[j - 1]) {
count++;
} else {
newS += count + s[j - 1];
count = 1;
}
}
newS += count + s[s.length - 1];
s = newS;
}
return s;
}

PHP

class Solution {

/**
* @param Integer $n
* @return String
*/
function countAndSay($n) {
$s = "1";
for ($i = 0; $i < $n - 1; $i++) {
$count = 1;
$newS = "";
for ($j = 1; $j < strlen($s); $j++) {
if ($s[$j] == $s[$j - 1]) {
$count++;
} else {
$newS .= $count . $s[$j - 1];
$count = 1;
}
}
$newS .= $count . $s[strlen($s) - 1];
$s = $newS;
}
return $s;
}

}

I hope this helps! Let me know if you have any questions. Don’t forget to follow and give some claps to support my content

--

--

Norman Aranez

I am a web developer skilled in React, GraphQL, TypeScript, and ASP.NET Core. I enjoy building modern, responsive applications