출처 : https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/
You are given two integer arrays of equal length target
and arr
. In one step, you can select any non-empty subarray of arr
and reverse it. You are allowed to make any number of steps.
Return true
if you can make arr equal to target
or false
otherwise.
class Solution {
public boolean canBeEqual(int[] target, int[] arr) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < arr.length; i++) {
map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
map.put(target[i], map.getOrDefault(target[i], 0) - 1);
}
for (int i : map.keySet()) {
if (map.get(i) != 0) return false;
}
return true;
}
}
🙈 풀이 참조한 문제
arr에 있는 모든 element들이 target에도 있는지 확인만 하면 됨