Data Structures & AlgorithmsHardGATE CS · TCS NQT

To test whether string s2 is a rotation of string s1 in linear time, which approach is standard?

  1. A. Sort both strings and compare
  2. B. Compare every rotation of s1 with s2 by brute force
  3. C. Build a suffix tree of s1 only
  4. D. Search for s2 inside s1 concatenated with itself, using KMP or Z-function

Correct answer: D. Search for s2 inside s1 concatenated with itself, using KMP or Z-function

Every rotation of s1 is a substring of s1 + s1 of length |s1|, so a linear-time pattern matcher finds it. Sorting detects anagrams, not rotations.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic