Given two strings text and pattern, find the first index where pattern exactly matches with any substring of text.  
Return -1 if pattern doesn't exist as a substring in text.
Example 1:
Input:
text = gffgfg
pattern = gfg
Output: 3
Explanation:  Considering 0-based indexing, substring of text from 3rd to last index is gfg.
Example 2:
Input:
text = gffggg
pattern = gfg
Output: -1
Explanation: pattern "gfg" does not exist in the text.
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMatching() which takes the strings text and pattern as the input parameters and returns the first index of matching.
Expected Time Complexity: O(|text| * |pattern|)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ |text|, |pattern| ≤  10^{3}

Source: geeksforgeeks | Difficulty: MEDIUM

Starter code: #User function Template for python3



class Solution:

    def findMatching(self, text, pattern):

        # Code here

Solutions:
class Solution:

	def findMatching(self, text, pattern):
		if pattern in text:
			a = text.find(pattern)
			return a
		else:
			return -1


class Solution:

	def zfunction(self, s):
		n = len(s)
		z = [0] * n
		(l, r) = (0, 0)
		for i in range(1, n):
			if i < r:
				z[i] = min(z[i - l], r - i)
			while i + z[i] < n and s[z[i]] == s[i + z[i]]:
				z[i] += 1
			if i + z[i] > r:
				(l, r) = (i, i + z[i])
		return z

	def findMatching(self, text, pattern):
		z = self.zfunction(pattern + '$' + text)
		n = len(pattern)
		for i in range(n, len(z)):
			if z[i] == n:
				return i - n - 1
		return -1


class Solution:

	def findMatching(self, text, pattern):
		return text.find(pattern)


class Solution:

	def findMatching(self, text, pattern):
		if pattern in text:
			k = len(pattern)
			for i in range(len(text)):
				if pattern == text[i:k + i]:
					return i
		else:
			return -1


class Solution:

	def findMatching(self, haystack, needle):
		if needle == '':
			return 0
		lps = [0] * len(needle)
		(prevLPS, i) = (0, 1)
		while i < len(needle):
			if needle[i] == needle[prevLPS]:
				lps[i] = prevLPS + 1
				prevLPS += 1
				i += 1
			elif prevLPS == 0:
				lps[i] = 0
				i += 1
			else:
				prevLPS = lps[prevLPS - 1]
		i = 0
		j = 0
		while i < len(haystack):
			if haystack[i] == needle[j]:
				(i, j) = (i + 1, j + 1)
			elif j == 0:
				i += 1
			else:
				j = lps[j - 1]
			if j == len(needle):
				return i - len(needle)
		return -1


class Solution:

	def findMatching(self, haystack, needle):
		(hayS, haystackLen) = (0, len(haystack))
		(nedS, nedE) = (0, len(needle) - 1)
		while hayS < haystackLen:
			if haystack[hayS] == needle[nedS] and nedS == nedE:
				return hayS - nedE
			elif haystack[hayS] == needle[nedS]:
				nedS += 1
				hayS += 1
			else:
				hayS = hayS - nedS + 1
				nedS = 0
		return -1 if nedS != nedE + 1 else hayS - nedE


class Solution:

	def findMatching(self, text, pattern):
		ans = []
		if pattern not in text:
			return -1
		for i in range(len(text)):
			if pattern in text:
				k = text.index(pattern)
				ans.append(k)
		return min(ans)


class Solution:

	def findMatching(self, text, pattern):
		leN = len(pattern)
		for i in range(len(text) - leN + 1):
			if text[i:i + leN] == pattern:
				return i
		return -1


class Solution:

	def findMatching(self, text, pattern):
		n = len(text)
		m = len(pattern)
		for i in range(n):
			if text[i:i + m] == pattern:
				return i
		return -1


class Solution:

	def findMatching(self, text, pattern):
		if pattern in text:
			return text.index(pattern)
		return -1


class Solution:

	def findMatching(self, txt, pat):
		(i, j) = (0, 0)
		while i < len(txt):
			prev = i
			while i < len(txt) and j < len(pat) and (txt[i] == pat[j]):
				i += 1
				j += 1
			if j == len(pat):
				return prev
			else:
				i = prev + 1
				j = 0
		return -1


class Solution:

	def findMatching(self, text, pattern):
		n = len(text)
		m = len(pattern)
		i = 0
		j = 0
		while i < n:
			if pattern[j] == text[i]:
				i += 1
				j += 1
				if j >= m:
					return i - m
			else:
				i = i - j + 1
				j = 0
		return -1


class Solution:

	def findMatching(self, text, pattern):
		pl = len(pattern)
		tl = len(text)
		for i in range(0, tl - pl + 1):
			match = True
			for j in range(pl):
				if pattern[j] != text[i + j]:
					match = False
					break
			if match:
				return i
		return -1


class Solution:

	def findMatching(self, text, pattern):
		if pattern not in text:
			return -1
		else:
			j = 0
			for i in range(len(text)):
				if text[i:i + len(pattern)] == pattern:
					return i


class Solution:

	def findMatching(self, text, pattern):
		if pattern in text:
			k = text.find(pattern)
			return k
		return -1


class Solution:

	def findMatching(self, haystack, needle):
		n = len(needle)
		count = 0
		for x in range(len(haystack)):
			c = 0
			if x + n <= len(haystack) and haystack[x] == needle[c]:
				while c != n:
					if haystack[x + c] != needle[c]:
						break
					c += 1
				if c == n:
					return x
		return -1


class Solution:

	def findMatching(self, text, pattern):
		for i in range(len(text) - len(pattern) + 1):
			if text[i] != pattern[0]:
				continue
			found = True
			for j in range(len(pattern)):
				if text[i + j] != pattern[j]:
					found = False
					break
			if found:
				return i
		return -1


class Solution:

	def findMatching(self, text, pattern):
		haystack = text
		needle = pattern
		m = len(haystack)
		n = len(needle)
		lps = [0] * n
		cur = 1
		prev = 0
		while cur < n:
			if needle[cur] == needle[prev]:
				prev += 1
				lps[cur] = prev
				cur += 1
			elif prev == 0:
				lps[cur] = 0
				cur += 1
			else:
				prev = lps[prev - 1]
		ans = -1
		hay = 0
		nee = 0
		while hay < m:
			if haystack[hay] == needle[nee]:
				hay += 1
				nee += 1
			elif nee == 0:
				hay += 1
			else:
				nee = lps[nee - 1]
			if nee == n:
				ans = hay - nee
				return ans
		return ans


class Solution:

	def findMatching(self, text, pattern):
		x = []
		for i in range(0, len(text)):
			x.append(text[i:i + len(pattern)])
		for i in range(len(x)):
			if pattern == x[i]:
				return i
		return -1


class Solution:

	def findMatching(self, text, pat):
		return text.find(pat)


class Solution:

	def findMatching(self, text, pattern):
		for (i, c) in enumerate(text):
			if c == pattern[0]:
				if text[i:i + len(pattern)] == pattern:
					return i
		return -1


class Solution:

	def findMatching(self, text, pattern):
		c = text.__contains__(pattern)
		if c:
			return text.index(pattern)
		else:
			return -1


def lps(s):
	n = len(s)
	LPS = [0] * n
	i = 1
	j = 0
	while i < n:
		if s[i] == s[j]:
			j += 1
			LPS[i] = j
			i += 1
		elif j != 0:
			j = LPS[j - 1]
		else:
			LPS[i] = 0
			i += 1
	return LPS

class Solution:

	def findMatching(self, text, pattern):
		n = len(pattern)
		st = pattern + '$' + text
		LPS = lps(st)
		LPS = LPS[n + 1:]
		ans = -1
		for i in range(0, len(LPS)):
			if LPS[i] == n:
				return i - n + 1
		return -1


class Solution:

	def findMatching(self, text, pattern):
		try:
			return text.find(pattern)
		except:
			return -1


from functools import reduce

class Solution:

	def findMatching(self, text, pattern):
		(hlen, nlen) = (len(text), len(pattern))
		if nlen == 0:
			return 0
		if nlen > hlen or hlen == 0:
			return -1
		rolling = lambda x, y: x * 29 + y
		get_hash = lambda ch: ord(ch) - ord('a')
		nhash = reduce(rolling, map(get_hash, pattern))
		hhash = reduce(rolling, map(get_hash, text[:nlen]))
		if nhash == hhash:
			return 0
		high_base = 29 ** (nlen - 1)
		for i in range(nlen, hlen):
			hhash -= get_hash(text[i - nlen]) * high_base
			hhash = rolling(hhash, get_hash(text[i]))
			if nhash == hhash:
				return i - nlen + 1
		return -1


class Solution:

	def findMatching(self, text, pattern):
		if len(text) < len(pattern) or (len(text) == len(pattern) and text != pattern):
			return -1
		if len(pattern) == 1 and pattern in text:
			return text.index(pattern)
		first = pattern[0]
		n = len(text)
		for i in range(n):
			if text[i] == first:
				k = 1
				for j in range(i + 1, n):
					if k == len(pattern):
						return i
					elif text[j] == pattern[k]:
						k += 1
					else:
						break
				if k == len(pattern):
					return i
				else:
					k = 0
		return -1


class Solution:

	def findMatching(self, text, pattern):
		index = text.find(pattern, 0, len(text))
		return index


class Solution:

	def findMatching(self, text, pattern):
		ans = -1
		n = len(text)
		k = len(pattern)
		for i in range(n - k + 1):
			if text[i:i + k] == pattern:
				ans = i
				break
		return ans