Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the S Ubstring together. Assume the given string consists of lowercase Chinese letters only and its length would not exceed 10000.
Example 1:
Input: "Abab" Output:TrueExplanation:It ' s the substring "AB" twice.
Example 2:
Input: "ABA" Output:false
Example 3:
Input: "Abcabcabcabc" Output:TrueExplanation:It ' s The substring "abc" four times. (and the substring "ABCABC" twice.)
Test instructions
See if a string can be repeated by a substring, return true if possible, or return false
Ideas:
Change the scanning step, double cycle
1. Assume that a string of length p can be repeated, 1=<p<=len (s)
2. Repeat multiple loops, verifying str[i]! in each cycle =str[i+p], if true, break;
3. If one can scan to the end, that is i+p=l, and i%p==0, then return to the True
1 BOOLRepeatedsubstringpattern (Char*str) {2 intp,i;3 intL=strlen (str);4 intflag=0;5 for(p=1;p <=l/2;p + +)6 {7 for(i=0; i<l-p;i++)8 {9flag=0;Ten if(str[i]!=str[i+p]) One { Aflag=1; - Break; - } the } - if(0==flag&&i%p==0) - return true; - } + return false; -}
The time complexity of this solution is O (n^2), there is a KMP algorithm, the time complexity of O (n), I have not done, to be supplemented by
"Leetcode" 459. Repeated Substring Pattern