Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 093e97a

Browse files
committed
Add solution 30.
1 parent 7be79dc commit 093e97a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
3+
/**
4+
* @param String $s
5+
* @param String[] $words
6+
* @return Integer[]
7+
*/
8+
function findSubstring($s, $words) {
9+
$sol = [];
10+
if (count($words) == 0 || strlen($words[0]) == 0) return $sol;
11+
$wordLen = strlen($words[0]); $listLen = count($words); $strLen = strlen($s);
12+
$maxIdx = $strLen-$wordLen*$listLen;
13+
if ($maxIdx < 0) return $sol;
14+
$map = [];
15+
foreach ($words as $ss)
16+
$map[$ss] = ($map[$ss] == null ? 0 : $map[$ss]) + 1;
17+
$temp = [];
18+
for ($i = 0; $i < $wordLen; $i++){
19+
for ($j = $i; $j <= $maxIdx; $j+=$wordLen){
20+
for ($k = $listLen-1; $k >= 0; $k--){
21+
$str = substr($s, $k*$wordLen+$j, $wordLen);
22+
$num = ($temp[$str] == null ? 0 : $temp[$str]) + 1;
23+
if ($num > ($map[$str] == null ? 0 : $map[$str])){
24+
$j+=$k*$wordLen;
25+
break;
26+
}else if ($k == 0) {$sol[] = $j;}
27+
else {$temp[$str] = $num;}
28+
}
29+
$temp = [];
30+
}
31+
}
32+
return $sol;
33+
}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /