LeetCode/java/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.java

13 lines
268 B
Java
Raw Normal View History

class Solution {
public int isPrefixOfWord(String sentence, String searchWord) {
var words = sentence.split(" ");
for (int i = 0; i < words.length; ++i) {
if (words[i].startsWith(searchWord)) {
return 1 + i;
}
}
return -1;
}
}