We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent bbead9b commit 1855074Copy full SHA for 1855074
Leetcode_30day_challenge/Partition_Labels.py
@@ -0,0 +1,28 @@
1
+# Time: O(N) and Space: O(N)
2
+
3
+class Solution:
4
+ def partitionLabels(self, s: str) -> List[int]:
5
+ d = {}
6
+ # Store the first and last index of each character.
7
+ for i,v in enumerate(s):
8
+ if v not in d:
9
+ d[v] = [i,i]
10
+ else:
11
+ d[v][1] = i
12
13
14
15
+ # Now the problem is same as merge intervals.
16
+ temp = list(d.values())
17
+ st = [temp[0]]
18
+ for item in temp[1:]:
19
+ top = st[-1]
20
+ if item[0] <= top[1]:
21
+ top[1] = max(top[1], item[1])
22
23
24
+ st.append(item)
25
26
+ # Calculating the answer
27
+ ans = [x[1]-x[0]+1 for x in st]
28
+ return ans
0 commit comments