-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
minimum-number-of-moves-to-make-palindrome.py
69 lines (62 loc) · 1.65 KB
/
minimum-number-of-moves-to-make-palindrome.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
# Time: O(nlogn)
# Space: O(n)
class BIT(object): # 0-indexed
def __init__(self, n):
self.__bit = [0]*(n+1)
def add(self, i, val):
i += 1
while i < len(self.__bit):
self.__bit[i] += val
i += (i & -i)
def query(self, i):
i += 1
ret = 0
while i > 0:
ret += self.__bit[i]
i -= (i & -i)
return ret
# greedy, bit, fenwick tree
class Solution(object):
def minMovesToMakePalindrome(self, s):
"""
:type s: str
:rtype: int
"""
idxs = [[] for _ in xrange(26)]
for i, c in enumerate(s):
idxs[ord(c)-ord('a')].append(i)
targets, pairs = [0]*len(s), []
for c, idx in enumerate(idxs):
for i in xrange(len(idx)//2):
pairs.append((idx[i], idx[~i]))
if len(idx)%2:
targets[idx[len(idx)//2]] = len(s)//2
pairs.sort()
for i, (l, r) in enumerate(pairs):
targets[l], targets[r] = i, (len(s)-1)-i
bit = BIT(len(s))
result = 0
for i in targets:
result += i-bit.query(i-1) # move from bit.query(i-1) to i
bit.add(i, 1)
return result
# Time: O(n^2)
# Space: O(n)
# greedy
class Solution2(object):
def minMovesToMakePalindrome(self, s):
"""
:type s: str
:rtype: int
"""
s = list(s)
result = 0
while s:
i = s.index(s[-1])
if i == len(s)-1:
result += i//2
else:
result += i
s.pop(i)
s.pop()
return result