Skip to content

Commit 0799a56

Browse files
committed
feat: add solutions to leetcode problem: No.1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence
1 parent f38a8cd commit 0799a56

File tree

5 files changed

+66
-27
lines changed

5 files changed

+66
-27
lines changed

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,35 @@
8181
<!-- 这里可写当前语言的特殊实现逻辑 -->
8282

8383
```python
84-
84+
class Solution:
85+
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
86+
words = sentence.split(' ')
87+
i, n = 0, len(words)
88+
while i < n:
89+
word = words[i]
90+
if word[: len(searchWord)] == searchWord:
91+
return i + 1
92+
i += 1
93+
return -1
8594
```
8695

8796
### **Java**
8897

8998
<!-- 这里可写当前语言的特殊实现逻辑 -->
9099

91100
```java
92-
101+
class Solution {
102+
public int isPrefixOfWord(String sentence, String searchWord) {
103+
String[] words = sentence.split(" ");
104+
int i = 0, n = words.length;
105+
for (; i < n; ++i) {
106+
if (words[i].indexOf(searchWord) == 0) {
107+
return i + 1;
108+
}
109+
}
110+
return -1;
111+
}
112+
}
93113
```
94114

95115
### **...**

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,33 @@
7171
### **Python3**
7272

7373
```python
74-
74+
class Solution:
75+
def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
76+
words = sentence.split(' ')
77+
i, n = 0, len(words)
78+
while i < n:
79+
word = words[i]
80+
if word[: len(searchWord)] == searchWord:
81+
return i + 1
82+
i += 1
83+
return -1
7584
```
7685

7786
### **Java**
7887

7988
```java
80-
89+
class Solution {
90+
public int isPrefixOfWord(String sentence, String searchWord) {
91+
String[] words = sentence.split(" ");
92+
int i = 0, n = words.length;
93+
for (; i < n; ++i) {
94+
if (words[i].indexOf(searchWord) == 0) {
95+
return i + 1;
96+
}
97+
}
98+
return -1;
99+
}
100+
}
81101
```
82102

83103
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int isPrefixOfWord(String sentence, String searchWord) {
3+
String[] words = sentence.split(" ");
4+
int i = 0, n = words.length;
5+
for (; i < n; ++i) {
6+
if (words[i].indexOf(searchWord) == 0) {
7+
return i + 1;
8+
}
9+
}
10+
return -1;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
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
10+
return -1

test.py

-23
This file was deleted.

0 commit comments

Comments
 (0)