You are given two integer arrays target
and arr
of the same length. In one step, you can select any non-empty subarray of arr
and reverse it. You need to determine if you can make arr
equal to target
after performing any number of steps.
n
, where 1 <= target.length == arr.length <= 1000
.1
and 1000
.arr
and target
is not important since we can reverse any subarray.True
. Otherwise, return False
.from collections import Counter
def canBeEqual(target, arr):
return Counter(target) == Counter(arr)
# Example Usage
target = [1, 2, 3, 4]
arr = [2, 4, 1, 3]
print(canBeEqual(target, arr)) # Output: True
target = [7]
arr = [7]
print(canBeEqual(target, arr)) # Output: True
target = [3, 7, 9]
arr = [3, 7, 11]
print(canBeEqual(target, arr)) # Output: False
Counter
objects for both target
and arr
takes O(n) time each.Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?