Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-02-01 11:47:06
1071. Greatest Common Divisor of Strings
你给两个字串,求出它们的最大共通整除字串,若一个字串s可以被字串t整除,则s满足:
s = t + t + .. + t
Example:
Input: str1 = "ABABAB", str2 = "ABAB"
Output: "AB"
Input: str1 = "LEET", str2 = "CODE"
Output: ""
思路:
1.若s1和s2存在字串t可以整除他,那么s1 = n*t 且 s2 = m*t 因为它们全为t所组成
所以 s1 + s2 = (n+m)*t,因为组合后的字串只会有t所以 s1+s2 和 s2+s1 必定是
相同的字串,如果不相等就直接返回。
2.因为组合后的字串只有t所以可以把他简化为求最大工因子的问题,用碾转相除法得
到gcd后,返回长度为gcd的子字串即可。
Java Code:
作者: MurasakiSion (紫咲シオン)   2023-02-01 11:49:00
大师
作者: a1234555 (肉宝宝)   2023-02-01 11:51:00
大师
作者: pandix (面包屌)   2023-02-01 12:02:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com