Description
Given two strings s and goal, return true if and only if s can become goal after some number of shifts on s.
A shift on s consists of moving the leftmost character of s to the rightmost position.
- For example, if
s = "abcde", then it will be"bcdea"after one shift.
Example 1:
Input: s = "abcde", goal = "cdeab" Output: true
Example 2:
Input: s = "abcde", goal = "abced" Output: false
Constraints:
1 <= s.length, goal.length <= 100sandgoalconsist of lowercase English letters.
Solutions
This is the most elegant solution, combining both the length check and the concatenation technique into a single return statement using the && operator. It first verifies that s and goal have equal length, and only if that condition is true does it check whether goal appears in the concatenated s + s using includes(). This approach is both correct and concise.
Language: javascript(2026-05-03 08:40)DONE
CPU Performance100.00%
Memory Performance74.46%
/**
* @param {string} s
* @param {string} goal
* @return {boolean}
*/
var rotateString = function(s, goal) {
return s.length === goal.length && (s + s).includes(goal);
};