Skip to content

Commit 85abd8d

Browse files
committed
Count and Say
1 parent 60ee70e commit 85abd8d

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

DSA Crack Sheet/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
- [Print all the duplicates in the input string](https://www.geeksforgeeks.org/print-all-the-duplicates-in-the-input-string/ "view topic")
6161
- [Why strings are immutable in Java?](https://javarevisited.blogspot.com/2010/10/why-string-is-immutable-or-final-in-java.html#:~:text=The%20string%20is%20Immutable%20in,cached%20in%20the%20String%20pool.&text=Since%20Strings%20are%20very%20popular,which%20was%20stored%20in%20HashMap. "view topic")
6262
- [Check if strings are rotations of each other or not](https://practice.geeksforgeeks.org/problems/check-if-strings-are-rotations-of-each-other-or-not-1587115620/1# "view question") - [Cpp Solution](./solutions/Check%20if%20strings%20are%20rotations%20of%20each%20other%20or%20not.cpp)
63-
- [Java Program to Check if a string is a valid shuffle of two distinct strings](https://www.programiz.com/java-programming/examples/check-valid-shuffle-of-strings "view topic")
63+
- [Program to Check if a string is a valid shuffle of two distinct strings](https://www.programiz.com/java-programming/examples/check-valid-shuffle-of-strings "view topic")
64+
- [Count and Say](https://leetcode.com/problems/count-and-say/ "view question") - [Cpp Solution](./solutions/Count%20and%20Say.cpp)
6465
- []( "view question") - [Cpp Solution](./solutions/.cpp)
6566

6667
### Searching & Sorting
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Count and Say
3+
=============
4+
5+
The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
6+
7+
countAndSay(1) = "1"
8+
countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1), which is then converted into a different digit string.
9+
To determine how you "say" a digit string, split it into the minimal number of groups so that each group is a contiguous section all of the same character. Then for each group, say the number of characters, then say the character. To convert the saying into a digit string, replace the counts with a number and concatenate every saying.
10+
11+
For example, the saying and conversion for digit string "3322251":
12+
13+
Given a positive integer n, return the nth term of the count-and-say sequence.
14+
15+
Example 1:
16+
Input: n = 1
17+
Output: "1"
18+
Explanation: This is the base case.
19+
20+
Example 2:
21+
Input: n = 4
22+
Output: "1211"
23+
Explanation:
24+
countAndSay(1) = "1"
25+
countAndSay(2) = say "1" = one 1 = "11"
26+
countAndSay(3) = say "11" = two 1's = "21"
27+
countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211"
28+
29+
Constraints:
30+
1 <= n <= 30
31+
*/
32+
33+
class Solution
34+
{
35+
public:
36+
string countAndSay(int n)
37+
{
38+
if (n == 1)
39+
return "1";
40+
auto prev_ans = countAndSay(n - 1);
41+
42+
string ans = "";
43+
int N = prev_ans.size(), count = 1;
44+
45+
for (int i = 0; i < N; ++i)
46+
{
47+
if (i == N - 1)
48+
{
49+
ans += to_string(count);
50+
ans += prev_ans[i];
51+
}
52+
else if (prev_ans[i] == prev_ans[i + 1])
53+
count++;
54+
else
55+
{
56+
ans += to_string(count);
57+
ans += prev_ans[i];
58+
count = 1;
59+
}
60+
}
61+
62+
return ans;
63+
}
64+
};

0 commit comments

Comments
 (0)