diff --git a/java/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.java b/java/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.java new file mode 100644 index 0000000..055a670 --- /dev/null +++ b/java/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.java @@ -0,0 +1,12 @@ +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; + } +}