Check If a Word Occurs As a Prefix of Any Word in a Sentence LeetCode solution in Java

1
class Solution {
2
public int isPrefixOfWord(String sentence, String searchWord) {
3
if (!sentence.contains(searchWord)) return -1;
4
boolean y = false;
5
String[] str = sentence.split(" ");
6

7
for (int i = 0; i < str.length; i++) {
8
if (str[i].contains(searchWord)) {
9
for (int j = 0; j < searchWord.length(); j++) {
10
if (str[i].charAt(j) != searchWord.charAt(j)) {
11
y = true;
12
break;
13
}
14
}
15
if (!y) {
16
return i + 1;
17
}
18
}
19
y = false;
20
}
21

22
return -1;
23
}
24
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0