LC 1460 [E] Make Two Arrays Equal by Reversing Sub arrays - ALawliet/algorithms GitHub Wiki

We can bring any item to the front of an array by reversing the array from the start to the first occurence of the item. We can do that for the first item of the target list and then repeat the procedure with the rest of target and source until the whole target has been constructed. Therefore all we need to do is to check if both lists include the same items. This is really easy to do by comparing two Counters. We could also compare both lists after sorting them.

class Solution:
    def canBeEqual(self, target: List[int], arr: List[int]) -> bool:
        return Counter(target) == Counter(arr)