Skip to content

Commit ec5850f

Browse files
committed
feat: add solutions to lc problem: No.1455
No.1455.Check If a Word Occurs As a Prefix of Any Word in a Sentence
1 parent c2c11c9 commit ec5850f

File tree

6 files changed

+105
-30
lines changed

6 files changed

+105
-30
lines changed

solution/1400-1499/1455.Check If a Word Occurs As a Prefix of Any Word in a Sentence/README.md

+43-10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:字符串分割**
57+
58+
将 $sentence$ 按空格分割为 $words$,然后遍历 $words$,检查 $words[i]$ 是否是 $searchWord$ 的前缀,是则返回 $i+1$。若遍历结束,所有单词都不满足,返回 $-1$。
59+
60+
时间复杂度 $O(mn)$。其中 $m$ 是 $sentence$ 的长度,$n$ 是 $searchWord$ 的长度。
61+
62+
5663
<!-- tabs:start -->
5764

5865
### **Python3**
@@ -62,13 +69,9 @@
6269
```python
6370
class Solution:
6471
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
65-
words = sentence.split(' ')
66-
i, n = 0, len(words)
67-
while i < n:
68-
word = words[i]
69-
if word[: len(searchWord)] == searchWord:
70-
return i + 1
71-
i += 1
72+
for i, s in enumerate(sentence.split(), 1):
73+
if s.startswith(searchWord):
74+
return i
7275
return -1
7376
```
7477

@@ -80,9 +83,8 @@ class Solution:
8083
class Solution {
8184
public int isPrefixOfWord(String sentence, String searchWord) {
8285
String[] words = sentence.split(" ");
83-
int i = 0, n = words.length;
84-
for (; i < n; ++i) {
85-
if (words[i].indexOf(searchWord) == 0) {
86+
for (int i = 0; i < words.length; ++i) {
87+
if (words[i].startsWith(searchWord)) {
8688
return i + 1;
8789
}
8890
}
@@ -91,6 +93,37 @@ class Solution {
9193
}
9294
```
9395

96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
int isPrefixOfWord(string sentence, string searchWord) {
102+
stringstream ss(sentence);
103+
string s;
104+
for (int i = 1; ss >> s; ++i) {
105+
if (s.find(searchWord) == 0) {
106+
return i;
107+
}
108+
}
109+
return -1;
110+
}
111+
};
112+
```
113+
114+
### **Go**
115+
116+
```go
117+
func isPrefixOfWord(sentence string, searchWord string) int {
118+
for i, s := range strings.Split(sentence, " ") {
119+
if strings.HasPrefix(s, searchWord) {
120+
return i + 1
121+
}
122+
}
123+
return -1
124+
}
125+
```
126+
94127
### **...**
95128

96129
```

solution/1400-1499/1455.Check If a Word Occurs As a Prefix of Any Word in a Sentence/README_EN.md

+36-10
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,9 @@
5454
```python
5555
class Solution:
5656
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
57-
words = sentence.split(' ')
58-
i, n = 0, len(words)
59-
while i < n:
60-
word = words[i]
61-
if word[: len(searchWord)] == searchWord:
62-
return i + 1
63-
i += 1
57+
for i, s in enumerate(sentence.split(), 1):
58+
if s.startswith(searchWord):
59+
return i
6460
return -1
6561
```
6662

@@ -70,9 +66,8 @@ class Solution:
7066
class Solution {
7167
public int isPrefixOfWord(String sentence, String searchWord) {
7268
String[] words = sentence.split(" ");
73-
int i = 0, n = words.length;
74-
for (; i < n; ++i) {
75-
if (words[i].indexOf(searchWord) == 0) {
69+
for (int i = 0; i < words.length; ++i) {
70+
if (words[i].startsWith(searchWord)) {
7671
return i + 1;
7772
}
7873
}
@@ -81,6 +76,37 @@ class Solution {
8176
}
8277
```
8378

79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
int isPrefixOfWord(string sentence, string searchWord) {
85+
stringstream ss(sentence);
86+
string s;
87+
for (int i = 1; ss >> s; ++i) {
88+
if (s.find(searchWord) == 0) {
89+
return i;
90+
}
91+
}
92+
return -1;
93+
}
94+
};
95+
```
96+
97+
### **Go**
98+
99+
```go
100+
func isPrefixOfWord(sentence string, searchWord string) int {
101+
for i, s := range strings.Split(sentence, " ") {
102+
if strings.HasPrefix(s, searchWord) {
103+
return i + 1
104+
}
105+
}
106+
return -1
107+
}
108+
```
109+
84110
### **...**
85111

86112
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int isPrefixOfWord(string sentence, string searchWord) {
4+
stringstream ss(sentence);
5+
string s;
6+
for (int i = 1; ss >> s; ++i) {
7+
if (s.find(searchWord) == 0) {
8+
return i;
9+
}
10+
}
11+
return -1;
12+
}
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func isPrefixOfWord(sentence string, searchWord string) int {
2+
for i, s := range strings.Split(sentence, " ") {
3+
if strings.HasPrefix(s, searchWord) {
4+
return i + 1
5+
}
6+
}
7+
return -1
8+
}

solution/1400-1499/1455.Check If a Word Occurs As a Prefix of Any Word in a Sentence/Solution.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
class Solution {
22
public int isPrefixOfWord(String sentence, String searchWord) {
33
String[] words = sentence.split(" ");
4-
int i = 0, n = words.length;
5-
for (; i < n; ++i) {
6-
if (words[i].indexOf(searchWord) == 0) {
4+
for (int i = 0; i < words.length; ++i) {
5+
if (words[i].startsWith(searchWord)) {
76
return i + 1;
87
}
98
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
class Solution:
22
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
3-
words = sentence.split(' ')
4-
i, n = 0, len(words)
5-
while i < n:
6-
word = words[i]
7-
if word[: len(searchWord)] == searchWord:
8-
return i + 1
9-
i += 1
3+
for i, s in enumerate(sentence.split(), 1):
4+
if s.startswith(searchWord):
5+
return i
106
return -1

0 commit comments

Comments
 (0)