-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path187.swift
57 lines (51 loc) · 1.51 KB
/
187.swift
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
// 因为解空间比较小,用 trie 有点用力过猛了
// 直接用集合(也可以用字典,可以省掉一个Set)
class Solution {
func findRepeatedDnaSequences(_ s: String) -> [String] {
let l = Array(s)
guard l.count >= 10 else { return [] }
var found = Set<String>()
var res = Set<String>()
for i in 0...(l.count - 10) {
let str = String(l[i..<(i+10)])
let (inserted, _) = found.insert(str)
if !inserted {
res.insert(str)
}
}
return Array(res)
}
}
// trie 慢
class Solution1 {
class Node {
let val: Character
var children = [Node]()
init(_ val: Character) {
self.val = val
}
}
func findRepeatedDnaSequences(_ s: String) -> [String] {
let l = Array(s)
var res = Set<String>()
guard l.count >= 10 else { return [String]() }
let rt = Node("R")
for i in 0...(l.count-10) {
var match = true
var cur = rt
for j in i..<(i+10) {
var next: Node? = cur.children.first { $0.val == l[j] }
if next == nil {
next = Node(l[j])
cur.children.append(next!)
match = false
}
cur = next!
}
if match {
res.insert(String(l[i..<(i+10)]))
}
}
return Array(res)
}
}