Files
Leet-Code-prog/Easy/Common_Divisor_Of_Two_Strings/Solution.cpp
T
2026-05-08 18:57:31 +01:00

13 lines
813 B
C++

class Solution {
public:
string gcdOfStrings(string str1, string str2) {
return (str1 + str2 == str2 + str1)? // if adding str2 to the start is the same as adding it to the end (if they're the same it's just repeated same thing) == true then
str1.substr(0, gcd(size(str1),size(str2))) // whats being returned = SUBSTRING starting from 0 to the gcd ( COMMON DIVISOR FOR ) the size of str1 and str2
: ""; // else return nothing
}
};
// if str1 = BoomBoom and str2 = Boom
// Then str1 + str2 = BoomBoomBoom and str2 + str1 = BoomBoomBoom (they're the same) so we return the substring of str1 starting at 0 to the gcd of the size of str1 and str2
// size of str1 = 8 and size of str2 = 4 so the gcd of 8 and 4 is 4 so we return the substring of str1 starting at 0 to 4 which is Boom