From 03634721902e6a82160d6e9509274899fa281c4c Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Sun, 11 Dec 2022 22:34:01 +0330 Subject: 28 --- 28/go.mod | 3 +++ 28/main.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 28/go.mod create mode 100644 28/main.go (limited to '28') diff --git a/28/go.mod b/28/go.mod new file mode 100644 index 0000000..6fe8d22 --- /dev/null +++ b/28/go.mod @@ -0,0 +1,3 @@ +module 28 + +go 1.19 diff --git a/28/main.go b/28/main.go new file mode 100644 index 0000000..e35583f --- /dev/null +++ b/28/main.go @@ -0,0 +1,40 @@ +package main + +import "fmt" + +func strStr(haystack string, needle string) int { + if len(needle) > len(haystack) { + return -1 + } + + needle_len := len(needle) + haystack_len := len(haystack) + noMatch := false + + for i, _ := range haystack { + if needle_len <= haystack_len-i { + for j, _ := range needle { + if needle[j] != haystack[i+j] { + noMatch = true + break + } + } + if noMatch { + noMatch = false + } else { + return i + } + + } else { + return -1 + } + //fmt.Println(i, h) + } + + return -1 +} + +func main() { + // fmt.Println(strStr("sadbutsad", "sad")) + fmt.Println(strStr("mississipi", "a")) +} -- cgit v1.2.3