-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_1455_isPrefixOfWord.cc
67 lines (58 loc) · 1.23 KB
/
Problem_1455_isPrefixOfWord.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
bool isPrefix(const string &sentence, int start, int end, const string &searchWord)
{
for (int i = 0; i < searchWord.size(); i++)
{
if (start + i >= end || sentence[start + i] != searchWord[i])
{
return false;
}
}
return true;
}
// 双指针
int isPrefixOfWord(string sentence, string searchWord)
{
int n = sentence.size(), index = 1, start = 0, end = 0;
while (start < n)
{
while (end < n && sentence[end] != ' ')
{
end++;
}
if (isPrefix(sentence, start, end, searchWord))
{
return index;
}
index++;
end++;
start = end;
}
return -1;
}
};
void testIsPrefixOfWord()
{
string str1 = "i love eating burger";
string word1 = "burg";
string str2 = "this problem is an easy problem";
string word2 = "pro";
string str3 = "i am tired";
string word3 = "you";
Solution s;
EXPECT_EQ_INT(4, s.isPrefixOfWord(str1, word1));
EXPECT_EQ_INT(2, s.isPrefixOfWord(str2, word2));
EXPECT_EQ_INT(-1, s.isPrefixOfWord(str3, word3));
EXPECT_SUMMARY;
}
int main()
{
testIsPrefixOfWord();
return 0;
}