Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Strings Rotations

Medium Acceptance 43.83% Points 30.00

You are given two strings, s1 and s2, of equal lengths. Your task is to determine whether s2 can be obtained by rotating the characters of s1.

Note: The characters in the strings are in lowercase.

Examples
Example 1

Example 1

Input: s1 = "abcd", s2 = "cdab"

Output: true

Explanation: After 2 right rotations, s1 will become equal to s2.

Example 2

Example 2

Input: s1 = "aab", s2 = "aba"

Output: true

Explanation: After 1 left rotation, s1 will become equal to s2.

Hints
Hint 1
Expected Time Complexity: O(n)
Hint 2
Expected Space Complexity: O(n)
Constraints
  • 1 <= s1.size(), s2.size() <= 10^5
  • The characters in the strings are in lowercase.
Companies
Adobe Oracle
Topics
String
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions