Last active
June 7, 2016 20:38
-
-
Save jayceekay/dae8f83ad7255f2a08d3c86d72e052c7 to your computer and use it in GitHub Desktop.
find min number of splits such that every subword is a palindrome
This file contains hidden or 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
def is_palindrome(word): | |
# should round down | |
limit = len(word) / 2 | |
for i in xrange(limit): | |
if word[i] != word[limit - i - 1]: | |
return False | |
return True | |
def find_splits(word): | |
if is_palindrome(word): | |
return 0 | |
count = 0 | |
best_count = 9223372036854775807 | |
for i in xrange(1, len(word)): | |
count = count + find_splits(word[0:i]) + find_splits(word[i:]) + 1 | |
if count < best_count: | |
best_count = count | |
return best_count | |
print find_splits("aaba") | |
print find_splits("abcdef") | |
print find_splits("racecar") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment