This repository provides step-by-step explanations for solving the "Rotate String" problem across multiple programming languages: C++, Java, JavaScript, Python, and Go.
Given two strings, s
and goal
, determine if s
can become goal
after a certain number of rotations. A rotation involves moving the leftmost character to the rightmost position, one character at a time.
For example, if s = "abcde"
:
- After one rotation:
"bcdea"
- After two rotations:
"cdeab"
The goal is to check if such rotations can make s
equal to goal
.
- First, verify if the lengths of
s
andgoal
are equal. - If they are not, then
s
cannot be rotated to becomegoal
, so we immediately returnfalse
.
- Concatenate
s
with itself to create a new string,doubled = s + s
. - This combined string includes all possible rotations of
s
as substrings. By doublings
, each possible rotation is represented as a contiguous substring withindoubled
.
- Use a substring search to see if
goal
is contained withindoubled
. - If
goal
is found withindoubled
, thens
can indeed be rotated to matchgoal
, and we returntrue
. - If
goal
is not found indoubled
, then it's impossible to matchs
togoal
via rotations, so we returnfalse
.
- Check Lengths: First, check if the length of
s
is equal togoal
. If they are different, returnfalse
. - Concatenate Strings: Create a new string by concatenating
s
with itself. - Search for Substring: Use the
find
function to check ifgoal
is a substring of the concatenated string. Ifgoal
is found, returntrue
; otherwise, returnfalse
.
- Check Lengths: First, compare the lengths of
s
andgoal
. If they don’t match, returnfalse
. - Concatenate Strings: Create a new string by combining
s
with itself. - Search for Substring: Use the
contains
method to see ifgoal
is a substring of the concatenated string. Ifgoal
exists in this combined string, returntrue
; otherwise, returnfalse
.
- Check Lengths: Begin by comparing the lengths of
s
andgoal
. If they are different, immediately returnfalse
. - Concatenate Strings: Construct a new string by concatenating
s
with itself. - Search for Substring: Use the
includes
method to determine ifgoal
is a substring of the concatenated string. Ifgoal
is found, returntrue
; otherwise, returnfalse
.
- Check Lengths: Start by checking if the length of
s
matches the length ofgoal
. If not, returnfalse
. - Concatenate Strings: Create a new string by concatenating
s
with itself. - Search for Substring: Use the
in
keyword to check ifgoal
is a substring of the concatenated string. Ifgoal
is found within, returntrue
; otherwise, returnfalse
.
- Check Lengths: First, verify if
s
andgoal
have the same length. If they don’t, returnfalse
. - Concatenate Strings: Create a new string by concatenating
s
with itself. - Search for Substring: Use
strings.Contains
to check ifgoal
is within the concatenated string. Ifgoal
is a substring, returntrue
; otherwise, returnfalse
.
- Time Complexity: (O(n)), where (n) is the length of
s
orgoal
, because checking ifgoal
is a substring of the doubled string takes linear time. - Space Complexity: (O(n)), as we create a concatenated string of size (2 \times n).
This approach provides an efficient solution by leveraging string concatenation and substring search instead of generating each rotation explicitly. The method is consistent across different languages, offering a uniform understanding and solution to the problem.