I have a list of numbers like this (array)
1 2 3 4
So my goal is the check given another array if this array if a permutation of the original example, the array (3 4 1 2) and (1 2 4 3) are permutations of the original but (1 2 1 1) or (1 5 4 3) not.
Two possible solutions are:
(1)
O(n)space & average time solution will be to create a histogram, based on a hash table, of the data – and check if the histograms are identicals. The idea is – count how many each element appears in each list, and then check to see each element appears exactly the same times in each array.pseudo code:
(2)
O(nlogn)time solution will be to sort both arrays, and then iterate and check if they are identical.