-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solution(cpp,java): 5. Longest Palindromic Substring
5. Longest Palindromic Substring - C++ - Java
- Loading branch information
Showing
3 changed files
with
94 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,35 @@ | ||
#include <string> | ||
|
||
class Solution { | ||
public: | ||
std::string longestPalindrome(std::string s) { | ||
std::string res = ""; | ||
int reslen = 0; | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
// odd length | ||
int left = i, right = i; | ||
while (left >= 0 && right < s.length() && s[left] == s[right]) { | ||
if (right - left + 1 > reslen) { | ||
res = s.substr(left, right - left + 1); | ||
reslen = right - left + 1; | ||
} | ||
left -= 1; | ||
right += 1; | ||
} | ||
|
||
// even length; | ||
left = i; | ||
right = i + 1; | ||
while (left >= 0 && right < s.length() && s[left] == s[right]) { | ||
if (right - left + 1 > reslen) { | ||
res = s.substr(left, right - left + 1); | ||
reslen = right - left + 1; | ||
} | ||
left -= 1; | ||
right += 1; | ||
} | ||
} | ||
return res; | ||
} | ||
}; |
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,32 @@ | ||
public class Solution { | ||
public String longestPalindrome(String s) { | ||
String res=""; | ||
int reslen=0; | ||
|
||
for(int i=0;i<s.length();i++){ | ||
// odd length | ||
int left=i,right=i; | ||
while(left>=0 && right<s.length() && s.charAt(left)==s.charAt(right)){ | ||
if((right-left+1)>reslen){ | ||
res=s.substring(left,right+1); | ||
reslen=right-left+1; | ||
} | ||
left-=1; | ||
right+=1; | ||
} | ||
|
||
// even length; | ||
left=i; | ||
right=i+1; | ||
while(left>=0 && right<s.length() && s.charAt(left)==s.charAt(right)){ | ||
if((right-left+1)>reslen){ | ||
res=s.substring(left,right+1); | ||
reslen=right-left+1; | ||
} | ||
left-=1; | ||
right+=1; | ||
} | ||
} | ||
return res; | ||
} | ||
} |
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,27 @@ | ||
# Problem Title | ||
|
||
Longest Palindromic Substring | ||
|
||
## Problem Description | ||
|
||
Given a string s, return the longest `palindromic substring` in s. | ||
|
||
Here are some key points to understand about the problem: | ||
|
||
1. You are given a string s, and the goal is to find the longest palindromic substring within this string. | ||
2. A palindromic substring is a sequence of characters that reads the same forwards as it does backward. | ||
3. s consist of only digits and English letters. | ||
|
||
## Method to Solve | ||
|
||
`Initialization:` Start with an empty result res and a length tracker reslen set to 0. | ||
|
||
`Iterate Through String:` For each character in the string, consider it as the center of a possible palindrome. | ||
|
||
`Odd-Length Palindromes:` Expand around the center character, checking for palindromes with odd lengths. | ||
|
||
`Even-Length Palindromes:` Also, check for palindromes with even lengths by considering adjacent characters. | ||
|
||
`Update Result:` Whenever a longer palindrome is found, update res and reslen. | ||
|
||
`Return Longest Palindrome:` After checking all centers, return the longest palindromic substring found in the string. |