Math Fundamentals18 sections · 814 units
Open in Course

GCD of Strings - Pseudocode

Implementation

function gcdOfStrings(str1, str2):

if str1 + str2 != str2 + str1:

    return ""

g = gcd(len(str1), len(str2))

return str1[0:g]

The concatenation check ensures that both strings are made from the same repeating base. Then GCD of lengths gives the longest base length.