Skip to content

Commit 070d74f

Browse files
committed
Count and say.
1 parent 49d5929 commit 070d74f

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
3+
4+
countAndSay(1) = "1"
5+
countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1), which is then converted into a different digit string.
6+
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.
7+
8+
For example, the saying and conversion for digit string "3322251":
9+
10+
11+
Given a positive integer n, return the nth term of the count-and-say sequence.
12+
13+
 
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+
30+
Constraints:
31+
- 1 <= n <= 30
32+
*/
33+
class Solution {
34+
func countAndSay(_ n: Int) -> String {
35+
if n == 1 { return "1" }
36+
var sumArr = ["1"]
37+
var count = 0
38+
var number = Character("1")
39+
for i in 0..<n {
40+
if i > 0 {
41+
let preElement = sumArr[i - 1]
42+
var element = ""
43+
for (i, c) in preElement.enumerated() {
44+
if i == 0 {
45+
number = c
46+
count += 1
47+
} else {
48+
let preCharacter = preElement[preElement.index(preElement.startIndex, offsetBy: i - 1)]
49+
if preCharacter == c {
50+
count += 1
51+
} else {
52+
element.append("\(count)\(number)")
53+
number = c
54+
count = 1
55+
}
56+
}
57+
if i == preElement.count - 1 {
58+
element.append("\(count)\(number)")
59+
}
60+
}
61+
count = 0
62+
sumArr.append(element)
63+
}
64+
}
65+
return sumArr.last!
66+
}
67+
}
68+
69+
let s = Solution()
70+
let r = s.countAndSay(30)
71+
print(r)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/38.Count and Say.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,4 @@
131131
16. [Find First and Last Position of Element in Sorted Array](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/34.Find%20First%20and%20Last%20Position%20of%20Element%20in%20Sorted%20Array.playground/Contents.swift)
132132
17. [Valid Sudoku](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/36.Valid%20Sudoku.playground/Contents.swift)
133133
18. [Maximum Subarray](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/53.Maximum%20Subarray.playground/Contents.swift)
134+
19. [Count and Say](https://github.com/recherst/leetcode-algtorithm/blob/main/Medium/38.Count%20and%20Say.playground/Contents.swift)

0 commit comments

Comments
 (0)