Simple Java Solution


  • 0
    J

    '''
    public class Solution {
    String result;
    int len;
    public String longestPalindrome(String s) {

        for(int i=0;i<s.length();i++)
        {
            helper(s,i,i);
            helper(s,i,i+1);
        }
        
        return result;
    }
    
    public void helper(String s,int i,int j){
        
        if(i<0 || j>=s.length()) return;
        
        
        if(s.charAt(i)!=s.charAt(j)) return;
        
        helper(s,i-1,j+1);
        
        if(j-i+1 > len){
            result=s.substring(i,j+1);
            len=j-i+1;
        }
        
    }
    

    }
    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.