comments | difficulty | edit_url | tags | |||
---|---|---|---|---|---|---|
true |
困难 |
|
给定一个字符串 s
和一个字符串数组 words
。 words
中所有字符串 长度相同。
s
中的 串联子串 是指一个包含 words
中所有字符串以任意顺序排列连接起来的子串。
- 例如,如果
words = ["ab","cd","ef"]
, 那么"abcdef"
,"abefcd"
,"cdabef"
,"cdefab"
,"efabcd"
, 和"efcdab"
都是串联子串。"acdbef"
不是串联子串,因为他不是任何words
排列的连接。
返回所有串联子串在 s
中的开始索引。你可以以 任意顺序 返回答案。
示例 1:
输入:s = "barfoothefoobarman", words = ["foo","bar"]
输出:[0,9]
解释:因为 words.length == 2 同时 words[i].length == 3,连接的子字符串的长度必须为 6。
子串 "barfoo" 开始位置是 0。它是 words 中以 ["bar","foo"] 顺序排列的连接。
子串 "foobar" 开始位置是 9。它是 words 中以 ["foo","bar"] 顺序排列的连接。
输出顺序无关紧要。返回 [9,0] 也是可以的。
示例 2:
输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
输出:[]
解释:因为 words.length == 4 并且 words[i].length == 4,所以串联子串的长度必须为 16。
s 中没有子串长度为 16 并且等于 words 的任何顺序排列的连接。
所以我们返回一个空数组。
示例 3:
输入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"] 输出:[6,9,12] 解释:因为 words.length == 3 并且 words[i].length == 3,所以串联子串的长度必须为 9。 子串 "foobarthe" 开始位置是 6。它是 words 中以 ["foo","bar","the"] 顺序排列的连接。 子串 "barthefoo" 开始位置是 9。它是 words 中以 ["bar","the","foo"] 顺序排列的连接。 子串 "thefoobar" 开始位置是 12。它是 words 中以 ["the","foo","bar"] 顺序排列的连接。
提示:
1 <= s.length <= 104
1 <= words.length <= 5000
1 <= words[i].length <= 30
words[i]
和s
由小写英文字母组成
我们用哈希表
我们可以枚举滑动窗口的起点
每一次,我们提取字符串
时间复杂度
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
cnt = Counter(words)
m, n = len(s), len(words)
k = len(words[0])
ans = []
for i in range(k):
l = r = i
cnt1 = Counter()
while r + k <= m:
t = s[r : r + k]
r += k
if cnt[t] == 0:
l = r
cnt1.clear()
continue
cnt1[t] += 1
while cnt1[t] > cnt[t]:
rem = s[l : l + k]
l += k
cnt1[rem] -= 1
if r - l == n * k:
ans.append(l)
return ans
class Solution {
public List<Integer> findSubstring(String s, String[] words) {
Map<String, Integer> cnt = new HashMap<>();
for (var w : words) {
cnt.merge(w, 1, Integer::sum);
}
List<Integer> ans = new ArrayList<>();
int m = s.length(), n = words.length, k = words[0].length();
for (int i = 0; i < k; ++i) {
int l = i, r = i;
Map<String, Integer> cnt1 = new HashMap<>();
while (r + k <= m) {
var t = s.substring(r, r + k);
r += k;
if (!cnt.containsKey(t)) {
cnt1.clear();
l = r;
continue;
}
cnt1.merge(t, 1, Integer::sum);
while (cnt1.get(t) > cnt.get(t)) {
String w = s.substring(l, l + k);
if (cnt1.merge(w, -1, Integer::sum) == 0) {
cnt1.remove(w);
}
l += k;
}
if (r - l == n * k) {
ans.add(l);
}
}
}
return ans;
}
}
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
unordered_map<string, int> cnt;
for (const auto& w : words) {
cnt[w]++;
}
vector<int> ans;
int m = s.length(), n = words.size(), k = words[0].length();
for (int i = 0; i < k; ++i) {
int l = i, r = i;
unordered_map<string, int> cnt1;
while (r + k <= m) {
string t = s.substr(r, k);
r += k;
if (!cnt.contains(t)) {
cnt1.clear();
l = r;
continue;
}
cnt1[t]++;
while (cnt1[t] > cnt[t]) {
string w = s.substr(l, k);
if (--cnt1[w] == 0) {
cnt1.erase(w);
}
l += k;
}
if (r - l == n * k) {
ans.push_back(l);
}
}
}
return ans;
}
};
func findSubstring(s string, words []string) (ans []int) {
cnt := make(map[string]int)
for _, w := range words {
cnt[w]++
}
m, n, k := len(s), len(words), len(words[0])
for i := 0; i < k; i++ {
l, r := i, i
cnt1 := make(map[string]int)
for r+k <= m {
t := s[r : r+k]
r += k
if _, exists := cnt[t]; !exists {
cnt1 = make(map[string]int)
l = r
continue
}
cnt1[t]++
for cnt1[t] > cnt[t] {
w := s[l : l+k]
cnt1[w]--
if cnt1[w] == 0 {
delete(cnt1, w)
}
l += k
}
if r-l == n*k {
ans = append(ans, l)
}
}
}
return
}
function findSubstring(s: string, words: string[]): number[] {
const cnt: Map<string, number> = new Map();
for (const w of words) {
cnt.set(w, (cnt.get(w) || 0) + 1);
}
const ans: number[] = [];
const [m, n, k] = [s.length, words.length, words[0].length];
for (let i = 0; i < k; i++) {
let [l, r] = [i, i];
const cnt1: Map<string, number> = new Map();
while (r + k <= m) {
const t = s.substring(r, r + k);
r += k;
if (!cnt.has(t)) {
cnt1.clear();
l = r;
continue;
}
cnt1.set(t, (cnt1.get(t) || 0) + 1);
while (cnt1.get(t)! > cnt.get(t)!) {
const w = s.substring(l, l + k);
cnt1.set(w, cnt1.get(w)! - 1);
if (cnt1.get(w) === 0) {
cnt1.delete(w);
}
l += k;
}
if (r - l === n * k) {
ans.push(l);
}
}
}
return ans;
}
public class Solution {
public IList<int> FindSubstring(string s, string[] words) {
var cnt = new Dictionary<string, int>();
foreach (var w in words) {
if (cnt.ContainsKey(w)) {
cnt[w]++;
} else {
cnt[w] = 1;
}
}
var ans = new List<int>();
int m = s.Length, n = words.Length, k = words[0].Length;
for (int i = 0; i < k; ++i) {
int l = i, r = i;
var cnt1 = new Dictionary<string, int>();
while (r + k <= m) {
var t = s.Substring(r, k);
r += k;
if (!cnt.ContainsKey(t)) {
cnt1.Clear();
l = r;
continue;
}
if (cnt1.ContainsKey(t)) {
cnt1[t]++;
} else {
cnt1[t] = 1;
}
while (cnt1[t] > cnt[t]) {
var w = s.Substring(l, k);
cnt1[w]--;
if (cnt1[w] == 0) {
cnt1.Remove(w);
}
l += k;
}
if (r - l == n * k) {
ans.Add(l);
}
}
}
return ans;
}
}
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]++;
} else {
$cnt[$w] = 1;
}
}
$ans = [];
$m = strlen($s);
$n = count($words);
$k = strlen($words[0]);
for ($i = 0; $i < $k; $i++) {
$l = $i;
$r = $i;
$cnt1 = [];
while ($r + $k <= $m) {
$t = substr($s, $r, $k);
$r += $k;
if (!isset($cnt[$t])) {
$cnt1 = [];
$l = $r;
continue;
}
if (isset($cnt1[$t])) {
$cnt1[$t]++;
} else {
$cnt1[$t] = 1;
}
while ($cnt1[$t] > $cnt[$t]) {
$w = substr($s, $l, $k);
$cnt1[$w]--;
if ($cnt1[$w] == 0) {
unset($cnt1[$w]);
}
$l += $k;
}
if ($r - $l == $n * $k) {
$ans[] = $l;
}
}
}
return $ans;
}
}