repeated-substring-pattern

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

下面这个方法,开始我觉得挺好。可惜还是超时了。后来我就加了一个剪枝策略,只有长度能够整除总长度的子串,才需要进行比较。

package com.company;

import java.util.*;

class Solution {
    public boolean repeatedSubstringPattern(String str) {

        for (int i=1; i<=str.length()/2; i++) {
            if (str.length() % i != 0) {
                continue;
            }
            StringBuilder sb = new StringBuilder();
            sb.append(str.substring(i));
            sb.append(str.substring(0, i));
            if (str.equals(sb.toString())) {
                return true;
            }
        }
        return false;
    }
}

public class Main {

    public static void main(String[] args) throws InterruptedException {

        System.out.println("Hello!");
        Solution solution = new Solution();

        // Your Codec object will be instantiated and called as such:
        String str = "abcabcabcc";
        boolean ret = solution.repeatedSubstringPattern(str);
        System.out.printf("ret:%b\n", ret);

        System.out.println();

    }

}

下面是开始超时的方法,少了一个剪枝条件。

package com.company;

import java.util.*;

class Solution {
    public boolean repeatedSubstringPattern(String str) {

        for (int i=1; i<=str.length()/2; i++) {
            StringBuilder sb = new StringBuilder();
            sb.append(str.substring(i));
            sb.append(str.substring(0, i));
            if (str.equals(sb.toString())) {
                return true;
            }
        }
        return false;
    }
}

public class Main {

    public static void main(String[] args) throws InterruptedException {

        System.out.println("Hello!");
        Solution solution = new Solution();

        // Your Codec object will be instantiated and called as such:
        String str = "abcabcabcc";
        boolean ret = solution.repeatedSubstringPattern(str);
        System.out.printf("ret:%b\n", ret);

        System.out.println();

    }

}
时间: 2024-11-20 07:03:51

repeated-substring-pattern的相关文章

43. leetcode 459. Repeated Substring Pattern

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 an

459. Repeated Substring Pattern【easy】

459. Repeated Substring Pattern[easy] 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 o

LeetCode 459. 重复的子字符串(Repeated Substring Pattern)

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

LeetCode 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 100

459. Repeated Substring Pattern

https://leetcode.com/problems/repeated-substring-pattern/#/description 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 consis

[LeetCode] 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 100

KMP - LeetCode #459 Repeated Substring Pattern

复习一下KMP算法 KMP的主要思想是利用字符串自身的前缀后缀的对称性,来构建next数组,从而实现用接近O(N)的时间复杂度完成字符串的匹配 对于一个字符串str,next[j] = k 表示满足str[0...k-1] = str[j-k...j-1]的最大的k,即对于子串str[0...j-1],前k个字母等于后k个字母 现在求解str的next数组: 初始化:next[0] = -1 那么在知道了next[j]的情况下,如何递推地求出next[j+1]呢?分两种情况(令k=next[j]

【LeetCode】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 100

Leetcode: 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 100

[Swift]LeetCode459. 重复的子字符串 | 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 100