Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rotation Check Using strstr

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Rotation Check Using strstr. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

In Pocket Gems' Episode, a compact string may represent an ordered sequence of story-state tokens. Given two strings s1 and s2, determine whether s2 is a rotation of s1, meaning it can be produced by moving some prefix of s1 to its end without changing the character order.

Your solution must use at most one substring-search operation, equivalent to one call to C's strstr. Do not search for every possible rotation individually.

Formal Specification

Implement is_rotation(s1, s2).

  • Input: Two strings s1 and s2 containing arbitrary characters.
  • Output: Return True if s2 is a rotation of s1; otherwise, return False.
  • Two empty strings are considered rotations of each other.

Constraints

  • 0 <= len(s1), len(s2) <= 10^5
  • Strings may contain arbitrary characters.
  • The substring search may be performed at most once.
  • A rotation preserves the original string length.

Function Signature

def is_rotation(s1, s2):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output