2025年6月19日 星期四

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

難度: Easy
類型: Two Pointers, String, String Matching
C程式下載: 1455.c

前情題要:
檢查 searchWord 是否為句子中任一個字的開頭文字。
是的話回傳是第幾個字的prefix, 不是的話回傳 -1。

Given a sentence that consists of some words separated by a single space, and a searchWord, check if searchWord is a prefix of any word in sentence.

Return the index of the word in sentence (1-indexed) where searchWord is a prefix of this word. If searchWord is a prefix of more than one word, return the index of the first word (minimum index). If there is no such word return -1.

prefix of a string s is any leading contiguous substring of s.

 

Example 1:

Input: sentence = "i love eating burger", searchWord = "burg"
Output: 4
Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence.

Example 2:

Input: sentence = "this problem is an easy problem", searchWord = "pro"
Output: 2
Explanation: "pro" is prefix of "problem" which is the 2nd and the 6th word in the sentence, but we return 2 as it's the minimal index.

Example 3:

Input: sentence = "i am tired", searchWord = "you"
Output: -1
Explanation: "you" is not a prefix of any word in the sentence.

 

Constraints:

  • 1 <= sentence.length <= 100
  • 1 <= searchWord.length <= 10
  • sentence consists of lowercase English letters and spaces.
  • searchWord consists of lowercase English letters.
思考方式:
1. 空白間隔區分是第幾個字。
2. 檢查 searchword 是否和每個字的前幾個字母相同。

複雜度思考:

Time Complexity: O( N ) 

Space Complexity: O( x )

結果:

Runtime: 0 ms, Beats: 100%

Memory: 7.94 MB, Beats: 25.81%

Accepted
45 / 45 testcases passed
tendchen
tendchen
submitted at Jun 19, 2025 09:54
Runtime
0ms
Beats100.00%
Analyze Complexity
Memory
7.94MB
Beats25.81%
Analyze Complexity
2ms0%50%100%150%
avatar
2ms
Code
C
int isPrefixOfWord(char* sentence, char* searchWord) {
    int i,j,index=0,num_matchedchar=0;
    int len_sentence, len_searchWord, first_char=1;
    len_sentence=strlen(sentence);
    len_searchWord=strlen(searchWord);
    //printf("len_sentence=%d,len_searchWord=%d\n",len_sentence,len_searchWord);
    //printf("sentence=%s, searchWord=%s\n",sentence,searchWord);
    if (len_searchWord==0) return -1;
    for (i=0;i<len_sentence;i++)
    {
        if (sentence[i]==0x20)
        {
            first_char=1;
            index++;
            num_matchedchar=0;
            //printf("index=%d, i=%d\n",index,i);
            continue;
        }
        if (first_char==1)
        {
            //printf("sentence[%d]=%c, searchWord[%d]=%c\n",i,sentence[i],num_matchedchar,searchWord[num_matchedchar]);
            if (sentence[i]==searchWord[num_matchedchar])
            {
                num_matchedchar++;
                //printf("num_matchedchar=%d\n",num_matchedchar);
                if (num_matchedchar==len_searchWord)
                {
                    return index+1;
                }
                continue;
            }
            else
            {
                first_char++;
                continue;
            }
        }
    }
    return -1;
}