博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 459. Repeated Substring Pattern 重复子字符串模式
阅读量:4461 次
发布时间:2019-06-08

本文共 2667 字,大约阅读时间需要 8 分钟。

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: TrueExplanation: It's the substring "ab" twice.

Example 2:

Input: "aba"Output: False

Example 3:

Input: "abcabcabcabc"Output: TrueExplanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)

给定一个非空字符串,判断它是否可以通过自身的子串重复若干次构成。你可以假设字符串只包含小写英文字母,并且长度不会超过10000

解法1: 暴力法Brute Force

解法2:KMP,(简称为KMP算法)可在一个主文本字符串S内查找一个词W的出现位置。

Java:  直接截取了重复验证

public class Solution {    public boolean repeatedSubstringPattern(String str) {        int n = str.length();        for(int i=n/2;i>=1;i--) {            if(n%i==0) {                int m = n/i;                String substring = str.substring(0,i);                StringBuilder sb = new StringBuilder();                for(int j=0;j

  

Python:

class Solution(object):    def repeatedSubstringPattern(self, str):        """        :type str: str        :rtype: bool        """        size = len(str)        for x in range(1, size / 2 + 1):            if size % x:                continue            if str[:x] * (size / x) == str:                return True        return False

Python: KMP

class Solution(object):    def repeatedSubstringPattern(self, str):        """        :type str: str        :rtype: bool        """        size = len(str)        next = [0] * size        for i in range(1, size):            k = next[i - 1]            while str[i] != str[k] and k:                k = next[k - 1]            if str[i] == str[k]:                next[i] = k + 1        p = next[-1]        return p > 0 and size % (size - p) == 0

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;    }};

C++:  

class Solution {public:    bool repeatedSubstringPattern(string str) {        int i = 1, j = 0, n = str.size();        vector
dp(n + 1, 0); while (i < n) { if (str[i] == str[j]) dp[++i] = ++j; else if (j == 0) ++i; else j = dp[j]; } return dp[n] && (dp[n] % (n - dp[n]) == 0); }};

  

类似题目:

[LeetCode] 686. Repeated String Match

 

 

转载于:https://www.cnblogs.com/lightwindy/p/8656842.html

你可能感兴趣的文章
eclipse插件集
查看>>
SQL练习之求解填字游戏
查看>>
2017年11月15日
查看>>
codeforces 949B A Leapfrog in the Array
查看>>
类似懒加载的js功能
查看>>
Mysql的DATE_FORMAT()日期格式转换
查看>>
vue实战教程
查看>>
使用disruptor 将5百多万数据从mysql导入到oracle
查看>>
HDU1028 Ignatius and the Princess III 求一个整数被分为多个数相加有多少种可能
查看>>
团队怎样去做技术规划
查看>>
m_Orchestrate learning system---网站的语言选择功能(中文英文)
查看>>
Linux课程---5、常用文件命令和目录命令(创建文件命令)
查看>>
PHP缓存技术OB系统函数(总结)
查看>>
m_Orchestrate learning system---二十六、动态给封装好的控件添加属性
查看>>
Centos6.5静默安装Oracle 11g
查看>>
比目鱼
查看>>
autofac + owin + webform + mvc + webapi集成demo
查看>>
Maven学习存档(1)——安装
查看>>
hadoop yarn ui applications list 研究
查看>>
shiro(三),使用第三方jdbcRealm连接数据库操作
查看>>