<?phpclass Solution { /** * s = "barfoothefoobarman" * words = ["foo", "bar"] * barfoo 和 foobar 满足 * * @param $s * @param $words * @return array */ public function findSubstring($s, $words) { $map = []; $ret = []; if (!$s || !$words) return $ret; $wordsCount = count($words); $wordsLength = strlen($words[0]); foreach ($words as $word) { $map[$word] = isset($map[$word]) ? $map[$word] + 1 : 1; } for ($i = 0; $i <= strlen($s) - $wordsCount * $wordsLength; $i++) { $tmpMap = $map; $had = 0; while($had < $wordsCount) { $str = substr($s, $i + $had * $wordsLength, $wordsLength); echo $str . "\t"; if (!isset($tmpMap[$str]) || $tmpMap[$str] < 1) { break; } $tmpMap[$str] -= 1; $had++; } if ($had == $wordsCount) { $ret[] = $i; } } return $ret; }}$s = "barfoothefoobarman";$words = ["foo", "bar"];$cls = new Solution();$r = $cls->findSubstring($s, $words);print_r($r);