if it can be constructed by taking a substring of it and appending multiple copies of the substring together. Assume the given string consists of lowercase 中文版 letters only and its length would not exceed 100001< c2>"Abab"Output:TrueExplanation:It' s the substring "AB" twice. Example 2"ABA"3"ABCABCABCABC"Output:TrueExplanation:It' s the substring "ABC" four times. (and the substring "ABCABC" twice.)
This problem should not be able to use DP and other solutions, only brute force or KMP (for the drill)
1 Public classSolution {2 Public BooleanRepeatedsubstringpattern (String str) {3 intLen =str.length ();4 for(intI=LEN/2; i>0; i--) {5 if(len%i = = 0) {6 intm = len/i;7String subStr = str.substring (0, i);8 intj = 1;9 for(; j<m; J + +) {Ten if(!substr.equals (Str.substring (J*i, j*i+i))) One Break; A } - if(j = m)return true; - } the } - return false; - } -}
KMP solution not studied, Https://discuss.leetcode.com/topic/67590/java-o-n
leetcode:repeated Substring Pattern