Are Isomorphic?
Description
Two two-dimensional arrays are isomorphic if they have the same number of rows and each pair of respective rows contains the same number of elements.
Given two two-dimensional arrays, check if they are isomorphic.
Example
-
For
array1 = [[1, 1, 1], [0, 0]]
andarray2 = [[2, 1, 1], [2, 1]]
the output should be
areIsomorphic(array1, array2) = true;
-
For
array1 = [[2], []]
and
array2 = [[2]]
the output should be
areIsomorphic(array1, array2) = false.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] array.array.integer array1
Guaranteed constraints:
1 ≤ array1.length ≤ 5
,
0 ≤ array1[i].length ≤ 5
,
0 ≤ array1[i][j] ≤ 50
. -
[input] array.array.integer array2
Guaranteed constraints:
1 ≤ array2.length ≤ 5
,
0 ≤ array2[i].length ≤ 5
,
0 ≤ array2[i][j] ≤ 50
. -
[output] boolean
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
1
2
3
4
5
6
function areIsomorphic(array1, array2) {
return (
array1.length === array2.length &&
array1.every((row, i) => row.length === array2[i].length)
);
}