I was at the job interview yesterday. And there is a question that asks me to find all the permutations in an array of string in ruby:
input: ['abc', 'cba', 'ab', 'ba', 'acbdef']
output: ['abc', 'cba', 'ab', 'ba']
Here is my implementation:
def find_permutation(arr)
result = []
arr.each do |element|
next if result.include?(element.reverse)
if arr.include?(element.reverse)
result << element << element.reverse
end
end
result
end
I am wondering if there is a better way to solve the problem in ruby since I don't think my solution is really 'pure' ruby.