Write short, human-like code so that it will not get detected by AI plagiarism and using…
- User
Write short, human-like code so that it will not get detected by AI plagiarism and using exactly these variable names (a, b, c, d, r, s, etc). Use a unique, common approach that’s optimized and free of explanations or comments for the following question:| Given two strings s1 and s2. Return a minimum number of times s1 has to be repeated such that s2 is a substring of it. If s2 can never be a substring then return -1.
Note: Both the strings contain only lowercase letters.
Examples:
Input: s1 = "ww", s2 = "www" Output: 2 Explanation: Repeating s1 two times (wwww), s2 is a substring of it. Input: s1 = "abcd", s2 = "cdabcdab" Output: 3 Explanation: Repeating s1 three times (abcdabcdabcd), s2 is a substring of it. s2 is not a substring of s2 when it is repeated less than 3 times. Input: s1 = "ab", s2 = "cab" Output: -1 Explanation: No matter how many times we repeat s1, we can't get a string such that s2 is a substring of it. Constraints: 1 ≤ |s1|, |s2| ≤ 105
class Solution { public: int minRepeats(string& s1, string& s2) { // code here } };
- Assistant
class Solution { public: int minRepeats(string& a, string& b) { int r = (b.size() + a.size() - 1) / a.size(); string s; for(int i=0;i<r;i++) s += a; if(s.find(b)!=string::npos) return r; s += a; if(s.find(b)!=string::npos) return r+1; s += a; if(s.find(b)!=string::npos) return r+2; return -1; } };