aboutsummaryrefslogtreecommitdiff
path: root/shortest_palindrome.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-09-19 22:11:19 -0600
committerJack Sangdahl <[email protected]>2024-09-19 22:11:27 -0600
commite1681d1ad79dd6a2961fe04d24c1ed2152da161a (patch)
tree381aa71d95e40dc922d2962e64eaf1118fdc6095 /shortest_palindrome.py
parent0e3a73d4a297707a2eba42ac8b1f4a04d63f1135 (diff)
downloadleetcode-e1681d1ad79dd6a2961fe04d24c1ed2152da161a.tar.gz
leetcode-e1681d1ad79dd6a2961fe04d24c1ed2152da161a.zip
214. shortest palindrome
Diffstat (limited to 'shortest_palindrome.py')
-rw-r--r--shortest_palindrome.py25
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"))