Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Desolve/05f2cd5f34d32264fdc74ae1dac90d65 to your computer and use it in GitHub Desktop.
Save Desolve/05f2cd5f34d32264fdc74ae1dac90d65 to your computer and use it in GitHub Desktop.
1640 Check Array Formation Through Concatenation
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