package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/check-array-formation-through-concatenation/
 */
public class Problem1640 {
    public boolean canFormArray(int[] arr, int[][] pieces) {
        Arrays.sort(pieces, (a, b) -> {
            int cmp = Integer.compare(b.length, a.length);
            if (cmp == 0) {
                return Integer.compare(Arrays.toString(b).length(), Arrays.toString(a).length());
            }
            return cmp;
        });
        String str = toString(arr);
        for (int[] piece : pieces) {
            String s = toString(piece);
            int index = str.indexOf(s);
            if (index < 0) {
                return false;
            }
            str = str.substring(0, index) + str.substring(index + s.length());
        }
        return true;
    }

    private static String toString(int[] arr) {
        StringBuilder sb = new StringBuilder();
        for (int a : arr) {
            if (sb.length() == 0) {
                sb.append(a);
            } else {
                sb.append(",").append(a);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        Problem1640 prob = new Problem1640();
        System.out.println(prob.canFormArray(new int[]{85}, new int[][]{{85}})); // true
        System.out.println(prob.canFormArray(new int[]{15,88}, new int[][]{{88},{15}})); // true
        System.out.println(prob.canFormArray(new int[]{49,18,16}, new int[][]{{16,18,49}})); // false
        System.out.println(prob.canFormArray(new int[]{91,4,64,78}, new int[][]{{78},{4,64},{91}})); // true
        System.out.println(prob.canFormArray(new int[]{91,4,64,78}, new int[][]{{78},{64,4},{91}})); // false
        System.out.println(prob.canFormArray(new int[]{1,3,5,7}, new int[][]{{2,4,6,8}})); // false
        System.out.println(prob.canFormArray(new int[]{4,56,4}, new int[][]{{4}, {4,56}})); // true
        System.out.println(prob.canFormArray(new int[]{32,66,73,15,3,70,53}, new int[][]{{66,73},{15},{3},{32},{70},{53}})); // true
        System.out.println(prob.canFormArray(new int[]{75,1,60,91,84,55,5,39,19,52,38,66,14,17,49},
            new int[][]{{60},{52,38},{66},{39},{19},{1},{84,55},{17},{14},{49},{91},{5},{75}})); // true
    }
}

@@ -29,22 +29,45 @@
 va.util.
-Arrays
+HashMap;%0Aimport java.util.Map
 ;%0A%0A/**%0A 
@@ -247,577 +247,255 @@
     
-Arrays.sort(pieces, (a, b) -%3E %7B%0A            int cmp = Integer.compare(b.length, a.length);%0A            if (cmp == 0) %7B%0A                return Integer.compare(Arrays.toString(b).length(), Arrays.toString(a).length());%0A            %7D%0A            return cmp;%0A        %7D);%0A        String str = toString(arr);%0A        for (int%5B%5D piece : pieces) %7B%0A            String s = toString(piece);%0A            int index = str.indexOf(s);%0A            if (index %3C 0) %7B%0A                return false;%0A            %7D%0A            str = str.substring(0, index) + str.substring(index + s.length()
+Map%3C/* first element */ Integer, /* index */ Integer%3E map = new HashMap%3C%3E();%0A        for (int i = 0; i %3C pieces.length; i++) %7B%0A            // We only care about the first index because the integers are distinct.%0A            map.put(pieces%5Bi%5D%5B0%5D, i
 );%0A 
@@ -515,151 +515,79 @@
     
-return true
+int i = 0
 ;%0A    
-%7D%0A%0A
     
-private static String toString(int%5B%5D arr) %7B%0A        StringBuilder sb = new StringBuilder();%0A        for (
+while (i %3C arr.length) %7B%0A            
 int a 
-:
+=
  arr
-) %7B
+%5Bi%5D;
 %0A   
@@ -603,24 +603,27 @@
 if (
-sb.length() == 0
+!map.containsKey(a)
 ) %7B%0A
@@ -642,20 +642,20 @@
     
-sb.append(a)
+return false
 ;%0A  
@@ -669,15 +669,8 @@
    %7D
- else %7B
 %0A   
@@ -682,696 +682,162 @@
     
-    sb.append(%22,%22).append(a);%0A            %7D%0A        %7D%0A        return sb.toString();%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1640 prob = new Problem1640();%0A        System.out.println(prob.canFormArray(new int%5B%5D%7B85%7D, new int%5B%5D%5B%5D%7B%7B85%7D%7D)); // true%0A        System.out.println(prob.canFormArray(new int%5B%5D%7B15,88%7D, new int%5B%5D%5B%5D%7B%7B88%7D,%7B15%7D%7D)); // true%0A        System.out.println(prob.canFormArray(new int%5B%5D%7B49,18,16%7D, new int%5B%5D%5B%5D%7B%7B16,18,49%7D%7D)); // false%0A        System.out.println(prob.canFormArray(new int%5B%5D%7B91,4,64,78%7D, new int%5B%5D%5B%5D%7B%7B78%7D,%7B4,64%7D,%7B91%7D%7D)); // true%0A        System.out.println(prob.canFormArray(new int%5B%5D%7B91,4,64,78%7D, new int%5B%5D%5B%5D%7B%7B78%7D,%7B64,4%7D,%7B91%7D%7D)); //
+int index = map.get(a);%0A            for (int b : pieces%5Bindex%5D) %7B%0A                if (i %3C arr.length && b != arr%5Bi++%5D) %7B%0A                    return
  false
+;
 %0A   
@@ -845,537 +845,62 @@
     
-System.out.println(prob.canFormArray(new int%5B%5D%7B1,3,5,7%7D, new int%5B%5D%5B%5D%7B%7B2,4,6,8%7D%7D)); // false%0A        System.out.println(prob.canFormArray(new int%5B%5D%7B4,56,4%7D, new int%5B%5D%5B%5D%7B%7B4%7D, %7B4,56%7D%7D)); // true%0A        System.out.println(prob.canFormArray(new int%5B%5D%7B32,66,73,15,3,70,53%7D, new int%5B%5D%5B%5D%7B%7B66,73%7D,%7B15%7D,%7B3%7D,%7B32%7D,%7B70%7D,%7B53%7D%7D)); // true%0A        System.out.println(prob.canFormArray(new int%5B%5D%7B75,1,60,91,84,55,5,39,19,52,38,66,14,17,49%7D,%0A            new int%5B%5D%5B%5D%7B%7B60%7D,%7B52,38%7D,%7B66%7D,%7B39%7D,%7B19%7D,%7B1%7D,%7B84,55%7D,%7B17%7D,%7B14%7D,%7B49%7D,%7B91%7D,%7B5%7D,%7B75%7D%7D)); //
+        %7D%0A            %7D%0A        %7D%0A        return
  true
+;
 %0A   

Solve problem 1640