Your task is to implement the function strstr. The function takes two strings as arguments (s,x) and  locates the occurrence of the string x in the string s. The function returns and integer denoting the first occurrence of the string x in s (0 based indexing).
Note: You are not allowed to use inbuilt function.
 
Example 1:
Input:
s = GeeksForGeeks, x = Fr
Output: -1
Explanation: Fr is not present in the
string GeeksForGeeks as substring.
 
Example 2:
Input:
s = GeeksForGeeks, x = For
Output: 5
Explanation: For is present as substring
in GeeksForGeeks from index 5 (0 based
indexing).
 
Your Task:
You don't have to take any input. Just complete the strstr() function which takes two strings str, target as an input parameter. The function returns -1 if no match if found else it returns an integer denoting the first occurrence of the x in the string s.
 
Expected Time Complexity: O(|s|*|x|)
Expected Auxiliary Space: O(1)
Note : Try to solve the question in constant space complexity.
 
Constraints:
1 <= |s|,|x| <= 100

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #Function to locate the occurrence of the string x in the string s.
def strstr(s,x):
    #code here

Solutions:
def strstr(s, x):
	n = len(s)
	m = len(x)
	if m > n:
		return -1
	for i in range(n - m + 1):
		j = 0
		while j < m:
			if s[i + j] != x[j]:
				break
			j += 1
		if j == m:
			return i
	return -1


def strstr(s, x):
	p = len(s)
	q = len(x)
	if x in s:
		for i in range(p):
			if s[i:i + q] == x:
				return i
	else:
		return -1


def strstr(s, x):
	if x not in s:
		return -1
	k = s.find(x)
	return k


def strstr(s, x):
	l = len(s)
	m = len(x)
	if x in s:
		for i in range(l):
			if s[i:i + m] == x:
				return i
	else:
		return -1


def strstr(s, x):
	if not x:
		return 0
	for i in range(len(s)):
		if s[i:i + len(x)] == x:
			return i
	return -1


def strstr(s, x):
	return s.find(x) if x in s else -1


def strstr(s, x):
	return s.find(x)


def strstr(s, x):
	t = []
	for i in range(len(s)):
		if x[0] in s[i]:
			t.append(i)
	l = -1
	for i in t:
		xx = i
		l = xx
		mj = xx + 1
		c = 1
		for j in range(1, len(x)):
			if mj < len(s) and x[j] == s[mj]:
				mj = mj + 1
				c = c + 1
			else:
				break
		if c == len(x):
			return xx
	return -1


def strstr(s, x):
	cur_pos = 0
	s_ptr = 0
	x_ptr = 0
	while cur_pos < len(s):
		while s[s_ptr] == x[x_ptr]:
			s_ptr += 1
			x_ptr += 1
			if x_ptr >= len(x):
				return cur_pos
			elif s_ptr >= len(s):
				return '-1'
		cur_pos += 1
		s_ptr = cur_pos
		x_ptr = 0
	return '-1'


def strstr(s, x):
	if x in s:
		return s.index(x)
	return -1


def strstr(s, x):
	for i in range(len(s) - len(x) + 1):
		loop = ''
		for j in range(i, len(x) + i):
			loop += s[j]
		if loop == x:
			return i
	return -1


def strstr(s, x):
	m = len(s)
	n = len(x)
	flag = True
	for i in range(m - n + 1):
		flag = True
		for j in range(n):
			if s[i + j] != x[j]:
				flag = False
				break
		if flag and j + 1 == n:
			return i
	return -1


def strstr(s, x):
	if not s or not x:
		return -1
	for i in range(len(s) - len(x) + 1):
		j = 0
		while j < len(x) and s[i + j] == x[j]:
			j += 1
		if j == len(x):
			return i
	return -1


def strstr(s, x):
	n = len(x)
	for i in range(len(s)):
		if s[i:n] == x:
			return i
		n += 1
	return -1


def strstr(s, x):
	los = len(s)
	lox = len(x)
	for i in range(los):
		if s[i] == x[0]:
			tmp = i
			j = 0
			while i < los and j < lox and (s[i] == x[j]):
				i += 1
				j += 1
			if j == lox:
				return tmp
			else:
				tmp = i
	return -1


def strstr(s, x):
	sn = len(s)
	xn = len(x)
	for i in range(sn):
		if s[i] == x[0]:
			tmp = i
			j = 0
			while j < xn and i < sn and (s[i] == x[j]):
				i += 1
				j += 1
			if j == xn:
				return tmp
			else:
				i = tmp
	return -1


def strstr(s, x):
	s = list(s)
	for i in range(len(s)):
		if ''.join(s[i:i + len(x)]) == x:
			return i
	return -1


def strstr(s, x):
	len_x = len(x)
	len_s = len(s)
	for i in range(0, len_s - len_x + 1):
		if s[i:i + len_x] == x:
			return i
	return -1


def strstr(s, x):
	i = 0
	while i < len(s):
		if s[i] == x[0]:
			k = i
			j = 0
			while k < len(s) and j < len(x):
				if s[k] == x[j]:
					k += 1
					j += 1
				else:
					break
			if j == len(x):
				return i
		i += 1
	return -1


def strstr(s, x):
	g = x[0]
	if x in s:
		return s.index(x)
	return -1


def strstr(s, x):
	i = 0
	j = 0
	while i < len(s):
		if s[i] == x[j]:
			j += 1
			if j == len(x):
				return i - j + 1
		else:
			i = i - j
			j = 0
		i += 1
	return -1


def strstr(s, x):
	a = 0
	if x in s:
		a = s.index(x)
	else:
		a = '-1'
	return a


def strstr(s, x):
	try:
		i = s.index(x)
	except:
		return -1
	return i


def strstr(s, x):
	if len(x) > len(s):
		return -1
	for i in range(len(s)):
		if s[i] == x[0] and i + len(x) <= len(s) and (s[i:i + len(x)] == x):
			return i
		elif i + len(x) > len(s):
			break
	return -1


def strstr(s, x):
	l = len(s)
	k = -1
	n = len(x)
	for i in range(l):
		st = s[i:i + n]
		if st == x:
			k = i
			break
	return k


def strstr(s, x):
	ind_s = 0
	while ind_s < len(s):
		if s[ind_s] == x[0]:
			ind_p = 0
			temp_s = ind_s
			while temp_s < len(s) and s[temp_s] == x[ind_p]:
				ind_p += 1
				temp_s += 1
				if ind_p == len(x):
					return ind_s
		ind_s += 1
	return -1


from collections import deque

def strstr(s, x):
	if x not in s:
		return -1
	else:
		for i in range(len(s)):
			if s[i] == x[0]:
				j = 0
				k = i
				temp = ''
				while j < len(x) and k < len(s):
					if s[k] == x[j]:
						temp += s[k]
						k += 1
						j += 1
					else:
						break
				if temp == x:
					return i
	return -1


def strstr(s, x):
	leng = len(x)
	for i in range(len(s)):
		if s[i:i + leng] == x:
			return i
	else:
		return -1


def strstr(s, x):
	lens = len(s)
	lenx = len(x)
	for i in range(lens):
		if s[i] == x[0]:
			pointer = i
			j = 0
			while j < lenx and i < lens and (s[i] == x[j]):
				i += 1
				j += 1
			if j == lenx:
				return pointer
			else:
				i = pointer
	return -1


def strstr(s, x):
	sn = len(s)
	xn = len(x)
	if x not in s:
		return -1
	for i in range(sn - xn + 1):
		if s[i] == x[0]:
			y = 1
			while y < xn:
				if s[i + y] != x[y]:
					break
				y += 1
			if y == xn:
				return i


def strstr(s, x):
	S = str(s)
	for i in range(len(S)):
		if x in S:
			return S.index(x)
		else:
			return -1


def strstr(s, x):
	if x not in s:
		return -1
	if x in s:
		b = s.index(x)
		return b


def strstr(s, x):
	i = 0
	j = 0
	flag = 0
	started_at = 0
	while i < len(s) and j < len(x):
		if s[i] == x[j]:
			if flag == 0:
				started_at = i
				flag = 1
			j += 1
		elif flag == 1:
			flag = 0
			j = 0
			i = started_at
		i += 1
	if flag == 1 and j == len(x):
		return started_at
	return -1


def strstr(s, x):
	if s == x:
		return 0
	if s.find(x):
		return s.find(x)
	return -1