-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (71 loc) · 2.1 KB
/
main.go
File metadata and controls
77 lines (71 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Source: https://leetcode.com/problems/count-and-say
// Title: Count and Say
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// The **count-and-say** sequence is a sequence of digit strings defined by the recursive formula:
//
// - `countAndSay(1) = "1"`
// - `countAndSay(n)` is the run-length encoding of `countAndSay(n - 1)`.
//
// **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"`.
//
// Given a positive integer `n`, return the `n^th` element of the **count-and-say** sequence.
//
// **Example 1:**
//
// ```
// Input: n = 4
// Output: "1211"
// Explanation:
// countAndSay(1) = "1"
// countAndSay(2) = RLE of "1" = "11"
// countAndSay(3) = RLE of "11" = "21"
// countAndSay(4) = RLE of "21" = "1211"
// ```
//
// **Example 2:**
//
// ```
// Input: n = 1
// Output: "1"
// Explanation:
// This is the base case.
// ```
//
// **Constraints:**
//
// - `1 <= n <= 30`
//
// **Follow up:** Could you solve it iteratively?
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
import (
"strconv"
)
func countAndSay(n int) string {
rle := func(input string) string {
var res []byte
ch := input[0]
count := 1
for i := 1; i < len(input); i++ {
if input[i] == ch {
count++
} else {
res = append(res, []byte(strconv.Itoa(count))...)
res = append(res, ch)
ch = input[i]
count = 1
}
}
res = append(res, []byte(strconv.Itoa(count))...)
res = append(res, ch)
return string(res)
}
res := "1"
for i := 2; i <= n; i++ {
res = rle(res)
}
return res
}