Skip to content

Commit 95a6cd7

Browse files
committed
Solution added.
1 parent 96987a9 commit 95a6cd7

File tree

1 file changed

+32
-0
lines changed
  • 30 Days September Challange/Week 1/4. Partition Labels

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
A string S of lowercase English letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts.
3+
4+
5+
6+
Example 1:
7+
8+
Input: S = "ababcbacadefegdehijhklij"
9+
Output: [9,7,8]
10+
Explanation:
11+
The partition is "ababcbaca", "defegde", "hijhklij".
12+
This is a partition so that each letter appears in at most one part.
13+
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits S into less parts.
14+
15+
16+
Note:
17+
18+
S will have length in range [1, 500].
19+
S will consist of lowercase English letters ('a' to 'z') only.
20+
"""
21+
class Solution:
22+
def partitionLabels(self, S: str) -> List[int]:
23+
hmap = {}
24+
for i, char in enumerate(S):
25+
hmap[char] = i
26+
start, end, result = 0, 0, []
27+
for i, char in enumerate(S):
28+
end = max(end, hmap[char])
29+
if i == end:
30+
result.append(i - start + 1)
31+
start = i + 1
32+
return result

0 commit comments

Comments
 (0)