?題目來源:
? ? ? ? leetcode題目,網(wǎng)址:1967. 作為子字符串出現(xiàn)在單詞中的字符串?dāng)?shù)目 - 力扣(LeetCode)
解題思路:
? ? ? ? 遍歷字符串?dāng)?shù)組,根據(jù) word.indexOf(pattern) 的返回值是否為 -1 判斷改字符串是否為單詞的字符串并對其計數(shù)即可。
解題代碼:
class Solution {
public int numOfStrings(String[] patterns, String word) {
int res=0;
for(String pattern:patterns){
if(word.indexOf(pattern)!=-1){
res++;
}
}
return res;
}
}
總結(jié):
? ? ? ? 官方題解給出了暴力匹配和 KMP 兩種解法。上次看懂的KMP算法有忘了,重新看一遍......文章來源:http://www.zghlxwxcb.cn/news/detail-540273.html
? ? ? ? 貼一個 KMP 算法鏈接,下次碰到時就不用找了。?前綴函數(shù)與 KMP 算法 - OI Wiki (oi-wiki.org)文章來源地址http://www.zghlxwxcb.cn/news/detail-540273.html
到了這里,關(guān)于題目:1967.作為子字符串出現(xiàn)在單詞中的字符串?dāng)?shù)組的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!