letecode [459] - Repeated Substring Pattern

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.

Example 1:

Input: "abab"
Output: True
Explanation: It's the substring "ab" twice.

Example 2:

Input: "aba"
Output: False

Example 3:

Input: "abcabcabcabc"
Output: True
Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)

Subject to the effect :

  Given a non-empty string, it can be determined whether the sequence is repeated a plurality of times its child configuration.

Understanding:

  The mathematical definition of f (x + t) = f (x), traversed to find qualified period t.

Code C ++:

class Solution {
 public :
     BOOL repeatedSubstringPattern ( String S) {
         // determines whether there is the period t 
        int t = . 1 , len, I; 
        len = s.length ();
         for (; t <= len / 2 ; ++ t) {
             IF (len% T) Continue ;
             // from the second period to the first period of each comparison value 
            for (T = I; I <len; ++ I) {
                 IF ! (S [I] = S [ % I T])
                     BREAK ; 
            } 
            IF (I ==len)
                return true;
        }
        return false;
    }
};

operation result:

  When execution: 40 ms, beat the 80.82% of all users to submit in C ++

  Memory consumption: 11.6 MB, defeated 93.22% of all users to submit in C ++

Guess you like

Origin www.cnblogs.com/lpomeloz/p/11113482.html