题目如下:

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. 1 <= str1.length <= 1000
  2. 1 <= str2.length <= 1000
  3. str1[i] and str2[i] are English uppercase letters.

解题思路:题目比较简单,依次判断str1[0:i]是否满足条件即可。

代码如下:

class Solution(object):
def gcdOfStrings(self, str1, str2):
"""
:type str1: str
:type str2: str
:rtype: str
"""
for i in range(len(str1),0,-1):
if len(str1) % i == 0 and len(str2) % i == 0 and \
str1[:i] * (len(str1) / i ) == str1 and str1[:i] * (len(str2) / i ) == str2:
return str1[:i]
return ''

【leetcode】1071. Greatest Common Divisor of Strings的更多相关文章

  1. 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...

  2. 【Leetcode_easy】1071. Greatest Common Divisor of Strings

    problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrin ...

  3. leetcode 1071 Greatest Common Divisor of Strings

    lc1071 Greatest Common Divisor of Strings 找两个字符串的最长公共子串 假设:str1.length > str2.length 因为是公共子串,所以st ...

  4. LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45

    1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...

  5. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. LeetCode.1071-字符串最大公约数(Greatest Common Divisor of Strings)

    这是小川的第391次更新,第421篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第253题(顺位题号是1071).对于字符串S和T,当且仅当S = T + ... + T ...

  7. 【leetcode】1143. Longest Common Subsequence

    题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...

  8. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...

  9. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

随机推荐

  1. RAC容灾演练

    RAC容灾演练:在节点一进行验证:步骤 操作命令关闭步骤 检测RAC集群资源状态 crsctl status resource -t 关闭监听 srvctl stop listener -n < ...

  2. 封装好日志的类 logging

    import logging from logging import handlers class MyLogger(): def __init__(self,file_name,level='inf ...

  3. Caffe::Snapshot的运行过程

    Snapshot的存储 概述 Snapshot的存储格式有两种,分别是BINARYPROTO格式和hdf5格式.BINARYPROTO是一种二进制文件,并且可以通过修改shapshot_format来 ...

  4. spring map获取同类型的bean

    今天看博客怎么减少if else 方法, 才发现spring 还有很多功能我没有用到,以后真的得花时间学学spring,今天学到的东西如下: 1.定义一个接口 store public interfa ...

  5. jQuery Ajax方法调用 Asp.Net WebService、WebMethod 的详细实例代码

    将以下html存为ws.aspx <%@ Page Language="C#" AutoEventWireup="true" %> <scri ...

  6. 【python+selenium自动化】使用pytest+allure2完成自动化测试报告的输出

    pytest的pytest-html插件是一个很方便的测试报告,运行自动化测试用例时,pytest后加上参数即可 allure是一个测试报告的框架,相比pytest-html的优势就是“逼格” 他的优 ...

  7. DP---DAG、背包、LIS、LCS

    DP是真的难啊,感觉始终不入门路,还是太弱了┭┮﹏┭┮ DAG上的DP ​ 一般而言,题目中如果存在明显的严格偏序关系,并且求依靠此关系的最大/最小值,那么考虑是求DAG上的最短路或者是最长路.(据说 ...

  8. 将字符串转换成C#认可的对象(有键值对的对象)

    var resobj = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JArray>(result ...

  9. ios平台appstore不支持网页内嵌app解决方案

    苹果一直拒绝 UIWebView 内嵌 HTML5 页面的 iPhone.iPad APP应用上架到 App Store,建议这样的APP去做成Safari的Web应用.但是,苹果的审核人员只从界面. ...

  10. HIbernate入门3

    HIbernate的一对多操作: 1. 创建实体类:一个Customer类(客户类)和一个LinkMan类(联系人),两者的关系为:一个客户中可能有多个联系人(关于一对多的实体类之间的关联,不做详细介 ...