Daily LeetCode – day0023 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence

// 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence
class Solution {
    public int isPrefixOfWord(String sentence, String searchWord) {
        String[] words = sentence.split(" ");
        int ans = 1;
        for (String word : words) {
            int i = 0;
            int j = 0;
            while (i < word.length() && j < searchWord.length() && word.charAt(i) == searchWord.charAt(j)) {
                ++i;
                ++j;
                if (j == searchWord.length()) return ans;
            }
            ++ans;
        }
        return -1;
    }
}
学习笔记:
今天又是一道字符串的水题。


关于樊轶群

一个善良的理想主义者。
此条目发表在每日LeetCode分类目录。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。