置頂

GeoGebra 文章目錄

GeoGebra 文章目錄  更新日期:2018/2/8 我將 GeoGebra 相關的文章及檔案連結都整理在這篇裡,之後如果有新的文章也會同時更新這個目錄。上傳到 GeoGebraTube 的檔案,我有試著用 Google Chrome 63.0.3239.13...

熱門文章

2026年6月29日 星期一

LeetCode 解題筆記:1967. Number of Strings That Appear as Substrings in Word

作者:王一哲
日期:2026年6月29日


LeetCode 題目連結:1967. Number of Strings That Appear as Substrings in Word

解題想法


簡單題。這題用 Python 很簡單,用 sum, for, in 搭配可以一行解。用 C++ 解題可以用 find 找 pattern 是否出現在 word 之中,如果有找到就將答案 cnt 加 1。

Python 程式碼


Runtime: 0 ms, beats 100.00%. Memory: 19.21 MB, beats 58.44%.
class Solution:
    def numOfStrings(self, patterns: List[str], word: str) -> int:
        return sum(patter in word for patter in patterns)


C++ 程式碼


Runtime: 0 ms, beats 100.00%. Memory: 11.80 MB, beats 33.74%.
class Solution {
public:
    int numOfStrings(vector<string>& patterns, string word) {
        int cnt = 0;
        for(auto pattern : patterns) {
            if (word.find(pattern) != string::npos) cnt++;
        }
        return cnt;
    }
};

沒有留言:

張貼留言