Given a smaller strings and a bigger string b, design an algorithm to find all permutations of the shorter string within the longer one.
def findPermutations(large: String, small: String): Int =
large.sliding(small.length).filter(_.diff(small).isEmpty).length
@ findPermutations("cbabadcbbabbcbabaabccbabc", "abbc")
res11: Int = 7
@ findPermutations("xaabx", "abb")
res12: Int = 0
Archived (faulty)
@ def findPermutations(large: String, small: String): Int =
large.sliding(small.length)
.toList
.filter(_.toSet == small.toSet)
.length
defined function findPermutations
Please evaluate it for correctness and speed.
findPermutations("xaabx", "abb")
and the.toList
doesn't serve any useful purpose. \$\endgroup\$