-
Notifications
You must be signed in to change notification settings - Fork 14
/
PartitionLabels_763.swift
50 lines (44 loc) · 1.21 KB
/
PartitionLabels_763.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
//
// PartitionLabels_763.swift
// LeetCode_Swift
//
// Created by Huni on 28/02/2018.
// Copyright © 2018 KnighhtJoker. All rights reserved.
//
import Foundation
class PartitionLabels_763 {
func partitionLabels(_ S: String) -> [Int] {
var hash = Array(repeating: (-1, -1), count: 26)
for (i, char) in S.unicodeScalars.enumerated() {
let n = Int(char.value) - 97
if hash[n].0 == -1 {
hash[n] = (i, i)
} else {
hash[n].1 = i
}
}
var index = 0
while index < hash.count {
if hash[index].0 == -1 {
hash.remove(at: index)
} else {
index += 1
}
}
hash.sort{ $0.0 < $1.0 }
var ans = [Int]()
var maxIndex = 0
var minIndex = 0
for (start, end) in hash {
if start > maxIndex {
ans.append(maxIndex - minIndex + 1)
maxIndex = end
minIndex = start
} else {
maxIndex = max(maxIndex, end)
}
}
ans.append(maxIndex - minIndex + 1)
return ans
}
}