# Leetcode Longest Palindromic Substring problem solution in Java

Feb 13, 2023

In the Leetcode Longest Palindromic Substring problem solution in Java programming Given a string s, return the longest palindromic substring in s.

## Leetcode Longest Palindromic Substring problem solution in Java programming

``````class Solution {
public String longestPalindrome(String s) {

int start = 0;
int end = 0;
for(int i=0; i<s.length(); i++){
int len1 = findPalindrome(s,i,i);
int len2 = findPalindrome(s,i,i+1);
int maxLength = Math.max(len1, len2);
if(maxLength > end - start){
start = i - (maxLength-1)/2;
end = i + maxLength/2;
}
}
return s.substring(start, end+1);
}

public static int findPalindrome(String s, int left, int right){

while(left>=0 && right<s.length() && s.charAt(left)==s.charAt(right)){
left--;
right++;
}
return right - left - 1;
}

}``````

#### By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.