-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution1455.go
More file actions
29 lines (23 loc) · 799 Bytes
/
Copy pathsolution1455.go
File metadata and controls
29 lines (23 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package solution1455
import "strings"
// ============================================================================
// 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence
// URL: https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_isPrefixOfWord-24 21756010 65.10 ns/op 64 B/op 1 allocs/op
PASS
*/
func isPrefixOfWord(sentence string, searchWord string) int {
words := strings.Fields(sentence)
for i, word := range words {
if ok := strings.Index(word, searchWord); ok == 0 {
return i + 1
}
}
return -1
}