Implement the isRotationOfString method that checks whether one string can be formed by rotating the other string.

The input contains two strings first and second. Your task is to check whether second is a rotation of first.

A rotation keeps the same characters in the same circular order, but starts from a different position. For example, cdab is a rotation of abcd.

Example 1

Input
first (string) = abcd
second (string) = cdab
Returns (boolean) true

Example 2

Input
first (string) = abcd
second (string) = acbd
Returns (boolean) false

Example 3

Input
first (string) = water
second (string) = terwa
Returns (boolean) true

Two strings can be rotations only when their lengths are equal.

If second is a rotation of first, it will always appear inside first + first. For example, abcdabcd contains cdab.

Pseudocode:

function isRotationOfString(first, second):
    if length(first) != length(second):
        return false
    doubled = first + first
    if second exists inside doubled:
        return true
    return false
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.