Check If a Word Occurs As a Prefix of Any Word in a Sentence Solutions in C++
Number 1455
Difficulty Easy
Acceptance 64.6%
Link LeetCode
Other languages Go
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence//// Author : liuyubobobo/// Time : 2020-05-23#include <iostream>#include <vector>using namespace std;/// Brute Force: Split and Compare/// Time Complexity: O(n)/// Space Complexity: O(1)class Solution {public:int isPrefixOfWord(string sentence, string searchWord) {int index = 1;for(int start = 0, i = start + 1; i <= sentence.size(); i ++)if(i == sentence.size() || sentence[i] == ' '){string word = sentence.substr(start, i - start);if(searchWord.size() <= word.size() && word.substr(0, searchWord.size()) == searchWord)return index;index ++;start = i + 1;i = start;}return -1;}};int main() {cout << Solution().isPrefixOfWord("i love eating burger", "burg") << endl;// 4cout << Solution().isPrefixOfWord("this problem is an easy problem", "pro") << endl;// 2cout << Solution().isPrefixOfWord("i am tired", "you") << endl;// -1cout << Solution().isPrefixOfWord("i use triple pillow", "pill") << endl;// 4cout << Solution().isPrefixOfWord("hello from the other side", "they") << endl;// -1return 0;}