30 串聯所有單詞的子串

2021-09-12 12:53:19 字數 1171 閱讀 5738

本文參考

一開始的思路是使用遞迴做出words所有結合情況的字典

然後在s中擷取words長度去查字典

結果超時了……

超時**

class solution 

for (int i = 0; i < chang; i++)

}return;

} vectorfindsubstring(string s, vector& words) ;

vectorre;

unordered_maphash;

int c = s.size(), b = words.size();

int f = 0;

for (int i = 0; i < b; i++)

for (auto v : words[i])

f++;

vectorbook;

for (int i = 0; i < b; i++)

book.push_back(0);

dfs(words, b, 0, book, "", hash);

for (int i = 0; i <= c - f; i++)

return re;

}};

別人家的**

不必做出所有情況

每次找出給定單詞長度的子串,看其是否在第乙個雜湊表裡,如果沒有,則break,如果有,則加入第二個雜湊表,但相同的詞只能出現一次,如果多了,也break

class solution 

if (j == n) res.push_back(i);

}return res;

}};

圍觀大神**

class solution ;

vectorres;

int n = s.size(), cnt = words.size(), len = words[0].size();

unordered_mapm1;

for (string w : words) ++m1[w];

for (int i = 0; i < len; ++i) else

}if (count == cnt)

} else }}

return res;

}};

30 串聯所有單詞的子串

給定乙個字串 s 和一些長度相同的單詞words。找出s中恰好可以由words中所有單詞串聯形成的子串的起始位置。注意子串要與words中的單詞完全匹配,中間不能有其他字元,但不需要考慮words中單詞串聯的順序。輸入 s barfoothefoobarman words foo bar 輸出 0,...

30 串聯所有單詞的子串

給定乙個字串s和一些長度相同的單詞words。找出s中恰好可以由words中所有單詞串聯形成的子串的起始位置。注意子串要與words中的單詞完全匹配,中間不能有其他字元,但不需要考慮words中單詞串聯的順序。示例 1 輸入 s barfoothefoobarman words foo bar 輸出...

30 串聯所有單詞的子串

題目.很有意思 首先想到的就是動態規劃,當多乙個字串的字元的時候,要麼增加的這個字元起到了作用,要麼沒起到作用。更進一步,我們不需要之前的狀態,所以動態規劃的陣列也省了。func checkmatch subs string wordsmap map string int wordlen int b...