forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexicographically-smallest-string-after-applying-operations.py
73 lines (67 loc) · 2.36 KB
/
lexicographically-smallest-string-after-applying-operations.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Time: O(100 * n^2) = O(n^2)
# Space: O(1)
class Solution(object):
def findLexSmallestString(self, s, a, b):
"""
:type s: str
:type a: int
:type b: int
:rtype: str
"""
def less(s, i, j):
for k in xrange(len(s)):
if s[(k+i)%len(s)] != s[(k+j)%len(s)]:
return s[(k+i)%len(s)] < s[(k+j)%len(s)]
return False
s = list(s)
result = s[:]
even = [False]*10
while not even[int(s[0])]: # at most O(10) times
even[int(s[0])] = True
odd = [False]*10
while not odd[int(s[1])]: # at most O(10) times
odd[int(s[1])] = True
best_rotate = 0
lookup = [False]*len(s)
i = b
while not lookup[i]: # find best rotation, at most O(n) times
lookup[i] = True
if less(s, i, best_rotate): # O(n) time
best_rotate = i
i = (i+b)%len(s)
result = min(result, s[best_rotate:] + s[:best_rotate])
for k in xrange(1, len(s), 2): # flip odd index
s[k] = str((int(s[k])+a) % 10)
if b%2: # if rotate length is odd, even index could be also flipped
for k in xrange(0, len(s), 2): # flip even index
s[k] = str((int(s[k])+a) % 10)
return "".join(result)
# Time: O(100 * n^2), at most O(100n) strings and each compare costs O(n)
# Space: O(n^2)
import collections
class Solution2(object):
def findLexSmallestString(self, s, a, b):
"""
:type s: str
:type a: int
:type b: int
:rtype: str
"""
q, lookup, result = collections.deque([s]), {s}, s
while q:
curr = q.popleft()
if curr < result:
result = curr
add_a = list(curr)
for i, c in enumerate(add_a):
if i%2:
add_a[i] = str((int(c)+a) % 10)
add_a = "".join(add_a)
if add_a not in lookup:
lookup.add(add_a);
q.append(add_a)
rotate_b = curr[b:] + curr[:b]
if rotate_b not in lookup:
lookup.add(rotate_b)
q.append(rotate_b)
return result