Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 138ef72

Browse files
committed
feat: update solutions to lc problem: No.0187
No.0187.Repeated DNA Sequences
1 parent 7f17820 commit 138ef72

File tree

3 files changed

+86
-19
lines changed

3 files changed

+86
-19
lines changed

‎solution/0100-0199/0187.Repeated DNA Sequences/README.md‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,17 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48-
**朴素解法:**
48+
**方法一:哈希表**
4949

50-
使用哈希表,记录所有连续长度为 10 的子字符串出现次数(字符串为 Key,次数为 Value),当出现一次以上时,将其加入返回列表当中。
50+
朴素解法,用哈希表保存所有长度为 10 的子序列出现的次数,当子序列出现次数大于 1 时,把该子序列作为结果之一。
51+
52+
假设字符串 `s` 长度为 `n`,则时间复杂度 $O(n \times 10),ドル空间复杂度 $O(n)$。
53+
54+
**方法二:Rabin-Karp 字符串匹配算法**
55+
56+
本质上是滑动窗口和哈希的结合方法,和 [0028.找出字符串中第一个匹配项的下标](https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string/) 类似,本题可以借助哈希函数将子序列计数的时间复杂度降低到 $O(1)$。
57+
58+
假设字符串 `s` 长度为 `n`,则时间复杂度为 $O(n),ドル空间复杂度 $O(n)$。
5159

5260
<!-- tabs:start -->
5361

@@ -115,12 +123,12 @@ var findRepeatedDnaSequences = function (s) {
115123

116124
### **Go**
117125

126+
哈希表:
127+
118128
```go
119129
func findRepeatedDnaSequences(s string) []string {
120-
cnt := make(map[string]int)
121-
n := len(s) - 10
122-
ans := make([]string, 0)
123-
for i := 0; i <= n; i++ {
130+
ans, cnt := []string{}, map[string]int{}
131+
for i := 0; i <= len(s)-10; i++ {
124132
sub := s[i : i+10]
125133
cnt[sub]++
126134
if cnt[sub] == 2 {
@@ -131,6 +139,29 @@ func findRepeatedDnaSequences(s string) []string {
131139
}
132140
```
133141

142+
Rabin-Karp:
143+
144+
```go
145+
func findRepeatedDnaSequences(s string) []string {
146+
hashCode := map[byte]int{'A': 0, 'C': 1, 'G': 2, 'T': 3}
147+
ans, cnt, left, right := []string{}, map[int]int{}, 0, 0
148+
149+
sha, multi := 0, int(math.Pow(4, 9))
150+
for ; right < len(s); right++ {
151+
sha = sha*4 + hashCode[s[right]]
152+
if right-left+1 < 10 {
153+
continue
154+
}
155+
cnt[sha]++
156+
if cnt[sha] == 2 {
157+
ans = append(ans, s[left:right+1])
158+
}
159+
sha, left = sha-multi*hashCode[s[left]], left+1
160+
}
161+
return ans
162+
}
163+
```
164+
134165
### **C++**
135166

136167
```cpp

‎solution/0100-0199/0187.Repeated DNA Sequences/README_EN.md‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232

3333
## Solutions
3434

35+
**Approach 1: HashTable**
36+
37+
Time complexity $O(n \times 10),ドル Space complexity $O(n)$.
38+
39+
**Approach 2: Rabin-Karp**
40+
41+
Time complexity $O(n),ドル Space complexity $O(n)$.
42+
3543
<!-- tabs:start -->
3644

3745
### **Python3**
@@ -94,12 +102,12 @@ var findRepeatedDnaSequences = function (s) {
94102

95103
### **Go**
96104

105+
HashTable:
106+
97107
```go
98108
func findRepeatedDnaSequences(s string) []string {
99-
cnt := make(map[string]int)
100-
n := len(s) - 10
101-
ans := make([]string, 0)
102-
for i := 0; i <= n; i++ {
109+
ans, cnt := []string{}, map[string]int{}
110+
for i := 0; i <= len(s)-10; i++ {
103111
sub := s[i : i+10]
104112
cnt[sub]++
105113
if cnt[sub] == 2 {
@@ -110,6 +118,29 @@ func findRepeatedDnaSequences(s string) []string {
110118
}
111119
```
112120

121+
Rabin-Karp:
122+
123+
```go
124+
func findRepeatedDnaSequences(s string) []string {
125+
hashCode := map[byte]int{'A': 0, 'C': 1, 'G': 2, 'T': 3}
126+
ans, cnt, left, right := []string{}, map[int]int{}, 0, 0
127+
128+
sha, multi := 0, int(math.Pow(4, 9))
129+
for ; right < len(s); right++ {
130+
sha = sha*4 + hashCode[s[right]]
131+
if right-left+1 < 10 {
132+
continue
133+
}
134+
cnt[sha]++
135+
if cnt[sha] == 2 {
136+
ans = append(ans, s[left:right+1])
137+
}
138+
sha, left = sha-multi*hashCode[s[left]], left+1
139+
}
140+
return ans
141+
}
142+
```
143+
113144
### **C++**
114145

115146
```cpp
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
func findRepeatedDnaSequences(s string) []string {
2-
cnt := make(map[string]int)
3-
n:= len(s) -10
4-
ans:=make([]string, 0)
5-
fori := 0; i<=n; i++ {
6-
sub:=s[i : i+10]
7-
cnt[sub]++
8-
if cnt[sub] ==2 {
9-
ans=append(ans, sub)
2+
hashCode := map[byte]int{'A': 0, 'C': 1, 'G': 2, 'T': 3}
3+
ans, cnt, left, right:= []string{}, map[int]int{}, 0, 0
4+
5+
sha, multi := 0, int(math.Pow(4, 9))
6+
for ; right<len(s); right++ {
7+
sha=sha*4+hashCode[s[right]]
8+
if right-left+1<10 {
9+
continue
1010
}
11+
cnt[sha]++
12+
if cnt[sha] == 2 {
13+
ans = append(ans, s[left:right+1])
14+
}
15+
sha, left = sha-multi*hashCode[s[left]], left+1
1116
}
1217
return ans
13-
}
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /