Ap 2
Ap 2
Experiment 1.2
1. Aim:
To demonstrate the concept of String-Matching algorithms.
2. Objective:
A) 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.
B) Given two strings a and b, return the minimum number of times you should repeat string a
so that string b is a substring of it. If it is impossible for b to be a substring of a after repeating it,
return -1.
A. Rotate String:
class Solution {
public:
bool rotateString(string s, string goal) {
return s.length() == goal.length() && (s + s).find(goal) != string::npos;
}
};
if (s.find(b) != string::npos)
return n;
if ((s + a).find(b) != string::npos)
return n + 1;
return -1;
}
};
Output: