Repeated Substring Pattern

来源:互联网 发布:seo 专家 顺丰 编辑:程序博客网 时间:2024/06/06 23:58

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.)

class Solution {public:    bool repeatedSubstringPattern(string s) {        string pattern = "";        for(int i = 0 ; i < s.size() ; ++i){            pattern += s[i];            if(pattern.size() == s.size())                break;            if(s.size()%pattern.size()==0){                int count = s.size() / pattern.size() ;                string temp = "";                while(count--){                    temp+=pattern;                }                if(temp == s)                    return true;            }        }        return false;    }};
0 0
原创粉丝点击