forked from spring1843/go-dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlook_and_tell.go
53 lines (46 loc) · 853 Bytes
/
look_and_tell.go
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
package strings
import (
"strconv"
)
// LookAndTell solves the problem in O(n) time and O(n) space.
func LookAndTell(depth int) []string {
output := []string{"1"}
if depth <= 0 {
return []string{"-1"}
}
if depth == 1 {
return output
}
for i := 1; i < depth; i++ {
output = append(output, tell(output[i-1]))
}
return output
}
func tell(n string) string {
var step, what string
var count, until int
for until < len(n) {
count, what, until = findHowMany(n, until)
step += strconv.Itoa(count) + what
}
return step
}
func findHowMany(n string, until int) (int, string, int) {
last := ""
count := 0
for until < len(n) {
char := n[until : until+1]
if last == "" {
last = char
count++
until++
continue
}
if char != last {
return count, last, until
}
count++
until++
}
return count, last, until
}