diff options
author | Jack Sangdahl <[email protected]> | 2024-09-19 22:11:19 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-09-19 22:11:27 -0600 |
commit | e1681d1ad79dd6a2961fe04d24c1ed2152da161a (patch) | |
tree | 381aa71d95e40dc922d2962e64eaf1118fdc6095 /shortest_palindrome.py | |
parent | 0e3a73d4a297707a2eba42ac8b1f4a04d63f1135 (diff) | |
download | leetcode-e1681d1ad79dd6a2961fe04d24c1ed2152da161a.tar.gz leetcode-e1681d1ad79dd6a2961fe04d24c1ed2152da161a.zip |
214. shortest palindrome
Diffstat (limited to 'shortest_palindrome.py')
-rw-r--r-- | shortest_palindrome.py | 25 |
1 files changed, 25 insertions, 0 deletions
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")) |