From 65222585483aef48952387edc7dfa25c39cffafa Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 2 Dec 2024 23:09:47 +0100 Subject: [PATCH] =?UTF-8?q?java:=20add=20=C2=AB1455.=20Check=20If=20a=20Wo?= =?UTF-8?q?rd=20Occurs=20As=20a=20Prefix=20of=20Any=20Word=20in=20a=20Sent?= =?UTF-8?q?ence=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/ Signed-off-by: Matej Focko --- ...occurs-as-a-prefix-of-any-word-in-a-sentence.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 java/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.java 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; + } +}