-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1071-greatest-common-divisor-of-strings.php
167 lines (148 loc) · 4.26 KB
/
1071-greatest-common-divisor-of-strings.php
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
// 1071. Greatest Common Divisor of Strings
// https://leetcode.com/problems/greatest-common-divisor-of-strings/
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 12 ms, faster than 100.00% of PHP online submissions
// for Greatest Common Divisor of Strings.
// Memory Usage: 14.7 MB, less than 100.00% of PHP online submissions
// for Greatest Common Divisor of Strings.
// class Solution
// {
// /**
// * @param String $str1
// * @param String $str2
// * @return String
// */
// function gcdOfStrings($str1, $str2)
// {
// if (strlen($str2) < strlen($str1)) list($str1, $str2) = [$str2, $str1];
// if (strlen($str1) < strlen($str2)) $str2 = substr($str2, strlen($str1));
// if (strlen($str2) < strlen($str1)) list($str1, $str2) = [$str2, $str1];
// $test = '';
// $gcd = '';
// for ($i = 0; $i < strlen($str1); $i++) {
// $test .= $str1[$i];
// if (
// 0 === strlen(str_replace($test, '', $str1))
// && 0 === strlen(str_replace($test, '', $str2))
// ) {
// $gcd = $test;
// }
// }
// return $gcd;
// }
// }
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 4 ms, faster than 100.00% of PHP online submissions
// for Greatest Common Divisor of Strings.
// Memory Usage: 14.8 MB, less than 100.00% of PHP online submissions
// for Greatest Common Divisor of Strings.
// class Solution
// {
// function gcd($a, $b)
// {
// if (0 === $b) return $a;
// return $this->gcd($b, $a % $b);
// }
// /**
// * @param String $str1
// * @param String $str2
// * @return String
// */
// function gcdOfStrings($str1, $str2)
// {
// $gcd = $this->gcd(strlen($str1), strlen($str2));
// $combo = $str1 . $str2;
// $gcdString = substr($combo, 0, $gcd);
// for ($i = $gcd; $i < strlen($combo); $i += $gcd) {
// if ($gcdString !== substr($combo, $i, $gcd)) return '';
// }
// return $gcdString;
// }
// }
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 4 ms, faster than 100.00% of PHP online submissions
// for Greatest Common Divisor of Strings.
// Memory Usage: 14.7 MB, less than 100.00% of PHP online submissions
// for Greatest Common Divisor of Strings.
class Solution
{
function gcd($a, $b)
{
return 0 === $b ? $a : $this->gcd($b, $a % $b);
}
/**
* @param String $str1
* @param String $str2
* @return String
*/
function gcdOfStrings($str1, $str2)
{
if ($str1 . $str2 !== $str2 . $str1) return '';
return substr($str1, 0, $this->gcd(strlen($str1), strlen($str2)));
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
$tests = [
[
'name' => 'ABCABC, ABC',
'input' => [
'str1' => 'ABCABC',
'str2' => 'ABC',
],
'expected' => 'ABC'
],
[
'name' => 'ABABAB, ABAB',
'input' => [
'str1' => 'ABABAB',
'str2' => 'ABAB',
],
'expected' => 'AB'
],
[
'name' => 'ABABAB, ABABC',
'input' => [
'str1' => 'ABABAB',
'str2' => 'ABABC',
],
'expected' => ''
],
[
'name' => 'AB, AB',
'input' => [
'str1' => 'AB',
'str2' => 'AB',
],
'expected' => 'AB'
],
[
'name' => 'ABABABABABAB, ABABABABABAB',
'input' => [
'str1' => 'ABABABABABAB',
'str2' => 'ABABABABABAB',
],
'expected' => 'ABABABABABAB'
],
[
'name' => 'LEET, CODE',
'input' => [
'str1' => 'LEET',
'str2' => 'CODE',
],
'expected' => ''
]
];
$s = new Solution();
foreach ($tests as $test) {
extract($test);
extract($input);
$output = $s->gcdOfStrings($str1, $str2);
if ($expected === $output) {
echo "✅ {$name}<br>";
} else {
echo "🔴 {$name}<br>";
echo "Expected \"{$expected}\" but got \"{$output}\"";
}
echo '<br>';
}