Skip to content

Commit e720621

Browse files
authored
Create maximum-number-of-non-overlapping-palindrome-substrings.py
1 parent 7c75dbc commit e720621

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(n * k)
2+
# Space: O(1)
3+
4+
# greedy
5+
class Solution(object):
6+
def maxPalindromes(self, s, k):
7+
"""
8+
:type s: str
9+
:type k: int
10+
:rtype: int
11+
"""
12+
result = prev = 0
13+
for mid in xrange(2*len(s)-1):
14+
left, right = mid//2, mid//2+mid%2
15+
while left >= prev and right < len(s) and s[left] == s[right]:
16+
if right-left+1 >= k:
17+
prev = right+1
18+
result += 1
19+
break
20+
left, right = left-1, right+1
21+
return result

0 commit comments

Comments
 (0)