package com.fishercoder.solutions;

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

/**
 * 1065. Index Pairs of a String
 *
 * Given a text string and words (a list of strings),
 * return all index pairs [i, j] so that the substring text[i]...text[j] is in the list of words.
 *
 * Example 1:
 * Input: text = "thestoryofleetcodeandme", words = ["story","fleet","leetcode"]
 * Output: [[3,7],[9,13],[10,17]]
 *
 * Example 2:
 * Input: text = "ababa", words = ["aba","ab"]
 * Output: [[0,1],[0,2],[2,3],[2,4]]
 * Explanation:
 * Notice that matches can overlap, see "aba" is found in [0,2] and [2,4].
 *
 * Note:
 *
 * All strings contains only lowercase English letters.
 * It's guaranteed that all strings in words are different.
 * 1 <= text.length <= 100
 * 1 <= words.length <= 20
 * 1 <= words[i].length <= 50
 * Return the pairs [i,j] in sorted order (i.e. sort them by their first coordinate in case of ties sort them by their second coordinate).
 * */
public class _1065 {
    public static class Solution1 {
        public int[][] indexPairs(String text, String[] words) {
            List<List<Integer>> lists = new ArrayList<>();
            for (String word : words) {
                lists.addAll(findAllMatchsForThisWord(word, text));
            }
            if (lists.isEmpty()) {
                return new int[][]{};
            }
            Collections.sort(lists, (o1, o2) -> {
                if (o1.get(0) > o2.get(0)) {
                    return 1;
                } else if (o1.get(0) < o2.get(0)) {
                    return -1;
                } else {
                    if (o1.get(1) > o2.get(1)) {
                        return 1;
                    } else {
                        return -1;
                    }
                }
            });
            int[][] result = new int[lists.size()][lists.get(0).size()];
            for (int i = 0; i < lists.size(); i++) {
                result[i][0] = lists.get(i).get(0);
                result[i][1] = lists.get(i).get(1);
            }
            return result;
        }

        private List<List<Integer>> findAllMatchsForThisWord(String word, String text) {
            List<List<Integer>> lists = new ArrayList<>();
            for (int i = 0; i <= text.length() - word.length(); i++) {
                if (text.substring(i, i + word.length()).equals(word)) {
                    lists.add(Arrays.asList(i, i + word.length() - 1));
                }
            }
            return lists;
        }
    }
}

@@ -140,884 +140,8 @@
 t;%0A%0A
-/**%0A * 1065. Index Pairs of a String%0A *%0A * Given a text string and words (a list of strings),%0A * return all index pairs %5Bi, j%5D so that the substring text%5Bi%5D...text%5Bj%5D is in the list of words.%0A *%0A * Example 1:%0A * Input: text = %22thestoryofleetcodeandme%22, words = %5B%22story%22,%22fleet%22,%22leetcode%22%5D%0A * Output: %5B%5B3,7%5D,%5B9,13%5D,%5B10,17%5D%5D%0A *%0A * Example 2:%0A * Input: text = %22ababa%22, words = %5B%22aba%22,%22ab%22%5D%0A * Output: %5B%5B0,1%5D,%5B0,2%5D,%5B2,3%5D,%5B2,4%5D%5D%0A * Explanation:%0A * Notice that matches can overlap, see %22aba%22 is found in %5B0,2%5D and %5B2,4%5D.%0A *%0A * Note:%0A *%0A * All strings contains only lowercase English letters.%0A * It's guaranteed that all strings in words are different.%0A * 1 %3C= text.length %3C= 100%0A * 1 %3C= words.length %3C= 20%0A * 1 %3C= words%5Bi%5D.length %3C= 50%0A * Return the pairs %5Bi,j%5D in sorted order (i.e. sort them by their first coordinate in case of ties sort them by their second coordinate).%0A * */%0A
 publ

refactor 1065