What you're doing now is creating an enumerator which will yield one permutation at a time, not consuming much memory, then with the .map(&:join)
you're putting everything that comes out of this enumerator into one gigantic array searchMap
.
Instead of that you should pull one permutation from the enumerator at a time and do your trick on that, instead of iterating over the gigantic array with searchMap.each
:
line = gets
L=line.split(" ")
S = gets
L.permutation do |item|
if S.include? item.join
puts S.index(item.join)
end
end