From e1681d1ad79dd6a2961fe04d24c1ed2152da161a Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Thu, 19 Sep 2024 22:11:19 -0600 Subject: 214. shortest palindrome --- shortest_palindrome.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 shortest_palindrome.py (limited to 'shortest_palindrome.py') diff --git a/shortest_palindrome.py b/shortest_palindrome.py new file mode 100644 index 0000000..48f2306 --- /dev/null +++ b/shortest_palindrome.py @@ -0,0 +1,25 @@ +# 214. Shortest Palindrome + +""" +you are given a string 's'. you can convert 's' to a palindrome by adding +characters in front of it. return the shortest palindrome you can find by +performing this transformation. +""" + + +class Solution(object): + def shortestPalindrome(self, s): + """ + :type s: str + :rtype: str + """ + r = s[::-1] + for i in range(len(s) + 1): + if s.startswith(r[i:]): + return r[:i] + s + + +if __name__ == "__main__": + obj = Solution() + print(obj.shortestPalindrome("aacecaaa")) + print(obj.shortestPalindrome("abcd")) -- cgit v1.2.3