-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.php
53 lines (53 loc) · 1.35 KB
/
Solution.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
class Solution {
/**
* @param String $s
* @param String[] $words
* @return Integer[]
*/
function findSubstring($s, $words) {
$cnt = [];
foreach ($words as $w) {
if (!isset($cnt[$w])) {
$cnt[$w] = 1;
} else {
$cnt[$w]++;
}
}
$m = strlen($s);
$n = count($words);
$k = strlen($words[0]);
$ans = [];
for ($i = 0; $i < $k; ++$i) {
$cnt1 = [];
$l = $i;
$r = $i;
$t = 0;
while ($r + $k <= $m) {
$w = substr($s, $r, $k);
$r += $k;
if (!array_key_exists($w, $cnt)) {
$cnt1 = [];
$l = $r;
$t = 0;
continue;
}
if (!isset($cnt1[$w])) {
$cnt1[$w] = 1;
} else {
$cnt1[$w]++;
}
++$t;
while ($cnt1[$w] > $cnt[$w]) {
$remove = substr($s, $l, $k);
$l += $k;
$cnt1[$remove]--;
$t--;
}
if ($t == $n) {
$ans[] = $l;
}
}
}
return $ans;
}
}