Learner Level
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) =
abcdsecond (string) =
cdabReturns (boolean)
trueExample 2
Input
first (string) =
abcdsecond (string) =
acbdReturns (boolean)
falseExample 3
Input
first (string) =
watersecond (string) =
terwaReturns (boolean)
trueTwo 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 falseReady to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run