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,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
随机推荐
- Ethical Hacking - Web Penetration Testing(3)
EXPLOITATION -File Upload VULNS Simple type of vulnerabilities. Allow users to upload executable fil ...
- CSS:有点难的面试题①
1 举例说明匿名块框和匿名行内框2 什么是标准文档流?3 inline-block遵循怎样的渲染规则?4 什么是BFC?如何触发BFC?5 什么是Line box?(最好画图说明) 6 <met ...
- C++语法小记---重载逻辑操作符
重载逻辑操作符 不建议重载逻辑操作符 原因:无法实现逻辑操作符的短路功能(即:不需要计算完全部表达式就可以得出结果) 逻辑操作符:|| && 操作符重载本质上是函数调用,而进行函数调用 ...
- java 如何正确的输出集合或者对象的值
java 如何正确的输出集合或者对象的值 一般out.println(Object) 和 System.out.println(Object),其中输出的都是Object.toString()方法.重 ...
- 剑指offo记录
一.二维数组中的查找 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是 ...
- vue学习(六) 事件修饰符 stop prevent capture self once
//html <div id="app"> <div @click="divHandler" style="height:150px ...
- pycharm 退出虚拟环境
pycharm 内置虚拟环境 venv 如果要退出就直接 deactivate 命令就行 运行的话直接在命令行输python3 文件名
- zookeeper 源码编译
环境:mac 1.github上下载 源码 项目地址:https://github.com/apache/zookeeper 2.安装 ant mac:brew update -> brew ...
- PHP 命名空间(namespace)定义
PHP 命名空间(namespace) PHP 命名空间(namespace)是在PHP 5.3中加入的,如果你学过C#和Java,那命名空间就不算什么新事物. 不过在PHP当中还是有着相当重要的意义 ...
- Python os.tmpnam() 方法
概述 os.tmpnam() 方法用于为创建一个临时文件返回一个唯一的路径.高佣联盟 www.cgewang.com 语法 tmpnam()方法语法格式如下: os.tmpnam 参数 无 返回值 返 ...