Skip to content

Commit bc249bf

Browse files
authored
Merge pull request 6boris#92 from kylesliu/develop
add 187 solution
2 parents 75c444f + a1c8cb4 commit bc249bf

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/kylesliu/awesome-golang-leetcode
22

33
require github.com/emirpasic/gods v1.12.0 // indirect
4+
5+
go 1.13
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [187. Repeated DNA Sequences][title]
2+
3+
## Description
4+
5+
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.
6+
7+
Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule.
8+
**Example 1:**
9+
10+
```
11+
Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
12+
13+
Output: ["AAAAACCCCC", "CCCCCAAAAA"]
14+
```
15+
16+
**Tags:** Math, String
17+
18+
## 题意
19+
> 所有DNA序列都可以用 A,C,G,T 四个字母表示,比如 "ACGAATTCCG",研究DNA序列时,有时识别重复子串是很有意义的。
20+
21+
请编写一个程序,找到所有长度为10的且出现次数多于1的子串。
22+
23+
## 题解
24+
25+
### 思路1
26+
> 用哈希表记录所有长度是10的子串的个数。
27+
从前往后扫描,当子串出现第二次时,将其记录在答案中。
28+
29+
时间复杂度分析:总共约 nn 个长度是10的子串,所以总共有 10n10n 个字符。计算量与字符数量成正比,所以时间复杂度是 O(n)O(n)。
30+
31+
```go
32+
33+
```
34+
35+
36+
## 结语
37+
38+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
39+
40+
[title]: https://leetcode.com/problems/repeated-dna-sequences/
41+
[me]: https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Solution
2+
3+
func findRepeatedDnaSequences(s string) []string {
4+
ans := make([]string, 0)
5+
if len(s) < 10 {
6+
return ans
7+
}
8+
9+
cache := make(map[string]int)
10+
for i := 0; i <= len(s)-10; i++ {
11+
curr := s[i : i+10]
12+
if cache[curr] == 1 {
13+
ans = append(ans, curr)
14+
}
15+
cache[curr] += 1
16+
}
17+
return ans
18+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
func TestSolution(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
inputs string
14+
expect []string
15+
}{
16+
{"TestCase", "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", []string{"AAAAACCCCC", "CCCCCAAAAA"}},
17+
}
18+
19+
// 开始测试
20+
for i, c := range cases {
21+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
22+
got := findRepeatedDnaSequences(c.inputs)
23+
if !reflect.DeepEqual(got, c.expect) {
24+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
25+
c.expect, got, c.inputs)
26+
}
27+
})
28+
}
29+
}
30+
31+
// 压力测试
32+
func BenchmarkSolution(b *testing.B) {
33+
34+
}
35+
36+
// 使用案列
37+
func ExampleSolution() {
38+
39+
}

0 commit comments

Comments
 (0)