12. Check if string is rotated by two places
The problem can be found at the following link: Question Link
My Approach
To check if a string is rotated by two places,
I compare the original string
s1
and check if the first string is equal to either of the two possible rotations of the second strings2
.The rotations are done by taking two characters from the end and placing them at the beginning, or taking two characters from the beginning and placing them at the end.
Time and Auxiliary Space Complexity
Time Complexity:
O(N)
, whereN
is the length of the strings.Auxiliary Space Complexity:
O(1)
as we use a constant amount of additional space.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated