Skip to content

1460. Make Two Arrays Equal by Reversing Subarrays 👍

  • Time: $O(n)$
  • Space: $O(n)$
1
2
3
4
5
6
7
class Solution {
 public:
  bool canBeEqual(vector<int>& target, vector<int>& arr) {
    return unordered_multiset<int>(arr.begin(), arr.end()) ==
           unordered_multiset<int>(target.begin(), target.end());
  }
};
1
2
3
4
5
6
7
8
9
class Solution {
  public boolean canBeEqual(int[] target, int[] arr) {
    return Arrays.stream(arr)
        .boxed()
        .collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()))
        .equals(Arrays.stream(target).boxed().collect(
            Collectors.groupingBy(Integer::intValue, Collectors.counting())));
  }
}
1
2
3
class Solution:
  def canBeEqual(self, target: list[int], arr: list[int]) -> bool:
    return collections.Counter(arr) == collections.Counter(target)