package com.fishercoder.solutions;

/**
 * 5. Longest Palindromic Substring
 *
 * Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

 Example:
 Input: "babad"
 Output: "bab"
 Note: "aba" is also a valid answer.

 Example:
 Input: "cbbd"
 Output: "bb"
 */
public class _5 {

    private int low;
    private int maxLen;

    public String longestPalindrome(String s) {
        int len = s.length();
        if (len < 2) {
            return s;
        }

        for (int i = 0; i < len - 1; i++) {
            extendPalindrome(s, i, i); // assume odd length, try to extend Palindrome as possible
            extendPalindrome(s, i, i + 1); // assume even length.
        }
        return s.substring(low, low + maxLen);
    }

    private void extendPalindrome(String s, int left, int right) {
        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            left--;
            right++;
        }
        if (maxLen < right - left - 1) {
            low = left + 1;
            maxLen = right - left - 1;
        }
    }

}

@@ -333,16 +333,56 @@
 s _5 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     priv
@@ -394,24 +394,28 @@
 nt low;%0A    
+   
+ 
 private int 
@@ -423,16 +423,20 @@
 axLen;%0A%0A
+    
     publ
@@ -483,16 +483,20 @@
         
+   
+ 
 int len 
@@ -513,24 +513,28 @@
 ();%0A        
+    
 if (len %3C 2)
@@ -540,32 +540,36 @@
 ) %7B%0A            
+   
+ 
 return s;%0A      
@@ -574,19 +574,26 @@
     
-%7D%0A%0A
+    %7D%0A%0A   
         
 for 
@@ -588,16 +588,17 @@
         
+ 
 for (int
@@ -621,24 +621,28 @@
 - 1; i++) %7B%0A
+    
             
@@ -727,16 +727,20 @@
 ossible%0A
+    
         
@@ -797,34 +797,42 @@
 length.%0A        
-%7D%0A
+    %7D%0A    
         return s
@@ -862,27 +862,35 @@
 axLen);%0A    
-%7D%0A%0A
+    %7D%0A%0A    
     private 
@@ -940,24 +940,28 @@
 nt right) %7B%0A
+    
         whil
@@ -1047,16 +1047,20 @@
         
+    
 left--;%0A
@@ -1071,16 +1071,20 @@
         
+ 
+   
 right++;
@@ -1084,34 +1084,42 @@
 ight++;%0A        
-%7D%0A
+    %7D%0A    
         if (maxL
@@ -1155,16 +1155,20 @@
         
+    
 low = le
@@ -1187,16 +1187,20 @@
         
+  
+  
 maxLen =
@@ -1214,24 +1214,38 @@
 - left - 1;%0A
+            %7D%0A
         %7D%0A  
@@ -1248,11 +1248,10 @@
 %7D%0A    %7D%0A
-%0A
 %7D%0A

refactor 5