459. Repeated Substring Pattern
创始人
2025-05-30 15:56:17

The Description of the problem

Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
Example 1:

Input: s = “abab”
Output: true
Explanation: It is the substring “ab” twice.

Example 2:

Input: s = “aba”
Output: false

The solutions via string perspective

class Solution {
public:bool repeatedSubstringPattern(string s) {int n = s.size();string ss = s + s;ss = ss.substr(1, 2 * n - 2);return ss.find(s) != string::npos;}
};
class Solution:def repeatedSubstringPattern(self, s: str) -> bool:n = len(s)ss = (s + s)[1: 2 * n - 1]return s in ss

The Explanations

This solution checks if the given string s can be constructed by repeating a substring of itself. The intuitive idea behind this solution is that if s can be constructed by repeating a substring of itself, then it must appear at least twice in ss, which is formed by concatenating two copies of s and removing the first and last characters. For example, let’s say s = "abab". Then ss = "abababab". After removing the first and last characters, we get ss = "bababa". Since "ab" appears twice in "bababa", we can conclude that "abab" can be constructed by repreating the substring "ab".


The function return true if s is found in ss using the find() method of the C++ string class. If it’s not found, it returns false.

The solutions via iteration

class Solution {
public:bool repeatedSubstringPattern(string s) {int n = s.size();for (int i = 1; i * 2 <= n; i++) {if (n % i == 0) {bool match = true;for (int j = i; j < n; j++) {if (s[j] != s[j - i]) {match = false;}}if (match) return true;}}return false;}
};
class Solution:def repeatedSubstringPattern(self, s: str) -> bool:n = len(s)for i in range(1, n //2 + 1):if n % i == 0:if all(s[j] == s[j - i] for j in range(i, n)):return Truereturn False

The Explanations

The intuitive idea behind this solution to the “Repeated Substring Pattern” problem is to check all possible substrings of the given string s and see if they can be repeated to form the original string. If any such substring is found, it returns true, otherwise it returns false.


The function first calculates the length of s and store it in the variable n. Then it iterates over all possible substring lengths from 1 to n/2. For each substring length i, it checks if n is divisible by i. If it is, then it’s possible that a substring of length i can be repeated to form the original string.


The function then checks if all substrings of length i are equal. If they are, then it returns true, indicating that the original string can be constructed by repeating a substring of itself. If no such substring is found after checking all possible lengths, the function returns false.


For example, let’s say we have a string "abab". The function will first check it its length 4 is divisible by 1. Since it is not divisible by 1, we move on to checking if its length is divisible by 2. Since 4 is divisible by 2, we check if all substrings of length 2 are equal. In this case, "ab" and "ab" are equal so we return ture.

The solutions with next array in KMP

class Solution {
public:bool repeatedSubstringPattern(string s) {int n = s.size();vector next = {0};int pos = 0;for (int i = 1; i < n; i++) {while (pos != 0 && s[pos] != s[i]) {pos = next[pos - 1];}if (s[pos] == s[i]) pos++;next.push_back(pos);}return next[n-1] && (n % (n - next[n-1]) == 0);}
};

相关内容

热门资讯

Docker等容器技术如何与移... 移动应用程序的开发面临着很多挑战,包括开发环境的设置、测试的困难、部署的复杂性等。由于...
【微服务】—— Nacos安装... 文章目录1. Windows安装1.1 下载安装包1.2 解压1.3 端口配置1.4 启动1.5 访...
【OpenGL】 为了理解这个函数我们需要先学习一些OpenGL的内容 OpenGL可视化 https://g...
hjr-详细说一下Redis集... Redis作用 缓存 一般我们用Redis做缓存,热点数据 击穿:访问到...
【蓝桥杯】 C++ 数字三角形... 文章目录题目描述输入描述输出描述实现代码解题思路注意点知识点 题目描述 上图给出了一个数字三角形。从...
VR全景展会丨探索未来,重塑现... 随着科技的不断发展,虚拟现实(VR)技术逐渐成为一个重要的...
C++数据类型 目录 C++基础数据类型 指针 指针类型 指针赋值 引用 参考:《深...
超实用!!! 三分钟将你的项目... 文章目录前言一、在项目中新增配置二、配置github page setting?三、如...
数据结构---队列 专栏:数据结构 个人主页:HaiFan. 专栏简介:这里是...
数字操作方法 系列文章目录 前端系列文章——传送门 JavaScript系列文章——传送门 文章目录系列文章目录...
Cartesi 2023 年 ... 查看 Cartesi Machine、Cartesi Rollups 和 Noether 的更新正在...
JavaWeb——jsp概述入... JSP定义:  在如下一个jsp文件里面有如下的代码  <%@ page content...
一切喜怒哀乐都来自于你的认知 01 有个学子,准备出国,父母请来清华的教授宁向东。请问教授࿱...
JAVA并发编程——synch... 引言         Java语言为了解决并发编程中存在的原子性、可见性和有序性问题,...
git学习----3.21 未... 文章目录前言Git :一个分布式版本控制工具目标一、概述1.1 开发中的实际场景1.2...
Qt优秀开源项目之十七:QtP... QtPromise是Promises/A+规范的Qt/C++实现。该规范的译...
【前端八股文】JavaScri... 文章目录Set概念与arr的比较属性和方法并集、交集、差集Map概念属性和方法String用索引值和...
海康硬盘录像机接入RTSP/o... EasyNVR安防视频云服务平台可支持设备通过RTSP/Onvif协议接入平台,能提供...
在混合劳动力时代如何避免网络安... 在混合劳动力时代如何避免安全网络风险 三年多来,混合工作一直是工作生活中不可或缺的一...
2023还不懂Jmeter接口... 这里介绍的Jmeter接口测试的的实战,如果文章内容没遇看懂的话,我这边...
基于4G/5G弱网聚合的多链路... 基于4G/5G多卡聚合(弱网聚合)的智能融合通信设备技术亮点 增强带宽提供可靠连接 通过将多个有线和...
如何使用Synplify综合v... 文章目录使用Synplify综合的好处synplify的教程方法1(无效)...
2023年全国最新高校辅导员精... 百分百题库提供高校辅导员考试试题、辅导员考试预测题、高校辅导员考试真题、辅导员证考试题库等ÿ...
2022年18个值得期待的Le... 有数百个独特的LearnDash附加组件,您可能很难选择您的LearnDash LMS...
【java基础】Stream流... 文章目录基本介绍流的创建流的各种常见操作forEach方法filter方法map方法peek方法fl...
javaweb高校行政办公自动... 本课题基于我国高校管理信息化建设现状,结合在实际工作中所遇到的问题和收获,...
一款专门为自动化测试打造的集成... 你好,我是不二。 随着行业内卷越来越严重,自动化测试已成为测试工程师的...
【go-zero】golang... 一、casbin 概览 1、casbin基本了解 casbin的GitHub:https://git...
现在开发低代码平台算晚吗? 现在开发低代码平台算晚吗?作为低代码的亲戚——零代码厂商,这篇就以“厂商...
【JavaWeb】书城项目(2... 222.书城项目-第三阶段:修改所有html页面为jsp页面 改成jsp页面之后&#x...