package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * https://leetcode.com/problems/palindrome-pairs/
 */
public class Problem336 {
    public List<List<Integer>> palindromePairs(String[] words) {
        List<List<Integer>> answer = new ArrayList<>();
        for (int i = 0; i < words.length; i++) {
            for (int j = i + 1; j < words.length; j++) {
                if (isPalindrome(words[i] + words[j])) {
                    answer.add(Arrays.asList(i, j));
                }
                if (isPalindrome(words[j] + words[i])) {
                    answer.add(Arrays.asList(j, i));
                }
            }
        }
        return answer;
    }

    private static boolean isPalindrome(String s) {
        for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Problem336 prob = new Problem336();
        System.out.println(prob.palindromePairs(new String[]{"abcd", "dcba", "lls", "s", "sssll"})); // [[0,1],[1,0],[3,2],[2,4]]
        System.out.println(prob.palindromePairs(new String[]{"bat", "tab", "cat"})); // [[0,1],[1,0]]
    }
}

@@ -291,24 +291,223 @@
 ayList%3C%3E();%0A
+        String%5B%5D reversedWords = new String%5Bwords.length%5D;%0A        for (int i = 0; i %3C words.length; i++) %7B%0A            reversedWords%5Bi%5D = new StringBuilder(words%5Bi%5D).reverse().toString();%0A        %7D%0A
         for 
@@ -579,17 +579,25 @@
  1; j %3C 
-w
+reversedW
 ords.len
@@ -649,20 +649,27 @@
 words%5Bi%5D
- + w
+, reversedW
 ords%5Bj%5D)
@@ -788,12 +788,19 @@
 s%5Bj%5D
- + w
+, reversedW
 ords
@@ -983,117 +983,450 @@
 ing 
-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
+word, String reversedWord) %7B%0A        if (word.equals(reversedWord)) %7B%0A            return true;%0A        %7D%0A        if (word.startsWith(reversedWord)) %7B%0A            String sub = word.substring(reversedWord.length());%0A            if (isPalindrome(sub)) %7B%0A                return true;%0A            %7D%0A        %7D else if (reversedWord.startsWith(word)) %7B%0A            String sub = reversedWord.substring(word.length());%0A            if (isPalindrome(sub
 )) %7B
@@ -1453,12 +1453,11 @@
 urn 
-fals
+tru
 e;%0A 
@@ -1490,27 +1490,28 @@
      return 
-tru
+fals
 e;%0A    %7D%0A%0A  
@@ -1517,21 +1517,22 @@
    p
-ublic
+rivate
  static 
 void
@@ -1531,30 +1531,36 @@
 tic 
-void main
+boolean isPalindrome
 (String
-%5B%5D arg
+ 
 s) %7B
@@ -1572,275 +1572,183 @@
     
-Problem336 prob = new Problem336();%0A        System.out.println(prob.palindromePairs(new String%5B%5D%7B%22abcd%22, %22dcba%22, %22lls%22, %22s%22, %22sssll%22%7D)); // %5B%5B0,1%5D,%5B1,0%5D,%5B3,2%5D,%5B2,4%5D%5D%0A        System.out.println(prob.palindromePairs(new String%5B%5D%7B%22bat%22, %22tab%22, %22cat%22%7D)); // %5B%5B0,1%5D,%5B1,0%5D%5D
+for (int i = 0, j = s.length() - 1; i %3C j; i++, j--) %7B%0A            if (s.charAt(i) != s.charAt(j)) %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true;
 %0A   

Solve problem 336