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 9110c21

Browse files
committed
Solve 1071. Greatest Common Divisor of Strings
1 parent da4639b commit 9110c21

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
3+
fun gcdOfStrings(str1: String, str2: String): String {
4+
val str1Str2 = str1 + str2
5+
val str2Str1 = str2 + str1
6+
7+
if (str1Str2 != str2Str1) {
8+
return ""
9+
}
10+
11+
var str1Length = str1.length
12+
var str2Length = str2.length
13+
14+
while (str1Length != str2Length) {
15+
when {
16+
str1Length > str2Length -> str1Length -= str2Length
17+
else -> str2Length -= str1Length
18+
}
19+
}
20+
21+
return str1.substring(0, str1Length)
22+
}
23+
24+
}

0 commit comments

Comments
(0)

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