From 93cd6700c86a3982321e9939627e7895afecfab6 Mon Sep 17 00:00:00 2001 From: Rishabhraghwendra18 Date: Fri, 1 Oct 2021 22:09:08 +0530 Subject: [PATCH 1/2] added Longest Palindromic Substring code in python --- 0001/longest_palindromic_substring.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 0001/longest_palindromic_substring.py diff --git a/0001/longest_palindromic_substring.py b/0001/longest_palindromic_substring.py new file mode 100644 index 0000000..cb35176 --- /dev/null +++ b/0001/longest_palindromic_substring.py @@ -0,0 +1,19 @@ +def longestPalindrome(self, s): + res = "" + for i in xrange(len(s)): + # odd case, like "aba" + tmp = self.helper(s, i, i) + if len(tmp) > len(res): + res = tmp + # even case, like "abba" + tmp = self.helper(s, i, i+1) + if len(tmp) > len(res): + res = tmp + return res + +# get the longest palindrome, l, r are the middle indexes +# from inner to outer +def helper(self, s, l, r): + while l >= 0 and r < len(s) and s[l] == s[r]: + l -= 1; r += 1 + return s[l+1:r] \ No newline at end of file From fdc7db7e7867a24aed98f5b968d01c0164f47615 Mon Sep 17 00:00:00 2001 From: Rishabhraghwendra18 Date: Fri, 1 Oct 2021 22:12:52 +0530 Subject: [PATCH 2/2] added longest palindromic substring folder --- 0005/README.md | 4 ++++ {0001 => 0005}/longest_palindromic_substring.py | 0 2 files changed, 4 insertions(+) create mode 100644 0005/README.md rename {0001 => 0005}/longest_palindromic_substring.py (100%) diff --git a/0005/README.md b/0005/README.md new file mode 100644 index 0000000..a403c7e --- /dev/null +++ b/0005/README.md @@ -0,0 +1,4 @@ +# Longest Palindromic Substring + +### Problem statemen +Given a string s, return the longest palindromic substring in s. \ No newline at end of file diff --git a/0001/longest_palindromic_substring.py b/0005/longest_palindromic_substring.py similarity index 100% rename from 0001/longest_palindromic_substring.py rename to 0005/longest_palindromic_substring.py