Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8908359

Browse files
1071. Greatest Common Divisor of Strings
1 parent 0c29b6a commit 8908359

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

‎greatest-common-divisor-of-strings.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Runtime: 8 ms
2+
// Memory Usage: 9.3 MB
3+
class Solution {
4+
public:
5+
string gcdOfStrings(string str1, string str2) {
6+
if (str1 == str2) return str1;
7+
8+
if ((str1 + str2) == (str2 + str1)) {
9+
int gcd = __gcd(str1.size(), str2.size());
10+
return str1.substr(0, gcd);
11+
}
12+
return "";
13+
}
14+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /