Skip to content

Instantly share code, notes, and snippets.

@mvallebr
Created June 15, 2017 18:28
Show Gist options
  • Select an option

  • Save mvallebr/7214f7a9958949f7e478734aacd6950a to your computer and use it in GitHub Desktop.

Select an option

Save mvallebr/7214f7a9958949f7e478734aacd6950a to your computer and use it in GitHub Desktop.
"""
# Expecto Palindronum
Memory Limit:64 MB
Time Limit: 5 s
A palindrome is a word that reads the same backward and forward.
Given a string S, you are allowed to convert it to a palindrome by adding 0 or more characters in front of it.
Find the length of the shortest palindrome that you can create from S by applying the above transformation.
## Input Specifications
Your program will take
A string S ( 1 ≤ Length(S) ≤ 100) where each character of S will be
a lowercase alphabet (Between 'a' to 'z')
## Output Specifications
For each input, print out an integer L denoting the length of the shortest palindrome you can
generate from S.
## Sample Input/Output
INPUT
baaa
OUTPUT
7
EXPLANATION
The shortest palindrome you can construct from 'baaa' is 'aaabaaa'.
"""
def find_shortest_palindrome(original):
s = ""
t = ""
start = 0
for end in range(len(original)-1, 0, -1):
if original[start] == original[end]:
t += original[start]
start += 1
else:
s += t + original[end]
start = 0
t = ""
return s + original
print (find_shortest_palindrome("abc"))
print (find_shortest_palindrome("aabc"))
print (find_shortest_palindrome("aaaaabc"))
"""
cbabc
cbaabc
cbaaaaabc
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment