Skip to content

Commit 730bb6f

Browse files
authored
Merge pull request #510 from thevijayshankersharma/0038-solution
Add a Solution for Count and Say (LeetCode Problem 38)
2 parents 959b2fb + 7c3fb22 commit 730bb6f

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
id: count-and-say
3+
title: Count and Say (LeetCode)
4+
difficulty: Medium
5+
sidebar_label: 0038-CountAndSay
6+
topics:
7+
- String
8+
- Recursion
9+
---
10+
11+
## Problem Description
12+
13+
| Problem Statement | Solution Link | LeetCode Profile |
14+
| :---------------- | :------------ | :--------------- |
15+
| [Merge Two Sorted Lists](https://leetcode.com/problems/count-and-say/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/count-and-say/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) |
16+
17+
## Problem Description
18+
19+
The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
20+
21+
- countAndSay(1) = "1"
22+
- countAndSay(n) is the run-length encoding of countAndSay(n - 1).
23+
24+
Run-length encoding (RLE) is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string "3322251" we replace "33" with "23", replace "222" with "32", replace "5" with "15" and replace "1" with "11". Thus the compressed string becomes "23321511".
25+
26+
Given a positive integer n, return the nth element of the count-and-say sequence.
27+
28+
### Examples
29+
30+
#### Example 1
31+
32+
- **Input:** n = 4
33+
- **Output:** "1211"
34+
- **Explanation:**
35+
- countAndSay(1) = "1"
36+
- countAndSay(2) = RLE of "1" = "11"
37+
- countAndSay(3) = RLE of "11" = "21"
38+
- countAndSay(4) = RLE of "21" = "1211"
39+
40+
#### Example 2
41+
42+
- **Input:** n = 1
43+
- **Output:** "1"
44+
- **Explanation:** This is the base case.
45+
46+
### Constraints
47+
48+
- `1 <= n <= 30`
49+
50+
### Approach
51+
52+
To solve this problem, we can use a recursive approach. Here's how the algorithm works:
53+
54+
1. Initialize a base case where if n equals 1, return "1".
55+
2. For any value of n greater than 1, recursively call the function countAndSay(n-1).
56+
3. Convert the result of countAndSay(n-1) into the count-and-say format.
57+
58+
### Solution Code
59+
60+
#### Python
61+
62+
```
63+
class Solution(object):
64+
def countAndSay(self, n):
65+
if n == 1:
66+
return "1"
67+
prev = self.countAndSay(n - 1)
68+
result = ""
69+
count = 1
70+
for i in range(len(prev)):
71+
if i + 1 < len(prev) and prev[i] == prev[i + 1]:
72+
count += 1
73+
else:
74+
result += str(count) + prev[i]
75+
count = 1
76+
return result
77+
```
78+
79+
#### C++
80+
81+
```
82+
class Solution {
83+
public:
84+
string countAndSay(int n) {
85+
if (n == 1)
86+
return "1";
87+
string prev = countAndSay(n - 1);
88+
string result = "";
89+
int count = 1;
90+
for (int i = 0; i < prev.length(); ++i) {
91+
if (i + 1 < prev.length() && prev[i] == prev[i + 1]) {
92+
count++;
93+
} else {
94+
result += to_string(count) + prev[i];
95+
count = 1;
96+
}
97+
}
98+
return result;
99+
}
100+
};
101+
```
102+
103+
#### Java
104+
105+
```
106+
class Solution {
107+
public String countAndSay(int n) {
108+
if (n == 1)
109+
return "1";
110+
String prev = countAndSay(n - 1);
111+
StringBuilder result = new StringBuilder();
112+
int count = 1;
113+
for (int i = 0; i < prev.length(); ++i) {
114+
if (i + 1 < prev.length() && prev.charAt(i) == prev.charAt(i + 1)) {
115+
count++;
116+
} else {
117+
result.append(count).append(prev.charAt(i));
118+
count = 1;
119+
}
120+
}
121+
return result.toString();
122+
}
123+
}
124+
```
125+
126+
### Conclusion
127+
128+
The count-and-say sequence is generated based on the previous sequence using run-length encoding. This problem can be efficiently solved using a recursive approach where each step generates the next sequence based on the previous one. The provided solution code effectively implements this recursive algorithm to generate the nth element of the count-and-say sequence.

0 commit comments

Comments
 (0)