【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/greatest-common-divisor-of-strings/
题目描述
For strings S
and T
, we say "T divides S"
if and only if S = T + ... + T
(T concatenated with itself 1 or more times)
Return the largest string X
such that X
divides str1
and X
divides str2
.
Example 1:
Input: str1 = "ABCABC", str2 = "ABC"
Output: "ABC"
Example 2:
Input: str1 = "ABABAB", str2 = "ABAB"
Output: "AB"
Example 3:
Input: str1 = "LEET", str2 = "CODE"
Output: ""
Note:
- 1 <= str1.length <= 1000
- 1 <= str2.length <= 1000
- str1[i] and str2[i] are English uppercase letters.
题目大意
求两个字符串的最长公共重复子串。重复子串是指原字符串可以有其子串重复若干次得到。
解题方法
暴力遍历
最长公共重复子串重复若干次之后能分别得到str1和str2,那么最明显地,该子串的长度一定是str1和str2长度的公因数。看了一下字符串的长度最多只有1000,所以我们完全可以对长度进行遍历,判断每个公因数是不是构成最长公共重复子串。因为要找最长的,所以找到最长之后,直接返回即可。
时间复杂度O(N^2)。外部循环找到公因数,时间复杂度O(N);内部要创建新的字符串和原先的字符串进行比较,时间复杂度也是O(N)。
Python代码如下:
class Solution(object):
def gcdOfStrings(self, str1, str2):
"""
:type str1: str
:type str2: str
:rtype: str
"""
l1, l2 = len(str1), len(str2)
shorter = min(l1, l2)
res = ""
for i in range(shorter, 0, -1):
if l1 % i or l2 % i:
continue
t1, t2 = l1 // i, l2 // i
gcd = str1[:i]
rebuild1 = gcd * t1
rebuild2 = gcd * t2
if rebuild1 == str1 and rebuild2 == str2:
res = gcd
break
return res
C++代码如下:
class Solution {
public:
string gcdOfStrings(string str1, string str2) {
int l1 = str1.size();
int l2 = str2.size();
int shorter = min(l1, l2);
string res;
for (int i = shorter; i >= 1; --i) {
if ((l1 % i == 0) && (l2 % i == 0)) {
int t1 = l1 / i;
int t2 = l2 / i;
string gcd = str1.substr(0, i);
string rebuild1 = genRepeatStr(t1, gcd);
string rebuild2 = genRepeatStr(t2, gcd);
if ((rebuild1 == str1) && (rebuild2 == str2)) {
res = gcd;
break;
}
}
}
return res;
}
string genRepeatStr(int times, string substr) {
string res;
while (times--) {
res += substr;
}
return res;
}
};
日期
2019 年 6 月 8 日 —— 刷题尽量不要停
【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)的更多相关文章
- leetcode 1071 Greatest Common Divisor of Strings
lc1071 Greatest Common Divisor of Strings 找两个字符串的最长公共子串 假设:str1.length > str2.length 因为是公共子串,所以st ...
- 【Leetcode_easy】1071. Greatest Common Divisor of Strings
problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrin ...
- 【leetcode】1071. Greatest Common Divisor of Strings
题目如下: For strings S and T, we say "T divides S" if and only if S = T + ... + T (T concate ...
- LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45
1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...
- LeetCode.1071-字符串最大公约数(Greatest Common Divisor of Strings)
这是小川的第391次更新,第421篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第253题(顺位题号是1071).对于字符串S和T,当且仅当S = T + ... + T ...
- [Swift]LeetCode1071.字符串的最大公因子 | Greatest Common Divisor of Strings
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
随机推荐
- QQ空间技术架构之深刻揭秘
QQ空间技术架构之深刻揭秘 来源: 腾讯大讲堂 发布时间: 2012-05-17 17:24 阅读: 7822 次 推荐: 4 [收藏] QQ 空间作为腾讯海量互联网服务产品,经过近七年 ...
- 8 — springboot中静态资源处理方式 - 前后端分离 这没屁用
7中说了thymeleaf,哪还有一个目录是static 那么就来研究一下静态资源 静态资源,springboot底层是怎么去装配的,都在WebMvcAutoConfiguration有答案,去看一下 ...
- 学习java的第二十一天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- 19. 删除链表的倒数第 N 个结点
目录 19.删除链表的倒数第N个节点 题目 题解-暴力 题解-哈希表 题解-双指针 19.删除链表的倒数第N个节点 题目 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点. 输入:he ...
- 日常Java 2021/10/25
ArrayList存储数字 import java.util.ArrayList; public class Arr_test { public static void main(String[] a ...
- Identity Server 4 从入门到落地(七)—— 控制台客户端
前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...
- 【Reverse】初遇花指令
解密花指令 全文参考了一个大师傅的blog:https://blog.csdn.net/zhangmiaoping23/article/details/38400393 介绍 花指令是对抗反汇编的有效 ...
- Struts 2 基础篇【转】
转载至 : http://www.blogjava.net/huamengxing/archive/2009/10/21/299153.html Struts2架构图 有视频讲解百度一下就可以找到了 ...
- 容器的分类与各种测试(二)——vector部分用法
向量 vector 是一种对象实体, 能够容纳许多其他类型相同的元素, 因此又被称为容器. 与string相同, vector 同属于STL(Standard Template Library, 标准 ...
- [学习总结]5、Android的ViewGroup中事件的传递机制(二)
下面是第一篇的连接 Android的ViewGroup中事件的传递机制(一) 关于onInterceptTouchEvent和onTouchEvent的详细解释. 1 public class Mai ...