-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path5.3.swift
49 lines (38 loc) · 1.19 KB
/
5.3.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
public func solution(inout S : String, inout _ P : [Int], inout _ Q : [Int]) -> [Int] {
// write your code in Swift 2.2
let N = S.characters.count
let M = P.count
var result: [Int] = Array(count: M, repeatedValue: 0)
var a: [Int] = Array(count: N + 1, repeatedValue: 0)
var c: [Int] = Array(count: N + 1, repeatedValue: 0)
var g: [Int] = Array(count: N + 1, repeatedValue: 0)
var t: [Int] = Array(count: N + 1, repeatedValue: 0)
var s = S.characters.map {String($0)}
for i in 0..<N {
switch s[i] {
case "A": a[i + 1] = 1
case "C": c[i + 1] = 1
case "G": g[i + 1] = 1
case "T": t[i + 1] = 1
default: break
}
}
for i in 1...N {
a[i] += a[i - 1]
c[i] += c[i - 1]
g[i] += g[i - 1]
t[i] += t[i - 1]
}
for i in 0..<M {
if (a[Q[i] + 1] - a[P[i]]) != 0 {
result[i] = 1
} else if (c[Q[i] + 1] - c[P[i]]) != 0 {
result[i] = 2
} else if (g[Q[i] + 1] - g[P[i]]) != 0 {
result[i] = 3
} else if (t[Q[i] + 1] - t[P[i]]) != 0 {
result[i] = 4
}
}
return result
}