File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
leetcode.com/swift/DS_ALGO_PRAC.playground/Sources/swift Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments