leetcode:1668. 最大重复子字符串(python3解法)

难度:简单

给你一个字符串 sequence ,如果字符串 word 连续重复 k 次形成的字符串是 sequence 的一个子字符串,那么单词 word重复值为 k 。单词 word 大重复值 是单词 wordsequence 中最大的重复值。如果 word 不是 sequence 的子串,那么重复值 k0

给你一个字符串 sequenceword ,请你返回 最大重复值 k

示例 1:

复制代码
输入:sequence = "ababc", word = "ab"
输出:2
解释:"abab" 是 "ababc" 的子字符串。

示例 2:

复制代码
输入:sequence = "ababc", word = "ba"
输出:1
解释:"ba" 是 "ababc" 的子字符串,但 "baba" 不是 "ababc" 的子字符串。

示例 3:

复制代码
输入:sequence = "ababc", word = "ac"
输出:0
解释:"ac" 不是 "ababc" 的子字符串。

提示:

  • 1 <= sequence.length <= 100
  • 1 <= word.length <= 100
  • sequenceword 都只包含小写英文字母。

题解:

python 复制代码
class Solution:
    def maxRepeating(self, sequence: str, word: str) -> int:
        c = 0
        i = 1 
        while True:
            if word * i in sequence:
                c += 1
            i += 1
            if word * i not in sequence:
                break
        return c
相关推荐
xoliu13 分钟前
Pytorch核心基础入门
人工智能·pytorch·python
一瞬祈望6 分钟前
ResNet50 图像分类完整实战(Notebook Demo + 训练代码)
人工智能·python·神经网络·数据挖掘
leiming67 分钟前
c++ string 容器
开发语言·c++·算法
其美杰布-富贵-李8 分钟前
PyTorch Lightning Callback 指南
人工智能·pytorch·python·回调函数·callback
_codemonster19 分钟前
python易混淆知识点(十六)lambda表达式
开发语言·python
wljun7391 小时前
六、OrcaSlicer 切片之区域
算法·切片软件 orcaslicer
superman超哥1 小时前
仓颉Option类型的空安全处理深度解析
c语言·开发语言·c++·python·仓颉
2401_841495641 小时前
【LeetCode刷题】跳跃游戏Ⅱ
数据结构·python·算法·leetcode·数组·贪心策略·跳跃游戏
leaves falling1 小时前
动态规划讲解
算法·动态规划
Data_agent1 小时前
OOPBUY模式淘宝1688代购系统搭建指南
开发语言·爬虫·python