Skip to content

Commit 0dccaf5

Browse files
committed
Add solution 763
1 parent 89d44ea commit 0dccaf5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

763_PartitionLabels.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
extension Character {
2+
var ascii: Int {
3+
return Int(self.unicodeScalars.first!.value)
4+
}
5+
}
6+
class Solution {
7+
func partitionLabels(_ S: String) -> [Int] {
8+
// if S.isEmpty {
9+
// return [0]
10+
// }
11+
var map = Array(repeating: -1, count: 26+97)
12+
for (i, c) in S.enumerated() {
13+
map[c.ascii] = i
14+
}
15+
var result = [Int]()
16+
var start = 0, end = 0
17+
for (i, c) in S.enumerated() {
18+
let right = map[c.ascii]
19+
end = max(end, right)
20+
if i == end {
21+
result.append(end-start+1)
22+
start = end + 1
23+
}
24+
}
25+
return result
26+
}
27+
}

0 commit comments

Comments
 (0)