Data Structures & AlgorithmsModerateProduct Company SDE pattern

What is printed? (string rotation check)

What is printed? (string rotation check)
def is_rot(a, b):
    return len(a) == len(b) and b in a + a
print(is_rot("waterbottle", "erbottlewat"), is_rot("abcd", "acbd"), is_rot("", ""))
  1. A. True True True
  2. B. True False True
  3. C. True False False
  4. D. False False True

Correct answer: B. True False True

Every rotation of a appears as a substring of a + a. "acbd" is a permutation but not a rotation. Two empty strings are trivially rotations. With KMP the containment check is O(n).

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

Exams that ask this topic