class Solution:
def countAndSay(self, n: int) -> str:
result = "1"
for _ in range(n - 1):
prev = result
result = ""
i = 0
while i < len(prev):
count = 1
while i + 1 < len(prev) and prev[i] == prev[i + 1]:
i += 1
count += 1
result += str(count) + prev[i]
i += 1
return result
class Solution {
public:
string countAndSay(int n) {
string result = "1";
for (int k = 1; k < n; ++k) {
string prev = result;
result = "";
int i = 0;
while (i < prev.size()) {
int count = 1;
while (i + 1 < prev.size() && prev[i] == prev[i + 1]) {
++i;
++count;
}
result += to_string(count) + prev[i];
++i;
}
}
return result;
}
};
class Solution {
public String countAndSay(int n) {
String result = "1";
for (int k = 1; k < n; k++) {
StringBuilder sb = new StringBuilder();
int i = 0;
while (i < result.length()) {
int count = 1;
while (i + 1 < result.length() && result.charAt(i) == result.charAt(i + 1)) {
i++;
count++;
}
sb.append(count).append(result.charAt(i));
i++;
}
result = sb.toString();
}
return result;
}
}
var countAndSay = function(n) {
let result = "1";
for (let k = 1; k < n; k++) {
let prev = result;
result = "";
let i = 0;
while (i < prev.length) {
let count = 1;
while (i + 1 < prev.length && prev[i] === prev[i + 1]) {
i++;
count++;
}
result += count.toString() + prev[i];
i++;
}
}
return result;
};
The "Count and Say" problem asks you to generate the nth term in a specific sequence of numbers.
The sequence starts with "1"
, and each subsequent term is constructed by describing the previous term in terms of consecutive digits.
For example, the first few terms are:
"1"
"11"
(one 1)"21"
(two 1s)"1211"
(one 2, then one 1)"111221"
(one 1, one 2, two 1s)
Given an integer n
, your task is to return the nth term of this sequence as a string.
n
.At first glance, the problem looks like it requires generating a string based on a simple pattern. However, the key is to realize that each term is not just a transformation of numbers, but a "verbal description" of the previous term's digits.
A brute-force approach might involve manually building each term by counting consecutive digits and appending the count and digit to a new string. This is manageable for small n
, but we need to ensure our method is efficient and can handle larger values.
As we analyze further, we notice that:
Let's break down the solution step by step:
"1"
as the first term.
n
, we generate the next term by reading the previous term.This method is efficient because:
Let's walk through generating the 5th term (n = 5
):
result = "1"
"11"
"21"
"12"
"11"
"1211"
"11"
"12"
"21"
"111221"
So, for n = 5
, the output is "111221"
.
Brute-force Approach:
The length of each term grows exponentially, but for reasonable n
(as in Leetcode constraints), this approach is efficient.
The "Count and Say" problem is a classic example of sequence construction based on simple rules. By iteratively describing the previous term, we can build each new term efficiently. The key insight is to process the string in a single pass, counting consecutive digits, and to build only what we need for the next term. This results in a simple, elegant, and efficient solution that is easy to implement in any major programming language.