generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.c
36 lines (30 loc) · 903 Bytes
/
solution.c
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
32
33
34
35
36
#include <stdbool.h>
#include <string.h>
bool checkCommonDivisor(
char* str1, int str1Size, char* str2, int str2Size, int n);
char* gcdOfStrings(char* str1, char* str2) {
int str1Size = strlen(str1);
int str2Size = strlen(str2);
for (int n = str1Size < str2Size ? str1Size : str2Size; n > 0; --n) {
if (str1Size % n != 0) continue;
if (str2Size % n != 0) continue;
if (checkCommonDivisor(str1, str1Size, str2, str2Size, n)) {
str1[n] = 0;
return str1;
}
}
return "";
}
bool checkCommonDivisor(
char* str1, int str1Size, char* str2, int str2Size, int n) {
for (int i = n - 1; i >= 0; --i) {
if (str1[i] != str2[i]) return false;
for (int j = i + n; j < str1Size; j += n) {
if (str1[j] != str1[i]) return false;
}
for (int j = i + n; j < str2Size; j += n) {
if (str2[j] != str2[i]) return false;
}
}
return true;
}