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.)
我的思路是如果序列是以重复的子序列组成的,那么每一个子序列的第一个字母肯定是整个序列的第一个字母。找到整个序列所有这个字母的位置k,那么[0,k-1]就是所有的子序列,一个一个子序列试。算法复杂度是0(n)。
public class Solution {
    public boolean repeatedSubstringPattern(String str) {
        if (str.length() == 0 || str == null) {
            return true;
        }
        int i = 1, j = 0;
        int length = 0;
        char[] chars = str.toCharArray();
        char startch = chars[0];
        boolean findstartch = false;
        boolean findsubstr = false;
        int flag = 1;
        while(i < chars.length) {
            if (chars[i] == startch) {
                findstartch = true;
                if (findsubstr == false && flag == 1) {
                    length = i;
                    flag = 0;
                }
            }
            if (findstartch == true) {
                if (chars[j] == chars[i]) {
                    j++;
                } else {
                    j = 0;
                    i = length;
                    findsubstr = false;
                    findstartch = false;
                    flag = 1;
                }
                if (j >= length) {
                    findsubstr = true;
                    j = 0;
                } else {
                    findsubstr = false;
                }
            }
            i++;
        }
        return findsubstr;
    }
}
这道题的o(n)解法是KMP算法,求解next数组那部分。利用求解next数组的思想对这个序列构造next数组,next数组长度等于这个序列的长度。next数组最后一个元素等于0,说明不可能是由重复的子序列组成。如果序列长度等于序列长度减去最后一个元素的倍数,说明是由重复的子序列组成的。下面是代码:

public class Solution {
    public boolean repeatedSubstringPattern(String str) {
        //This is the kmp issue
        int[] prefix = kmp(str);
        int len = prefix[str.length()-1];
        int n = str.length();
        return (len > 0 && n%(n-len) == 0);
    }
    private int[] kmp(String s){
        int len = s.length();
        int[] res = new int[len];
        char[] ch = s.toCharArray();
        int i = 0, j = 1;
        res[0] = 0;
        while(i < ch.length && j < ch.length){
            if(ch[j] == ch[i]){
                res[j] = i+1;
                i++;
                j++;
            }else{
                if(i == 0){
                    res[j] = 0;
                    j++;
                }else{
                    i = res[i-1];
                }
            }
        }
        return res;
    }
}

另一种比较清晰的解法如下:

public boolean repeatedSubstringPattern(String str) {
	int l = str.length();
	for(int i=l/2;i>=1;i--) {
		if(l%i==0) {
			int m = l/i;
			String subS = str.substring(0,i);
			StringBuilder sb = new StringBuilder();
			for(int j=0;j<m;j++) {
				sb.append(subS);
			}
			if(sb.toString().equals(str)) return true;
		}
	}
	return false;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值