-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[python]longest-palindromic-substring
- Loading branch information
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution: | ||
def longestPalindrome(self, s: str) -> str: | ||
res = s[0] | ||
for i in range(len(s)): | ||
for j in range(i + 1, len(s)): | ||
if j - i + 1 > len(res) and self.check(s, i, j): | ||
res = s[i : j + 1] | ||
return res | ||
|
||
def check(self, s, i, j): | ||
while i < j: | ||
if s[i] != s[j]: | ||
return False | ||
i += 1 | ||
j -= 1 | ||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
print(Solution().longestPalindrome("babad")) | ||
print(Solution().longestPalindrome("cbbd")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Solution: | ||
""" | ||
## P(i,j) | ||
1. i == j => true | ||
2. j - i <= 2 => s[i] == s[j] | ||
3. j -i > 2=> s[i] == s[j] && P(i+1,j-1) | ||
""" | ||
|
||
def longestPalindrome(self, s: str) -> str: | ||
res = s[0] | ||
length = len(s) | ||
arr = [[False for _ in range(length)] for _ in range(length)] | ||
for i in range(len(s) - 1, -1, -1): | ||
for j in range(i + 1, len(s)): | ||
if i == j: | ||
arr[i][j] = True | ||
elif j - i <= 2: | ||
arr[i][j] = s[i] == s[j] | ||
else: | ||
arr[i][j] = (s[i] == s[j]) and arr[i + 1][j - 1] | ||
if arr[i][j] and j - i + 1 > len(res): | ||
res = s[i : j + 1] | ||
return res | ||
|
||
|
||
if __name__ == "__main__": | ||
# print(Solution().longestPalindrome("babad")) | ||
# print(Solution().longestPalindrome("cbbd")) | ||
print(Solution().longestPalindrome("abcba")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution: | ||
def __init__(self): | ||
self.res = "" | ||
|
||
def longestPalindrome(self, s: str) -> str: | ||
self.res = s[0] | ||
|
||
for i in range(len(s)): | ||
self.work(s, i, i) | ||
self.work(s, i, i + 1) | ||
return self.res | ||
|
||
def work(self, s, left, right): | ||
while left >= 0 and right < len(s) and s[left] == s[right]: | ||
left -= 1 | ||
right += 1 | ||
if right - left - 1 > len(self.res): | ||
self.res = s[left + 1 : right] | ||
|
||
|
||
if __name__ == "__main__": | ||
print(Solution().longestPalindrome("babad")) | ||
print(Solution().longestPalindrome("cbbd")) |