Problem · String
Detect Substring In Any Rotation
Learn this problemProblem statement
You are given two strings s and needle consisting of lowercase English letters.
A rotation of s is any string formed by moving a prefix of s to its end without changing character order. The original string is a rotation of itself.
Return true if needle occurs as a contiguous substring of at least one rotation of s, and false otherwise.
The empty needle occurs in every rotation, including the empty string.
Function
substringInAnyRotation(s: String, needle: String) → booleanExamples
Example 1
s = "absdsdf"needle = "fab"return = trueMoving the prefix absd to the end produces sdfabsd, which contains fab. Equivalently, fab occurs in s + s.
Example 2
s = "absdsdf"needle = "xyz"return = falseNo rotation of absdsdf contains xyz.
Constraints
0 <= s.length <= 10^4.0 <= needle.length <= 10^4.sandneedlecontain only lowercase English letters.