14. Check if strings are rotations of each other or not
The problem can be found at the following link: Question Link
My Approach
To check if two strings, s1 and s2, are rotations of each other,
I concatenate
s1with itself to create a new strings.Then, I check if
s2is a substring ofsusing thefindfunction. If the index is not -1, thens2is a rotation ofs1.
Time and Auxiliary Space Complexity
Time Complexity:
O(N), whereNis the length ofs1.Auxiliary Space Complexity:
O(N), as we create a new string by concatenation.
Code (C++)
class Solution {
public:
bool areRotations(string s1, string s2) {
string s = s1 + s1;
return s.find(s2) != -1;
}
};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
Was this helpful?