Skip to content

Commit 7ae1bbd

Browse files
committed
solve medium task leetcode
1 parent 467552b commit 7ae1bbd

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Given a string s, partition it into unique segments according to the following procedure:
3+
4+
Start building a segment beginning at index 0.
5+
Continue extending the current segment character by character until the current segment has not been seen before.
6+
Once the segment is unique, add it to your list of segments, mark it as seen, and begin a new segment from the next index.
7+
Repeat until you reach the end of s.
8+
9+
Return an array of strings segments, where segments[i] is the ith segment created.
10+
11+
Example 1:
12+
13+
Input: s = "abbccccd"
14+
Output: ["a","b","bc","c","cc","d"]
15+
16+
Example 2:
17+
18+
Input: s = "aaaa"
19+
Output: ["a","aa"]
20+
"""
21+
from typing import List
22+
23+
def partitionString(s: str) -> List[str]: # noqa
24+
set_ = set()
25+
dp = []
26+
27+
length = len(s)
28+
f_ind = 0
29+
s_ind = f_ind + 1
30+
while (f_ind < length) and (s_ind < length):
31+
val = s[f_ind:s_ind]
32+
if val in set_:
33+
s_ind += 1
34+
else:
35+
f_ind = s_ind
36+
s_ind = f_ind + 1
37+
set_.add(val)
38+
dp.append(val)
39+
return dp
40+
41+
if __name__ == "__main__":
42+
test = partitionString(s="aaaaa")
43+
print("ok")
44+

0 commit comments

Comments
 (0)