Skip to content

Commit

Permalink
solution(cpp,java): 5. Longest Palindromic Substring
Browse files Browse the repository at this point in the history
5. Longest Palindromic Substring
- C++
- Java
  • Loading branch information
godkingjay authored Oct 13, 2023
2 parents d97fac9 + 5b4b857 commit cde208c
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Medium/5. Longest Palindromic Substring/Solution.cpp
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;
}
};
32 changes: 32 additions & 0 deletions Medium/5. Longest Palindromic Substring/Solution.java
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;
}
}
27 changes: 27 additions & 0 deletions Medium/5. Longest Palindromic Substring/Solution.md
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.

0 comments on commit cde208c

Please sign in to comment.