Created
June 13, 2014 04:25
-
-
Save adohe-zz/337eee16e6d2caef00ea to your computer and use it in GitHub Desktop.
Find the longest palindromic substring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.westudio.java; | |
| public class LeetCodeTwo { | |
| public static void main(String[] argv) { | |
| } | |
| /** | |
| * Naive programing | |
| * @param s | |
| * @return | |
| */ | |
| public static String longestPalindromeOne(String s) { | |
| int longest = 0; | |
| int length = s.length(); | |
| String longestPalindrome = null; | |
| for (int i = 0; i < length; i++) { | |
| for (int j = i + 1; j < length; j++) { | |
| int len = j - i; | |
| String str = s.substring(i, j + 1); | |
| if (isPalindrome(str)) { | |
| if (len > longest) { | |
| longest = len; | |
| longestPalindrome = str; | |
| } | |
| } | |
| } | |
| } | |
| return longestPalindrome; | |
| } | |
| /** | |
| * Dynamic programming | |
| * @param s | |
| * @return the longest palindrome string | |
| */ | |
| public static String longestPalindromeTwo(String s) { | |
| if (s == null) | |
| return null; | |
| if (s.length() <= 1) | |
| return s; | |
| int longest = 0; | |
| String longestPalindrome = null; | |
| int length = s.length(); | |
| int[][] table = new int[length][length]; | |
| // every single letter is palindrome | |
| for (int i = 0; i < length; i++) { | |
| table[i][i] = 1; | |
| } | |
| longest = 1; | |
| printTable(table); | |
| // two consecutive same letters are palindrome | |
| for (int i = 0; i <= length - 2; i++) { | |
| if (s.charAt(i) == s.charAt(i + 1)) { | |
| table[i][i+1] = 1; | |
| longestPalindrome = s.substring(i, i + 2); | |
| longest = 2; | |
| } | |
| } | |
| printTable(table); | |
| for (int l = 3; l <= length; l++ ) { | |
| for (int i = 0; i <= length - l; i++) { | |
| int j = i + l - 1; | |
| if (s.charAt(i) == s.charAt(j)) { | |
| table[i][j] = table[i+1][j-1]; | |
| if (table[i][j] == 1 && l > longest) { | |
| longestPalindrome = s.substring(i, j + 1); | |
| } | |
| } else { | |
| table[i][j] = 0; | |
| } | |
| printTable(table); | |
| } | |
| } | |
| return longestPalindrome; | |
| } | |
| /** | |
| * Simplest way | |
| * @param s | |
| * @return | |
| */ | |
| public static String longestPalindromeThree(String s) { | |
| if (s.isEmpty()) { | |
| return null; | |
| } | |
| if (s.length() == 1) { | |
| return s; | |
| } | |
| String longest =s.substring(0, 1); | |
| for (int i = 0; i < s.length(); i++) { | |
| String temp = center(s, i, i); | |
| if (temp.length() > longest.length()) { | |
| longest = temp; | |
| } | |
| temp = center(s, i, i + 1); | |
| if (temp.length() > longest.length()) { | |
| longest = temp; | |
| } | |
| } | |
| return longest; | |
| } | |
| public static boolean isPalindrome(String s) { | |
| for (int i = 0; i < s.length() - 1; i++) { | |
| if (s.charAt(i) != s.charAt(s.length() - 1 - i)) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| public static void printTable(int[][] table) { | |
| for (int[] y : table) { | |
| for (int z : y) { | |
| System.out.print(z + " "); | |
| } | |
| System.out.println(); | |
| } | |
| System.out.println("-------"); | |
| } | |
| public static String center(String s, int begin, int end) { | |
| while (begin >= 0 && end <= s.length() - 1 && s.charAt(begin) == s.charAt(end)) { | |
| begin --; | |
| end ++; | |
| } | |
| return s.substring(begin + 1, end); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment