C#LeetCode刷题之#459-重复的子字符串(Repeated Substring Pattern)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3945 访问。
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。
输入: "abab"
输出: True
解释: 可由子字符串 "ab" 重复两次构成。
输入: "aba"
输出: False
输入: "abcabcabcabc"
输出: True
解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。)
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.
Input: "abab"
Output: True
Explanation: It's the substring "ab" twice.
Input: "aba"
Output: False
Input: "abcabcabcabc"
Output: True
Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3945 访问。
public class Program {
public static void Main(string[] args) {
var s = "Iori's Blog";
var res = RepeatedSubstringPattern(s);
Console.WriteLine(res);
s = "abaababaab";
res = RepeatedSubstringPattern2(s);
Console.WriteLine(res);
Console.ReadKey();
}
private static bool RepeatedSubstringPattern(string s) {
//LeetCode 超出内存限制,未AC
//基本思路是一个指针截断字符串
//指针前所有的字符串为子字符串参考计算
//往后循环看每一段是否相同
//如果每一段都相同,则满足题意
for(var i = 0; i < s.Length / 2; i++) {
var sub = s.Substring(0, i + 1);
if(s.Length % sub.Length != 0) continue;
var nums = s.Length / sub.Length;
for(var j = 1; j < nums; j++) {
if(s.Substring(j * sub.Length, sub.Length) != sub) break;
if(j == nums - 1) return true;
}
}
return false;
}
private static bool RepeatedSubstringPattern2(string s) {
//基本思路是一个指针截断字符串
//指针前所有的字符串为子字符串参考计算
//以此字符串循环构建新字符
//比较新构建的字符串是否和原串相同
//变量 repeat 不能使用字符串,否则效率较低
//LeetCode 会TLE超时无法AC
var length = s.Length;
for(var i = length / 2; i >= 1; i--) {
if(length % i == 0) {
var repeat = new StringBuilder();
for(var j = 0; j < length / i; j++) {
repeat.Append(s.Substring(0, i));
}
if(repeat.ToString() == s) return true;
}
}
return false;
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3945 访问。
False
True
分析:
显而易见,以上2种算法的时间复杂度均为: 。
C#LeetCode刷题之#459-重复的子字符串(Repeated Substring Pattern)的更多相关文章
- LeetCode 459. 重复的子字符串(Repeated Substring Pattern)
459. 重复的子字符串 459. Repeated Substring Pattern 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且 ...
- [Swift]LeetCode459. 重复的子字符串 | Repeated Substring Pattern
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- Java实现 LeetCode 459 重复的子字符串
459. 重复的子字符串 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" ...
- Leetcode 459.重复的子字符串
重复的子字符串 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: ...
- leetcode刷题3.无重复字符的最长子串
题目:给定一个字符串,找出不含有重复字符的 最长子串 的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. ...
- C#LeetCode刷题之#680-验证回文字符串 Ⅱ(Valid Palindrome II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3961 访问. 给定一个非空字符串 s,最多删除一个字符.判断是否 ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- C#LeetCode刷题-树
树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历 61.6% 中等 95 不同的二叉搜索树 II 43.4% 中等 96 不同的二叉搜索树 51.6% 中等 98 验证二叉搜索树 ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
随机推荐
- web自动化测试实战之生成测试报告
同志们,老铁们,继上篇文章 web自动化测试实战之批量执行测试用例 之后我们接着继续往下走,有人说我们运行了所有测试用例,控制台输入的结果,如果很多测试用例那也不能够清晰快速的知道多少用例通过率以及错 ...
- OSCP Learning Notes - Buffer Overflows(1)
Introduction to Buffer Overflows Anatomy of Memory Anatomy of the Stack Fuzzing Tools: Vulnserver - ...
- Zabbix4.x如何安全传输数据
由于设备都在混合云,所以不少数据传输是通过公网,这样极大的增加了危险性,所以在Zabbix数据传输这块则进行PSK安全认证,由proxy主动收集agent数据后统一发送给server,这样只需要对pr ...
- C# 判断和创建目录路径
在进行一些导出或下载时,需要创建一个本地路径,以供文件进行下载和创建. if (Directory.Exists(Server.MapPath("~/upimg/hufu")) = ...
- idea中maven导入依赖报红的解决办法
使用idea创建maven项目,maven导入依赖报红,从以下几个步骤排查解决问题: 1.首先查看maven的安装和配置有没有问题.那么,要看那些内容呢.maven的安装位置.maven的settin ...
- git pull & git fetch
Git中从远程的分支获取最新的版本到本地有这样2个命令:1. git fetch:相当于是从远程获取最新版本到本地,不会自动merge git fetch origin mastergit log ...
- LaTeX公式学习
简介 本文公式较多可能有加载较慢. 使用 LaTeX 的主要原因之一是它可以方便地排版公式.我们使用数学模式来排版公式. 公式 插入公式 可以用一对$来启用数学模式. 行中公式可以用如下方法: $数学 ...
- 基于.Net Core的Redis实现查询附近的地理信息
1.使用的Redis客户端为:ServiceStack.Redis 2.Redis 中的 GEORedis是我们最为熟悉的K-V数据库,它常被拿来作为高性能的缓存数据库来使用,大部分项目都会用到它.从 ...
- 利用div显示隐藏实现的分页效果
实现步骤: 1.创建对应切换div <div class="bottom_daohang"> <div class="bottom_daohang_zo ...
- 如何查看Oracle的版本
本人使用的软件是DataGrip 在控制台输入 select * from v$version;