[leetcode, JS] 1071. Greatest Common Divisor of Strings

mxxn·2023년 9월 4일
0

leetcode

목록 보기
63/198

문제

문제 링크 : Greatest Common Divisor of Strings

풀이

/**
 * @param {string} str1
 * @param {string} str2
 * @return {string}
 */
var gcdOfStrings = function(str1, str2) {
    if (str1 + str2 != str2 + str1) return ''
    let str1Leng = str1.length
    let str2Leng = str2.length

    let GCD = (x,y) => {
        if(!y) return x
        return GCD(y, x%y)
    }

    let div = GCD(str1Leng , str2Leng)
    return str1.slice(0, div)
};
  1. str1,2의 길이로 최대공약수를 구한다고 생각하고 풀이
  • Runtime 47 ms, Memory 42.3 MB
profile
내일도 글쓰기

0개의 댓글