Another, but not necessarily the best solution using sorted arrays of characters and substring:
Given your two lines ...
subject = "aasmflathesorcerersnstonedksaottersapldrrysaahf" search = "harrypotterandthesorcerersstone"
You can sort the subject line using .chars.sort.join ...
subject = subject.chars.sort.join # => "aaaaaaacddeeeeeffhhkllmnnoooprrrrrrssssssstttty"
Then create a list of substrings to search for:
search = search.chars.group_by(&:itself).values.map(&:join) # => ["hh", "aa", "rrrrrr", "y", "p", "ooo", "tttt", "eeeee", "nn", "d", "sss", "c"]
Alternatively, you can create the same set of substrings using this method
search = search.chars.sort.join.scan(/((.)\2*)/).map(&:first)
And then just check if each line of the search substring appears within the sorted topic:
search.all? { |c| subject[c] }
meagar
source share