Given a text and a pattern, the task is to check if the pattern exists in the text or not.
Example 1:
Input: text = "geeksforgeeks"
       pat = "geek"
Output: 1
Explanation: "geek" exists in
"geeksforgeeks"
Example 2:
Input: text = "geeksforgeeks"
pat = "gfg"
Output: 0
Explanation: "gfg" does not exists in
"geeksforgeeks"
Your Task:  
You don't need to read input or print anything. Your task is to complete the function search() which takes the string two strings as inputs and returns 1 if the pattern is found, otherwise 0.
Expected Time Complexity: O(|text| + |pat|)
Expected Auxiliary Space: O(|text| + |pat|)
Constraints:
1 ≤ |text|, |pat| ≤ 10^{5}
Both of the string contains lower case English alphabets

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:

	def search(self, text, pat):

		# code here

Solutions:
class Solution:

	def search(self, text, pat):
		for i in range(len(text) - len(pat) + 1):
			if text[i:i + len(pat)] == pat:
				return 1
		return 0


class Solution:

	def search(self, text, pat):
		if pat in text:
			return 1
		else:
			return 0


class Solution:

	def search(self, text, pat):
		try:
			text.index(pat)
			return 1
		except ValueError:
			return 0


class Solution:

	def search(self, text, pat):
		n = len(text)
		m = len(pat)
		i = 0
		while i + m <= n:
			if text[i:i + m] == pat:
				return 1
			else:
				i += 1
		return 0


class Solution:

	def search(self, text, pat):
		return int(pat in text)


class Solution:

	def search(self, text, pat):
		m = len(pat)
		n = len(text)
		if len(pat) > len(text):
			return 0
		count = 0
		for i in range(n - m + 1):
			if text[i:i + m] == pat:
				count += 1
			if count > 1:
				count = 1
		return count


class Solution:

	def search(self, text, pat):
		A = text.find(pat)
		if A == -1:
			return 0
		return 1


import re

class Solution:

	def search(self, text, pat):
		match = re.search(pat, text)
		if match:
			return 1
		else:
			return 0


class Solution:

	def search(self, text, pat):
		if text.__contains__(pat):
			return 1
		return 0


class Solution:

	def search(self, text, pat):
		window = len(pat)
		length = len(text)
		slides = length - window + 1
		for i in range(slides):
			if text[i:window] == pat:
				return 1
			window += 1
		return 0


class Solution:

	def search(self, text, pat):
		M = len(pat)
		N = len(text)
		for i in range(N - M + 1):
			for j in range(M):
				if text[i + j] != pat[j]:
					break
				if j == M - 1:
					return 1
		return 0


class Solution:

	def search(self, text, pat):
		k = text.count(pat)
		if k != 0:
			return 1
		return 0


class Solution:

	def search(self, text, pat):
		s = text.replace(pat, '')
		if len(s) == len(text):
			return 0
		else:
			return 1


class Solution:

	def search(self, text, pat):
		n = len(text)
		m = len(pat)
		for i in range(n - m + 1):
			j = 0
			while j < m:
				if pat[j] != text[i + j]:
					break
				j += 1
			if j == m:
				return 1
		return 0


class Solution:

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


class Solution:

	def search(self, txt, pat):
		n = len(txt)
		m = len(pat)
		lps = [0] * m
		i = 1
		l = 0
		while i < m:
			if pat[i] == pat[l]:
				lps[i] = l + 1
				l += 1
				i += 1
			elif l != 0:
				l = lps[l - 1]
			else:
				lps[i] = 0
				i += 1
		i = 0
		j = 0
		while n - i >= m - j:
			if pat[j] == txt[i]:
				i += 1
				j += 1
			if j == m:
				return 1
			elif i < n and pat[j] != txt[i]:
				if j != 0:
					j = lps[j - 1]
				else:
					i += 1
		return 0


def hash(s):
	h = 0
	for c in s:
		h += ord(c) - 97
	return h

class Solution:

	def search(self, text, pat):
		(n, m) = (len(text), len(pat))
		if m > n:
			return 0
		hashPattern = hash(pat)
		hashText = hash(text[:m])
		for i in range(n - m + 1):
			if i != 0:
				hashText += ord(text[i + m - 1]) - ord(text[i - 1])
			if hashText == hashPattern and pat == text[i:i + m]:
				return 1
		return 0


class Solution:

	def search(self, text, pat):
		ans = text.count(pat)
		if ans > 0:
			return 1
		else:
			return ans


class Solution:

	def search(self, txt, pat):
		if pat in txt:
			return 1
		return 0


class Solution:

	def search(self, text, pat):
		s1 = text
		for i in s1:
			if pat in s1:
				return 1
			else:
				return 0


class Solution:

	def search(self, text, pat):
		if len(pat) > len(text):
			return 0
		else:
			a = text.find(pat)
			if a == -1:
				return 0
			else:
				return 1


class Solution:

	def search(self, text, pat):
		t = text.find(pat)
		if t != -1:
			return 1
		else:
			return 0