[코테 풀이] Make Two Arrays Equal by Reversing Subarrays

시내·2024년 7월 15일
0

Q_1460) Make Two Arrays Equal by Reversing Subarrays

출처 : 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에도 있는지 확인만 하면 됨

profile
contact 📨 ksw08215@gmail.com

0개의 댓글