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
Solution :
bool rotateString(string s, string goal) {
string tmp = s+s;
if(s.length() != goal.length())
return false;
if(tmp.find(goal) != string::npos)
return true;
return false;
}
Complete Code:
/*
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
*/
#include <bits/stdc++.h>
using namespace std;
bool checkRotation(string s1, string s2) {
string tmp = s1+s1;
if(s1.length() != s2.length())
return false;
cout << tmp << " : " << s2 << endl;
if(tmp.find(s2) != string::npos)
return true;
return false;
}
int main()
{
string str1 = "abcde";
string str2 = "abced";
cout << checkRotation(str1, str2) << endl;
return 0;
}