Rotate String
Determine whether rotating the first string left zero or more times can produce the second.
Work through the cases
3 examplesInput notation
Two nonempty strings on separate lines.Expected result
true or false.Case 1
Input
abcde cdeab
Expected output
true
Case 2
Input
abc acb
Expected output
false
Case 3
Input
a a
Expected output
true
These notes use simplified input/output notation. The linked platform defines the method signature and judge format.
FROM IDEA TO IMPLEMENTATION
Understand the approach.
Compare the documented approaches below. Brute force, better, and optimal versions appear where available; a direct exercise may need only one approach.
Is goal a rotation of s? Unequal lengths fail; otherwise goal is a substring of s+s.
class Solution {
public boolean rotateString(String s, String goal) {
return s.length() == goal.length() && (s + s).contains(goal);
}
}Reference code. Platform solutions may use supplied node types, imports, or collection libraries; foundational examples are complete programs.
Solutions adapted from Doocs / LeetCode contributors, licensed under CC BY-SA 4.0. Formatting changed; TypeScript versions converted to JavaScript. Supplemental implementations and approach notes added. Adapted solution material is available under the same license.