【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 ...
随机推荐
- Zabbix源码安装,使用service命令管理zabbix进程
1. 前期环境: Zabbix源代码解压包:/root/zabbix-3.0.27 Zabbix安装路径:/usr/local/zabbix-3.0.27 2. 复制启动脚本到 ...
- rabbit mq的一个实例,异步功能
简单的使用场景:消息队列的场景有:解耦,异步,削峰. 此例用的场景,异步 有时候会有请求消耗时间过长,不能老让用户等待返回结果,可以用消息队列来做异步实现,之前用过workmain等类似的异步,但不如 ...
- C/C++ Qt 数据库与TreeView组件绑定
在上一篇博文<C/C++ Qt 数据库QSql增删改查组件应用>介绍了Qt中如何使用SQL操作函数,并实现了对数据库的增删改查等基本功能,从本篇开始将实现数据库与View组件的绑定,通过数 ...
- Linux系统中安装软件方法总结
Linux系统中安装软件方法总结 [1]Linux系统中安装软件的几种方式 [2] Linux配置yum源(本地源和网络源) [3] SuSE下zypper源配置 [4] SUSE zypper 本地 ...
- Spring同一个类中的注解方法调用AOP失效问题总结
public interface XxxService { // a -> b void a(); void b(); } @Slf4j public class XxxServiceImpl ...
- iOS11&IPhoneX适配
1.在iOS 11中,会默认开启获取的一个估算值来获取一个大体的空间大小,导致不能正常显示,可以选择关闭.目前尝试在delegate中处理不能很好的解决,不过可以直接设置: Swift if #ava ...
- redis入门到精通系列(四):Jedis--使用java操作redis详解
(一)前言 如果不把数据库和后端语言联系起来,就起不到数据库应该要起到的作用.Java语言通过JDBC操作mysql,用Jedis操作redis.当然了,java操作redis的方式不止jedis一种 ...
- OpenStack之七: compute服务(端口8774)
注意此处的bug,参考o版 官网地址 https://docs.openstack.org/nova/stein/install/controller-install-rdo.html 控制端配置 # ...
- Linux:expr、let、for、while、until、shift、if、case、break、continue、函数、select
1.expr计算整数变量值 格式 :expr arg 例子:计算(2+3)×4的值 1.分步计算,即先计算2+3,再对其和乘4 s=`expr 2 + 3` expr $s \* 4 2.一步完成计算 ...
- centos7.4 64位安装 git
参考博客:Linux Jenkins配置Git 1. git --version 查看有没有安装 过 git,没有则 继续 2. git 压缩包下载地址:https://mirrors.edge.ke ...