-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcdOfStrings.java
More file actions
31 lines (26 loc) · 999 Bytes
/
gcdOfStrings.java
File metadata and controls
31 lines (26 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public String gcdOfStrings(String str1, String str2) {
// Find the GCD of the lengths of str1 and str2
int gcdLength = gcd(str1.length(), str2.length());
// The potential GCD string is the substring of str1 from 0 to gcdLength
String candidate = str1.substring(0, gcdLength);
// Check if this candidate can form both str1 and str2
if (canForm(str1, candidate) && canForm(str2, candidate)) {
return candidate;
}
return ""; // If no valid string found
}
// Helper function to check if a string can be formed by repeating another string
private boolean canForm(String s, String t) {
return s.length() % t.length() == 0 && (t.repeat(s.length() / t.length()).equals(s));
}
// Helper function to compute GCD
private int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}