Skip to content

Commit 9c1c326

Browse files
author
Partho Biswas
committed
763_Partition_Labels
1 parent 0e0cce5 commit 9c1c326

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Foundation
2+
class Solution {
3+
func partitionLabels(_ S: String) -> [Int] {
4+
guard S.count > 0 else {
5+
return []
6+
}
7+
var counter = [Character:Int]()
8+
let sArr = Array(S)
9+
sArr.forEach { char in
10+
counter[char, default: 0] += 1
11+
}
12+
13+
var result = [Int]()
14+
var (left, right) = (0, 0)
15+
var windowCharSet = Set<Character>()
16+
while right < sArr.count {
17+
let currentChar = sArr[right]
18+
windowCharSet.insert(currentChar)
19+
counter[currentChar] = counter[currentChar]! - 1
20+
21+
if counter[currentChar] == 0 {
22+
counter.removeValue(forKey: currentChar)
23+
windowCharSet.remove(currentChar)
24+
}
25+
26+
if windowCharSet.count <= 0 {
27+
result.append(right - left + 1)
28+
left = right + 1
29+
}
30+
right += 1
31+
}
32+
return result
33+
}
34+
}

0 commit comments

Comments
 (0)