首页 > 试题广场 >

重复的子字符串

[编程题]重复的子字符串
  • 热度指数:1701 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个字符串,请你判断这个字符串是否可以通过重复多次它的子字符串来构成。

数据范围:字符串长度 ,保证字符串中仅包含小写英文字母
示例1

输入

"skjl"

输出

false
示例2

输入

"nowcodernowcoder"

输出

true
最低条件就是回文字符串。若一个字符串满足题解,等价于该字符串必为回文字符串。想到了之前的一道题,故解法相同:
一行代码解决:
return (str+str).substring(1, str.length()*2 - 1).contains(str);

发表于 2022-03-27 21:11:41 回复(2)
import java.util.*;


public class Solution {
 
    public boolean repeatSubstring (String str) {
        return (str + str).substring(1, str.length() * 2 - 1).contains(str);
    }
}

发表于 2022-10-09 22:35:32 回复(0)
class Solution:
    def repeatSubstring(self , s: str) -> bool:
        len_s = len(s)
        sub_str = ''
        for i in s[:-1]:
            sub_str +=i
            count = len_s // len(sub_str)
            new_s = sub_str * count
            if new_s==s:
                return True
        return False

发表于 2022-09-07 10:40:03 回复(0)
class Solution {
public:
    bool repeatSubstring(string str) {
        return (str+str).find(str, 1)!=str.size();
    }
};

发表于 2022-08-05 23:44:41 回复(0)
class Solution:
    def repeatSubstring(self , str: str) -> bool:
        # write code here
        for i in range(len(str)):
            if str[:i] == str[-i:]:
                return True
        return False

发表于 2022-07-06 23:02:20 回复(1)
class Solution:
    def repeatSubstring(self , str: str) -> bool:
        # write code here
        if len(str)==1: return False
        for i in range(1,len(str)-1):
            sub = str[:i+1]
            if len(str) % len(sub) == 0 and sub*(len(str)//len(sub)) == str:
                return True
        return False

发表于 2022-04-22 15:55:58 回复(0)
class Solution:
    def repeatSubstring(self , str ):
        # write code here
        n=len(str)
        for i in range(1,n):
            if n%i == 0:
                if str[:i]*(n/i) ==str:
                    return True
        return False
            
发表于 2022-03-16 22:54:09 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param str string字符串 
# @return bool布尔型
#
class Solution:
    def repeatSubstring(self , str: str) -> bool:
        # write code here
        for i in range(1, len(str)):
            if len(str) % i != 0:
                continue
            if not str.replace(str[:i], ''):
                return True
        return False

发表于 2022-03-03 08:20:28 回复(0)

问题信息

难度:
8条回答 1815浏览

热门推荐

通过挑战的用户

查看代码