Skip to content

Commit e252c61

Browse files
Longest Palindromic Substring
1 parent 40bb31d commit e252c61

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Longest Palindromic Substring
2+
3+
## LeetCode Problem
4+
- Problem Number: 5
5+
- Problem Link: https://leetcode.com/problems/longest-palindromic-substring/
6+
- Difficulty: Medium
7+
- Topics: String, Dynamic Programming
8+
9+
## Problem Description
10+
Given a string s, return the longest palindromic substring in s.
11+
12+
Note:
13+
- A palindrome is a string that reads the same forward and backward
14+
- A substring is a contiguous sequence of characters within the string
15+
16+
## Examples
17+
18+
### Example 1:
19+
```
20+
Input: s = "babad"
21+
Output: "bab"
22+
Explanation: "aba" is also a valid answer.
23+
```
24+
25+
### Example 2:
26+
```
27+
Input: s = "cbbd"
28+
Output: "bb"
29+
```
30+
31+
## Constraints
32+
- 1 <= s.length <= 1000
33+
- s consists of only digits and English letters
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public String longestPalindrome(String s) {
3+
if (s == null || s.length() < 1) return "";
4+
5+
int start = 0, end = 0;
6+
7+
for (int i = 0; i < s.length(); i++) {
8+
9+
int len1 = expandAroundCenter(s, i, i);
10+
11+
int len2 = expandAroundCenter(s, i, i + 1);
12+
13+
int len = Math.max(len1, len2);
14+
15+
if (len > end - start) {
16+
start = i - (len - 1) / 2;
17+
end = i + len / 2;
18+
}
19+
}
20+
21+
return s.substring(start, end + 1);
22+
}
23+
24+
private int expandAroundCenter(String s, int left, int right) {
25+
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
26+
left--;
27+
right++;
28+
}
29+
return right - left - 1;
30+
}
31+
}

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ Each problem has its own directory containing:
1616
| 2 | [Add Two Numbers](Add%20Two%20Numbers) | Medium | [Java](Add%20Two%20Numbers/Solution.java) | Linked List, Math, Recursion |
1717
| 3 | [Longest Substring Without Repeating Characters](Longest%20Substring%20Without%20Repeating%20Characters) | Medium | [Java](Longest%20Substring%20Without%20Repeating%20Characters/Solution.java) | Hash Table, String, Sliding Window |
1818
| 4 | [Median of Two Sorted Arrays](Median%20of%20Two%20Sorted%20Arrays) | Hard | [Java](Median%20of%20Two%20Sorted%20Arrays/Solution.java) | Array, Binary Search, Divide and Conquer |
19+
| 5 | [Longest Palindromic Substring](Longest%20Palindromic%20Substring) | Medium | [Java](Longest%20Palindromic%20Substring/Solution.java) | Two Pointers, String, Dynamic Programming |
1920
| 2924 | [Find Champion II](Find%20Champion%20II) | Medium | [Java](Find%20Champion%20II/Solution.java) | Graph |
2021

2122
## Categories
2223

2324
### By Difficulty
2425
- Easy: 1
25-
- Medium: 3
26+
- Medium: 4
2627
- Hard: 1
2728

2829
### By Topics
@@ -34,6 +35,9 @@ Each problem has its own directory containing:
3435
- [Longest Substring Without Repeating Characters](Longest%20Substring%20Without%20Repeating%20Characters)
3536
- String Problems
3637
- [Longest Substring Without Repeating Characters](Longest%20Substring%20Without%20Repeating%20Characters)
38+
- [Longest Palindromic Substring](Longest%20Palindromic%20Substring)
39+
- Dynamic Programming Problems
40+
- [Longest Palindromic Substring](Longest%20Palindromic%20Substring)
3741
- Linked List Problems
3842
- [Add Two Numbers](Add%20Two%20Numbers)
3943
- Math Problems

0 commit comments

Comments
 (0)