LeetCode 5843. 作为子字符串出现在单词中的字符串数目
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 5843. 作为子字符串出现在单词中的字符串数目
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
給你一個字符串?dāng)?shù)組 patterns 和一個字符串 word ,統(tǒng)計 patterns 中有多少個字符串是 word 的子字符串。
返回字符串?dāng)?shù)目。
子字符串 是字符串中的一個連續(xù)字符序列。
示例 1: 輸入:patterns = ["a","abc","bc","d"], word = "abc" 輸出:3 解釋: - "a" 是 "abc" 的子字符串。 - "abc" 是 "abc" 的子字符串。 - "bc" 是 "abc" 的子字符串。 - "d" 不是 "abc" 的子字符串。 patterns 中有 3 個字符串作為子字符串出現(xiàn)在 word 中。示例 2: 輸入:patterns = ["a","b","c"], word = "aaaaabbbbb" 輸出:2 解釋: - "a" 是 "aaaaabbbbb" 的子字符串。 - "b" 是 "aaaaabbbbb" 的子字符串。 - "c" 不是 "aaaaabbbbb" 的字符串。 patterns 中有 2 個字符串作為子字符串出現(xiàn)在 word 中。示例 3: 輸入:patterns = ["a","a","a"], word = "ab" 輸出:3 解釋:patterns 中的每個字符串都作為子字符串出現(xiàn)在 word "ab" 中。提示: 1 <= patterns.length <= 100 1 <= patterns[i].length <= 100 1 <= word.length <= 100 patterns[i] 和 word 由小寫英文字母組成來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/number-of-strings-that-appear-as-substrings-in-word
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
2. 解題
class Solution:def numOfStrings(self, patterns: List[str], word: str) -> int:ans = 0for p in patterns:if p in word:ans += 1return ans24 ms 15.1 MB Python3
class Solution { public:int numOfStrings(vector<string>& patterns, string word) {int ans = 0;for(auto& p : patterns){if(word.find(p) != string::npos)ans++;}return ans;} };8 ms 8.4 MB C++
我的CSDN博客地址 https://michael.blog.csdn.net/
長按或掃碼關(guān)注我的公眾號(Michael阿明),一起加油、一起學(xué)習(xí)進步!
總結(jié)
以上是生活随笔為你收集整理的LeetCode 5843. 作为子字符串出现在单词中的字符串数目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 1871. 跳跃游戏
- 下一篇: LeetCode 2105. 给植物浇水