-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path187.RepeatedDnaSequences.cs
63 lines (61 loc) · 1.69 KB
/
187.RepeatedDnaSequences.cs
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
using System.Collections.Generic;
public class Solution {
public IList<string> FindRepeatedDnaSequences(string s) {
var once = new HashSet<int>();
var moreThanOnce = new HashSet<int>();
int bits = 0;
for (var i = 0; i < s.Length; ++i)
{
bits <<= 2;
switch (s[i])
{
case 'A':
break;
case 'C':
bits |= 1;
break;
case 'G':
bits |= 2;
break;
case 'T':
bits |= 3;
break;
}
if (i >= 10)
{
bits &= 0xFFFFF;
}
if (i >= 9 && !once.Add(bits))
{
moreThanOnce.Add(bits);
}
}
var results = new List<string>();
foreach (var item in moreThanOnce)
{
var itemCopy = item;
var charArray = new char[10];
for (var i = 9; i >= 0; --i)
{
switch (itemCopy & 3)
{
case 0:
charArray[i] = 'A';
break;
case 1:
charArray[i] = 'C';
break;
case 2:
charArray[i] = 'G';
break;
case 3:
charArray[i] = 'T';
break;
}
itemCopy >>= 2;
}
results.Add(new string(charArray));
}
return results;
}
}