package leetcode;

/**
 * https://leetcode.com/problems/shortest-palindrome/
 */
public class Problem214 {
    public String shortestPalindrome(String s) {
        int i = 0;
        int j = s.length() - 1;
        String answer = s;
        while (i < j) {
            // TODO
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem214 prob = new Problem214();
//        System.out.println(prob.shortestPalindrome("aacecaaa")); // "aaacecaaa"
        System.out.println(prob.shortestPalindrome("abcd")); // "dcbabcd"
//        System.out.println(prob.shortestPalindrome("aabaa")); // "aabaa"
//        System.out.println(prob.shortestPalindrome("aaaa")); // "aaaa"
//        System.out.println(prob.shortestPalindrome("aaba")); // "abaaba"
//        System.out.println(prob.shortestPalindrome("aababa")); // "ababaababa"
    }
}
@@ -212,70 +212,691 @@
     
-String answer = s;%0A        while (i %3C j) %7B%0A            // TODO
+int k = j;%0A        String prefix = %22%22;%0A        outer: while (i %3C j) %7B%0A            if (s.charAt(i) != s.charAt(j)) %7B%0A                for (int x = k; x %3E= j; x--) %7B%0A                    prefix += s.charAt(x);%0A                    if (isPalindrome(prefix + s)) %7B%0A                        break outer;%0A                    %7D%0A                %7D%0A                i = 0;%0A                j--;%0A                k = j;%0A            %7D else %7B%0A                i++;%0A                j--;%0A            %7D%0A        %7D%0A        return prefix + s;%0A    %7D%0A%0A    private static boolean isPalindrome(String s) %7B%0A        for (int i = 0, j = s.length() - 1; i %3C j; i++, j--) %7B%0A            if (s.charAt(i) != s.charAt(j)) %7B
 %0A   
@@ -892,34 +892,32 @@
 t(j)) %7B%0A        
-%7D%0A
         return a
@@ -919,14 +919,58 @@
 urn 
-answer
+false;%0A            %7D%0A        %7D%0A        return true
 ;%0A  
@@ -1063,18 +1063,16 @@
 m214();%0A
-//
         
@@ -1217,18 +1217,16 @@
 cbabcd%22%0A
-//
         
@@ -1286,26 +1286,24 @@
  // %22aabaa%22%0A
-//
         Syst
@@ -1361,18 +1361,16 @@
  %22aaaa%22%0A
-//
         
@@ -1434,18 +1434,16 @@
 abaaba%22%0A
-//
         

Update problem 214