public class Solution {
    public bool RepeatedSubstringPattern(string s) {
        var len = s.Length;
            if (len < 2)
            {
                return false;
            }
            else if (len == 2)
            {
                if (s[0] == s[1])
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                var bound = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(len) / 2));

                for (int i = 1; i <= bound; i++)
                {
                    if (len % i == 0)
                    {
                        var teams = len / i;//共teams组,每组i个字符
                        var orginal = s.Substring(0, i);//第一组的串
                        var count = 0;
                        for (int t = 1; t < teams; t++)
                        {
                            var copy = s.Substring(t * i, i);
                            if (orginal == copy)
                            {
                                count++;
                            }
                            else
                            {
                                break;
                            }
                        }
                        if (count == teams - 1)
                        {
                            return true;
                        }
                    }
                }
                return false;
            }
    }
}

https://leetcode.com/problems/repeated-substring-pattern/#/description

posted on 2017-04-20 11:50  Sempron2800+  阅读(98)  评论(0编辑  收藏  举报