Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eb9c5c2

Browse files
added Longest Palindromic Substring (Medium)
1 parent 225ec0e commit eb9c5c2

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Longest Palindromic Substring
2+
3+
[Leetcode Link](https://leetcode.com/problems/longest-palindromic-substring/)
4+
5+
## Problem:
6+
7+
Given a string `s`, return the **longest** palindromic substring in `s`.
8+
9+
## Example:
10+
11+
```
12+
Input: s = "babad"
13+
Output: "bab"
14+
Note: "aba" is also a valid answer.
15+
```
16+
17+
```
18+
Input: s = "cbbd"
19+
Output: "bb"
20+
```
21+
22+
```
23+
Input: s = "a"
24+
Output: "a"
25+
```
26+
27+
```
28+
Input: s = "ac"
29+
Output: "a"
30+
```
31+
32+
## Note:
33+
34+
- 1 <= s.length <= 1000
35+
- s consist of only digits and English letters (lower-case and/or upper-case),
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
class Solution:
3+
def longestPalindrome(self, s: str) -> str:
4+
N = len(s)
5+
longestLength = 0
6+
left = 0
7+
right = 0
8+
# create N x N dynamic programming table and init with setting i==j true
9+
# because a word of length 1 is always palindromic
10+
dp = [[False for n in range(N)] for n in range(N)]
11+
for i in range(N):
12+
dp[i][i] = True
13+
# print(dp)
14+
# bottom-up fill-in
15+
# if length of 2 -> palindromic iff two letters are same
16+
# if longer length -> palindromic iff start and end letters are same AND
17+
# inner word is palindromic (check for [i+1][j-1])
18+
for i in range(N-2, -1, -1):
19+
for j in range(N-1, i-1, -1):
20+
if i == j-1:
21+
if s[i] == s[j]:
22+
dp[i][j] = s[i] == s[j]
23+
else:
24+
if s[i] == s[j] and dp[i+1][j-1]:
25+
dp[i][j] = True
26+
if dp[i][j]:
27+
if j-i+1 > longestLength:
28+
# new longest palindromic substring, so update
29+
longestLength = j-i+1
30+
left = i
31+
right = j
32+
break
33+
print(dp)
34+
return s[left:right+1]
35+
36+
37+
sol = Solution()
38+
s = "babad"
39+
print(sol.longestPalindrome(s))

‎README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Languages used: Java and Python
4747
- [Merge Two Binary Trees](Easy/MergeTwoBinaryTrees)
4848
- [Invert Binary Tree](Easy/InvertBinaryTree)
4949
- Medium
50+
5051
- [Minimum Add to Make Parentheses Valid](Medium/MinimumAddtoMakeParenthesesValid)
5152
- [Distribute Coins in Binary Tree](Medium/DistributeCoinsInBinaryTree)
5253
- [Find Minimum Number of Fibonacci Numbers Whose Sum is K](Medium/FindMinNumFibNumSumK)
@@ -96,6 +97,8 @@ Languages used: Java and Python
9697
- [Permutations](Medium/Permutations)
9798
- [Permutations II](Medium/Permutations2)
9899
- [Combinations](Medium/Combinations)
100+
- [Longest Palindromic Substring](Medium/LongestPalindromicSubstring)
101+
99102
- Hard
100103

101104
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /