14. Look and Say Pattern
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given an integer n
, return the n
th term in the Look-and-Say Sequence, also known as the Count and Say sequence.
This sequence is built by describing the previous term in terms of the count of digits in groups of the same digit.
๐ How It Works:
Start with "1"
as the first term. To generate each subsequent term:
Read off the digits of the previous term.
For each group of consecutive identical digits, state:
The number of times it appears (the count),
Followed by the digit itself.
๐ Examples of the Sequence:
1 # First term
11 # One 1 โ "11"
21 # Two 1s โ "21"
1211 # One 2, One 1 โ "1211"
111221 # One 1, One 2, Two 1s โ "111221"
...
๐ Examples
Example 1:
Input: n = 5
Output: 111221
Explanation: The sequence evolves as: 1 โ 11 โ 21 โ 1211 โ 111221
Example 2:
Input: n = 3
Output: 21
Explanation: The third term is: 1 โ 11 โ 21
๐ Constraints
$1 \leq n \leq 30$
โ
My Approach
๐ง Iterative Character Grouping
We iteratively build each term in the Look-and-Say sequence by scanning the previous term and counting consecutive digits.
๐น Algorithm Steps:
Initialize the sequence with the first term as
"1"
.Repeat the following process from the 2nd term to the
n
th term:Create an empty string
next_term
.Traverse the current term:
Count how many times a digit repeats consecutively.
Append the count followed by the digit to
next_term
.
Update the current term to
next_term
for the next iteration.
Return the final term after
n - 1
transformations.
๐งฎ Time and Auxiliary Space Complexity
Time
O(n ร L), where L
is the average length of terms in the sequence. Each of the n
iterations processes a string with increasing size.
Auxiliary Space
O(L), used for building the next term at each step.
๐ง Code (C++)
class Solution {
public:
string countAndSay(int n) {
string res = "1";
for (int i = 1; i < n; ++i) {
string temp;
for (int j = 0; j < res.size(); ) {
int k = j;
while (k < res.size() && res[k] == res[j]) ++k;
temp += to_string(k - j) + res[j];
j = k;
}
res = temp;
}
return res;
}
};
๐งโ๐ป Code (Java)
class Solution {
public String countAndSay(int n) {
String result = "1";
for (int i = 2; i <= n; i++) {
StringBuilder temp = new StringBuilder();
int count = 1;
for (int j = 1; j < result.length(); j++) {
if (result.charAt(j) == result.charAt(j - 1)) {
count++;
} else {
temp.append(count).append(result.charAt(j - 1));
count = 1;
}
}
temp.append(count).append(result.charAt(result.length() - 1));
result = temp.toString();
}
return result;
}
}
๐ Code (Python)
class Solution:
def countAndSay(self, n: int) -> str:
result = "1"
for _ in range(n - 1):
i = 0
new_result = ""
while i < len(result):
count = 1
while i + 1 < len(result) and result[i] == result[i + 1]:
count += 1
i += 1
new_result += str(count) + result[i]
i += 1
result = new_result
return result
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Letโs make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated