Created
January 2, 2021 13:53
-
-
Save Desolve/05f2cd5f34d32264fdc74ae1dac90d65 to your computer and use it in GitHub Desktop.
1640 Check Array Formation Through Concatenation
This file contains 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
class Solution { | |
public boolean canFormArray(int[] arr, int[][] pieces) { | |
HashMap idx = new HashMap<Integer, Integer>(); | |
for (int i = 0; i < pieces.length; ++i) { | |
idx.put(pieces[i][0], i); | |
} | |
int cnt = 0; | |
while (cnt < arr.length) { | |
if (!idx.containsKey(arr[cnt])) return false; | |
int p = (int) idx.get(arr[cnt]); | |
for (int n : pieces[p]) { | |
if (n != arr[cnt++]) return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment