码迷,mamicode.com
首页 > 其他好文 > 详细

459 Repeated Substring Pattern 重复的子字符串

时间:2018-04-21 16:12:06      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:nbsp   www.   desc   解释   string   .com   size   bst   cpp   

给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。
示例 1:
输入: "abab"
输出: True
解释: 可由子字符串 "ab" 重复两次构成。

示例 2:
输入: "aba"
输出: False

示例 3:
输入: "abcabcabcabc"
输出: True
解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。)
详见:https://leetcode.com/problems/repeated-substring-pattern/description/

C++:

class Solution {
public:
    bool repeatedSubstringPattern(string str) 
    {
        int n = str.size();
        for (int i = n / 2; i >= 1; --i) 
        {
            if (n % i == 0)
            {
                int c = n / i;
                string t = "";
                for (int j = 0; j < c; ++j)
                {
                    t += str.substr(0, i); 
                }
                if (t == str)
                {
                    return true;
                }
            }
        }
        return false;
    }
};

 参考:https://www.cnblogs.com/grandyang/p/6087347.html

459 Repeated Substring Pattern 重复的子字符串

标签:nbsp   www.   desc   解释   string   .com   size   bst   cpp   

原文地址:https://www.cnblogs.com/xidian2014/p/8901681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!