"Leetcode" 459. Repeated Substring Pattern

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.