Skip to content

Instantly share code, notes, and snippets.

@jayceekay
Last active June 7, 2016 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayceekay/dae8f83ad7255f2a08d3c86d72e052c7 to your computer and use it in GitHub Desktop.
Save jayceekay/dae8f83ad7255f2a08d3c86d72e052c7 to your computer and use it in GitHub Desktop.
find min number of splits such that every subword is a palindrome
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