FastPrepDetect Substring In Any Rotation
Problem · String

Detect Substring In Any Rotation

Learn this problem
Easyinfosys logoinfosysNEW GRADONSITE INTERVIEW

Problem 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) → boolean

Examples

Example 1

s = "absdsdf"needle = "fab"return = true

Moving the prefix absd to the end produces sdfabsd, which contains fab. Equivalently, fab occurs in s + s.

Example 2

s = "absdsdf"needle = "xyz"return = false

No rotation of absdsdf contains xyz.

Constraints

  • 0 <= s.length <= 10^4.
  • 0 <= needle.length <= 10^4.
  • s and needle contain only lowercase English letters.

More infosys problems

drafts saved locally
public boolean substringInAnyRotation(String s, String needle) {
  // Write your code here.
}
s"absdsdf"
needle"fab"
expectedtrue
checking account