Problem Solving/LeetCode
2023.02.01 Today's Challenge
fabichoi
2023. 2. 1. 23:45
https://leetcode.com/problems/greatest-common-divisor-of-strings/
Greatest Common Divisor of Strings - LeetCode
Greatest Common Divisor of Strings - 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
leetcode.com
GCD 를 구하면 됨
class Solution:
def gcdOfStrings(self, str1: str, str2: str) -> str:
if str1 + str2 != str2 + str1:
return ""
max_length = gcd(len(str1), len(str2))
return str1[:max_length]
반응형