{"id":6810,"date":"2020-05-23T22:13:48","date_gmt":"2020-05-24T05:13:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6810"},"modified":"2020-05-23T22:15:23","modified_gmt":"2020-05-24T05:15:23","slug":"leetcode-1455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence"},"content":{"rendered":"\n<p>Given a&nbsp;<code>sentence<\/code>&nbsp;that consists of some words separated by a&nbsp;<strong>single space<\/strong>, and a&nbsp;<code>searchWord<\/code>.<\/p>\n\n\n\n<p>You have to check if&nbsp;<code>searchWord<\/code>&nbsp;is a prefix of any word in&nbsp;<code>sentence<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the index of the word<\/em>&nbsp;in&nbsp;<code>sentence<\/code>&nbsp;where&nbsp;<code>searchWord<\/code>&nbsp;is a prefix of this word (<strong>1-indexed<\/strong>).<\/p>\n\n\n\n<p>If&nbsp;<code>searchWord<\/code>&nbsp;is&nbsp;a prefix of more than one word, return the index of the first word&nbsp;<strong>(minimum index)<\/strong>. If there is no such word return&nbsp;<strong>-1<\/strong>.<\/p>\n\n\n\n<p>A&nbsp;<strong>prefix<\/strong>&nbsp;of a string&nbsp;<code>S<\/code>&nbsp;is any leading contiguous substring of&nbsp;<code>S<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> sentence = \"i love eating burger\", searchWord = \"burg\"\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> \"burg\" is prefix of \"burger\" which is the 4th word in the sentence.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> sentence = \"this problem is an easy problem\", searchWord = \"pro\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> \"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.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> sentence = \"i am tired\", searchWord = \"you\"\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> \"you\" is not a prefix of any word in the sentence.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> sentence = \"i use triple pillow\", searchWord = \"pill\"\n<strong>Output:<\/strong> 4\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> sentence = \"hello from the other side\", searchWord = \"they\"\n<strong>Output:<\/strong> -1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= sentence.length &lt;= 100<\/code><\/li><li><code>1 &lt;= searchWord.length &lt;= 10<\/code><\/li><li><code>sentence<\/code>&nbsp;consists of lowercase English letters and spaces.<\/li><li><code>searchWord<\/code>&nbsp;consists of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int isPrefixOfWord(string sentence, string searchWord) {\n    const int n = sentence.size();\n    const int l = searchWord.size();\n    int word = 0;\n    for (int i = 0; i <= n - l; ++i) {\n      if (i == 0 || sentence[i - 1] == ' ') {\n        ++word;\n        bool valid = true;\n        for (int j = 0; j < l &#038;&#038; valid; ++j)\n          valid = valid &#038;&#038; (sentence[i + j] == searchWord[j]);\n        if (valid) return word;      \n      }\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;sentence&nbsp;that consists of some words separated by a&nbsp;single space, and a&nbsp;searchWord. You have to check if&nbsp;searchWord&nbsp;is a prefix of any word in&nbsp;sentence. Return&nbsp;the index&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,97,4,109],"class_list":["post-6810","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-prefix","tag-string","tag-word","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6810"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6810\/revisions"}],"predecessor-version":[{"id":6813,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6810\/revisions\/6813"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}