aboutsummaryrefslogtreecommitdiffstats
path: root/28/main.go
blob: e35583f73c11b931c75ca3124c377d490aec14c5 (plain) (blame)
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
30
31
32
33
34
35
36
37
38
39
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"))
}