-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7aaf014
commit e5bc6a0
Showing
3 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import List | ||
|
||
|
||
# O(n) time || O(n) space | ||
def partition_labels(self, s: str) -> List[int]: | ||
last_occurrence = {c: i for i, c in enumerate(s)} | ||
res = [] | ||
low = high = 0 | ||
for i, c in enumerate(s): | ||
high = max(high, last_occurrence[c]) | ||
|
||
if i == high: | ||
res.append(high - low + 1) | ||
low = high + 1 | ||
|
||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import unittest | ||
|
||
from arrays.PartitionLabels import partition_labels | ||
|
||
|
||
class MyTestCase(unittest.TestCase): | ||
def test_partition_labels(self): | ||
self.assertEqual([9, 7, 8], partition_labels(self, s="ababcbacadefegdehijhklij")) | ||
|
||
def test_partition_labels_1(self): | ||
self.assertEqual([10], partition_labels(self, s="eccbbbbdec")) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |