鱼C论坛

 找回密码
 立即注册
查看: 1067|回复: 0

[技术交流] 【朱迪的LeetCode刷题笔记】】1071. Greatest Common Divisor of Strings #Python ...

[复制链接]
发表于 2023-6-1 09:43:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Judie 于 2023-5-31 22:40 编辑

For two strings s and t, we say "t divides s" if and only if s = t + ... + t (i.e., t is concatenated with itself one or more times).

Given two strings str1 and str2, return the largest string x such that x divides both str1 and 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: ""


Constraints:
1 <= str1.length, str2.length <= 1000
str1 and str2 consist of English uppercase letters.

Judy
Python stupid way calculating gcd
  1. class Solution(object):
  2.     def gcdOfStrings(self, str1, str2):
  3.         """
  4.         :type str1: str
  5.         :type str2: str
  6.         :rtype: str
  7.         """
  8.         l1 = len(str1)
  9.         l2 = len(str2)
  10.         l0 = min(l1, l2)
  11.         r = ""
  12.         for i0 in range(1,l0+1):
  13.             if l1 % i0 == 0 and l2 % i0 == 0 and str1[:i0] == str2[:i0] and str1.count(str1[:i0]) == l1 / i0 and str2.count(str2[:i0]) == l2 / i0:
  14.                 r = str2[:i0]
  15.         return r
复制代码


Sol1
Python amazing approach!
https://leetcode.com/problems/gr ... p;envId=leetcode-75
  1. class Solution(object):
  2.     def gcdOfStrings(self, str1, str2):
  3.         """
  4.         :type str1: str
  5.         :type str2: str
  6.         :rtype: str
  7.         """

  8.         def gcd(int1, int2):
  9.             if (int2 == 0):
  10.                 return int1
  11.             else:
  12.                 return gcd(int2, int1 % int2)

  13.         if str1 + str2 != str2 + str1:
  14.             return ""
  15.         maxlen = gcd(len(str1), len(str2))
  16.         return str1[:maxlen]
复制代码


Gray
C++ smarter way calculating gcd
  1. class Solution {
  2. public:
  3. int gcd(int a, int b) // The function runs recursive in nature to return GCD
  4. {

  5.     if (a == 0) // If a becomes zero
  6.        return b; // b is the GCD
  7.     if (b == 0)// If b becomes zero
  8.        return a;// a is the GCD

  9.     if (a == b) // The case of equal numbers
  10.         return a; // return any one of them

  11.    // Apply case of substraction
  12.     if (a > b) // if a is greater subtract b
  13.         return gcd(a-b, b);
  14.     return gcd(a, b-a); //otherwise subtract a
  15. }
  16.     string gcdOfStrings(string str1, string str2) {
  17.         int c = gcd(str1.size(),str2.size());
  18.         bool test = true;
  19.         string temp = str2.substr(0,c);
  20.         for(int i = 0; i < str1.size()/c; i++){
  21.             if(str1.substr(ic,c) != temp){
  22.                 return "";
  23.             }
  24.         }
  25.         for(int i = 0; i < str2.size()/c; i++){
  26.             if(str2.substr(ic,c) != temp){
  27.                 return "";
  28.             }
  29.         }
  30.         return temp;
  31.     }
  32. };
复制代码


Lemma & Proof

Lemma & Proof

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-21 05:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表