forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
last-substring-in-lexicographical-order.py
59 lines (53 loc) · 1.58 KB
/
last-substring-in-lexicographical-order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Time: O(n)
# Space: O(1)
class Solution(object):
def lastSubstring(self, s):
"""
:type s: str
:rtype: str
"""
left, right, l = 0, 1, 0
while right+l < len(s):
if s[left+l] == s[right+l]:
l += 1
continue
if s[left+l] > s[right+l]:
right += l+1
else:
left = max(right, left+l+1)
right = left+1
l = 0
return s[left:]
# Time: O(n)
# Space: O(n)
import collections
class Solution2(object):
def lastSubstring(self, s):
"""
:type s: str
:rtype: str
"""
count = collections.defaultdict(list)
for i in xrange(len(s)):
count[s[i]].append(i)
max_c = max(count.iterkeys())
starts = {}
for i in count[max_c]:
starts[i] = i+1
while len(starts)-1 > 0:
lookup = set()
next_count = collections.defaultdict(list)
for start, end in starts.iteritems():
if end == len(s): # finished
lookup.add(start)
continue
next_count[s[end]].append(start)
if end in starts: # overlapped
lookup.add(end)
next_starts = {}
max_c = max(next_count.iterkeys())
for start in next_count[max_c]:
if start not in lookup:
next_starts[start] = starts[start]+1
starts = next_starts
return s[next(starts.iterkeys()):]