leetcode686. 重复叠加字符串匹配

题目地址

https://leetcode.com/problems/repeated-string-match

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function repeatedStringMatch(string $a, string $b) :int {
$aLength = strlen($a);
$bLength = strlen($b);

for ($i = 0; $i < $aLength; $i++) {
for ($j = 0; $j < $bLength && $a[($i + $j) % $aLength] == $b[$j]; $j++);

if ($j == $bLength) {
return intdiv($i + $j - 1, $aLength) + 1;
}
}

return -1;
}

Leetcode28的变形。