-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path187.cpp
100 lines (95 loc) · 2.62 KB
/
187.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// 187. Repeated DNA Sequences - https://leetcode.com/problems/repeated-dna-sequences
#include "bits/stdc++.h"
using namespace std;
//struct TrieNode {
// bool is_word = false;
// unordered_map<char, TrieNode*> children;
//};
//
//class Trie {
//private:
// TrieNode* root;
// void add_word(const string & str) {
// auto cur = root;
// for (char ch : str) {
// if (!cur->children.count(ch)) {
// cur->children[ch] = new TrieNode();
// }
// cur = cur->children[ch];
// }
// cur->is_word = true;
// }
// bool find_word(const string & str) {
// auto cur = root;
// for (char ch : str) {
// if (!cur->children.count(ch)) {
// return false;
// }
// cur = cur->children[ch];
// }
// return cur->is_word;
// }
//public:
// Trie() { root = new TrieNode(); }
// bool is_already_present(const string & str) {
// if (!find_word(str)) {
// add_word(str);
// return false;
// }
// return true;
// }
//};
//
//class Solution {
//private:
// Trie* trie;
//public:
// vector<string> findRepeatedDnaSequences(string s) {
// trie = new Trie();
// int n = (int)s.length();
// int len = 10;
// unordered_set<string> result;
// for (int start = 0; start + len - 1 < n; ++start) {
// string dna = s.substr(start, len);
// if (!result.count(dna) && trie->is_already_present(dna)) {
// result.insert(dna);
// }
// }
// // Trick.
// return {result.begin(), result.end()};
// }
//};
class Solution {
public:
vector<string> findRepeatedDnaSequences(string s) {
unordered_map<char, int> let_to_bits = {
{'A', 0},
{'C', 1},
{'G', 2},
{'T', 3},
};
unordered_set<int> int_words;
unordered_set<string> result;
int len = 10;
int n = (int)s.length();
for (int start = 0; start + len - 1 < n; ++start) {
string dna = s.substr(start, len);
if (result.count(dna)) { continue; }
int int_from_dna = 0;
for (char ch : dna) {
int_from_dna <<= 2;
int_from_dna |= let_to_bits[ch];
}
if (int_words.count(int_from_dna)) {
result.insert(dna);
} else {
int_words.insert(int_from_dna);
}
}
return {result.begin(), result.end()};
}
};
int main() {
ios::sync_with_stdio(false);
return 0;
}