This answer has a large defect. It pretend every word in WORDS has the same length.
For example, the test case
str:[catcatabcdfoc] words:[cat, abcd]
doesn’t give the correct result.
This is actually specified as a constraint in the problem statement.