-
Notifications
You must be signed in to change notification settings - Fork 0
/
187.repeated-dna-sequences.go
80 lines (71 loc) · 1.62 KB
/
187.repeated-dna-sequences.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* @lc app=leetcode id=187 lang=golang
*
* [187] Repeated DNA Sequences
*
* https://leetcode.com/problems/repeated-dna-sequences/description/
*
* algorithms
* Medium (38.13%)
* Likes: 643
* Dislikes: 248
* Total Accepted: 152.6K
* Total Submissions: 400.3K
* Testcase Example: '"AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"'
*
* 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.
*
* Write a function to find all the 10-letter-long sequences (substrings) that
* occur more than once in a DNA molecule.
*
* Example:
*
*
* Input: s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
*
* Output: ["AAAAACCCCC", "CCCCCAAAAA"]
*
*
*/
// @lc code=start
func findRepeatedDnaSequences(s string) []string {
return solutionWithBitAndHash(s)
}
//位操作结合哈希
//bit with hash table
func solutionWithBitAndHash(s string) []string {
charMap := make(map[string]int, 0)
charMap["A"] = 0
charMap["C"] = 1
charMap["G"] = 2
charMap["T"] = 3
bitStrMap := make(map[int]int, 0)
strList := make([]string, 0)
strMap := make(map[int]string)
for i := 0; i < len(s)-9; i++ {
bitStr := 0
val := 0
for j := i; j < i+10; j++ {
bitStr <<= 2
bitStr = bitStr | charMap[string(s[j])]
bitStr = bitStr & 0xffffffff
}
val, ok := bitStrMap[bitStr]
if ok {
val++
bitStrMap[bitStr] = val
} else {
bitStrMap[bitStr] = 1
}
if val > 1 {
strMap[bitStr] = s[i : i+10]
}
}
for _, v := range strMap {
strList = append(strList, v)
}
return strList
}
// @lc code=end