src/main/java/com/leetcode/dp/medium/LargestPalindromicSubstring_5.java
public String longestPalindrome(String s) {
      boolean[][] dp = new boolean[s.length()][s.length()];
      int maxLeft = 0;
      int maxRight = 0;

      for (int i = 0; i < s.length(); i++) {
        dp[i][i] = true;
      }

      for (int right = 0; right < s.length(); right++) {
        for (int left = 0; left < right; left++) {
          if (s.charAt(right) == s.charAt(left) && (dp[left + 1][right - 1] || right - left == 1)) {
            dp[left][right] = true;
            if (right - left > maxRight - maxLeft) {
              maxLeft = left;
              maxRight = right;
            }
          }
        }
      }
      return s.substring(maxLeft, maxRight + 1);
    }
/*
     O(n^2) Runtime: 179 ms, faster than 35.57% of Java online submissions for Longest Palindromic Substring.
     O(n^2) Memory Usage: 42.9 MB, less than 25.33% of Java online submissions for Longest Palindromic Substring.
    */
O(n^2) Runtime: 179 ms, faster than 35.57% of Java online submissions for Longest Palindromic Substring.
O(n^2) Memory Usage: 42.9 MB, less than 25.33% of Java online submissions for Longest Palindromic Substring.
O ( n^2 ) Runtime : 179 ms faster than 35 . 57% of Java online submissions for Longest Palindromic Substring . O ( n^2 ) Memory Usage : 42 . 9 MB less than 25 . 33% of Java online submissions for Longest Palindromic Substring .
public String longestPalindrome(String s) {
      boolean[][] dp = new boolean[s.length()][s.length()];
      int maxLeft = 0;
      int maxRight = 0;
      for (int i = 0; i < s.length(); i++) {
        dp[i][i] = true;
      }
      for (int right = 0; right < s.length(); right++) {
        for (int left = 0; left < right; left++) {
          if (s.charAt(right) == s.charAt(left) && (dp[left + 1][right - 1] || right - left == 1)) {
            dp[left][right] = true;
            if (right - left > maxRight - maxLeft) {
              maxLeft = left;
              maxRight = right;
            }
          }
        }
      }
      return s.substring(maxLeft, maxRight + 1);
    }
public String longestPalindrome ( String s ) { boolean [ ] [ ] dp = new boolean [ s . length ( ) ] [ s . length ( ) ] ; int maxLeft = 0 ; int maxRight = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { dp [ i ] [ i ] = true ; } for ( int right = 0 ; right < s . length ( ) ; right ++ ) { for ( int left = 0 ; left < right ; left ++ ) { if ( s . charAt ( right ) == s . charAt ( left ) && ( dp [ left + 1 ] [ right - 1 ] || right - left == 1 ) ) { dp [ left ] [ right ] = true ; if ( right - left > maxRight - maxLeft ) { maxLeft = left ; maxRight = right ; } } } } return s . substring ( maxLeft , maxRight + 1 ) ; }
O(n^2) Runtime: 179 ms, faster than 35.57% of Java online submissions for Longest Palindromic Substring.
O ( n^2 ) Runtime : 179 ms faster than 35 . 57% of Java online submissions for Longest Palindromic Substring .