會 time limit exceeded,求高手指點
def find_all_concatenated_words_in_a_dict(words)
return [] if words.count <= 1 || words.count > 10000 || words.map(&:to_s).inject(&:+).length > 600000
result = []
[*0...words.count].each do |i|
new_arr = words.dup
new_word = new_arr.delete(words[i]).dup
result << words[i] if concated_by_others?(new_word, new_arr)
end
return result
end
def concated_by_others?(str, arr)
[*1..str.length].each do |j|
if arr.include?(str.slice(0, j))
new_str = str.dup
new_str.slice!(str.slice(0, j))
return true if new_str.length.zero? or concated_by_others?(new_str, arr)
end
end
return false
end
--