Skip to content

Instantly share code, notes, and snippets.

@DaniloOliveira28
Last active June 19, 2016 21:18
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 DaniloOliveira28/9957a3300d85106dbe09af09434befa3 to your computer and use it in GitHub Desktop.
Save DaniloOliveira28/9957a3300d85106dbe09af09434befa3 to your computer and use it in GitHub Desktop.
def is_palindrome(palavra):
return palavra[::-1] == palavra
def palindrome(texto):
if len(texto) <= 1:
return 0
res_pal = is_palindrome(texto)
if res_pal is True:
return 0
M = dict()
M[0] = 0
for j in range(1, len(texto)):
values = []
if is_palindrome(texto[0: j + 1]) is True:
M[j] = 0
else:
for i in range(0, j):
if is_palindrome(texto[i + 1: j + 1]) is True:
values.append(M[i] + 1)
M[j] = min(values)
return M[len(texto) - 1]
if __name__ == "__main__":
print palindrome("abbadanilo")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment