Matej Focko
6522258548
URL: https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/ Signed-off-by: Matej Focko <me@mfocko.xyz>
12 lines
268 B
Java
12 lines
268 B
Java
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;
|
|
}
|
|
}
|