Given a string str and another string patt. Find the minimum index of the character in str that is also present in patt.
Example 1:
Input:
str = geeksforgeeks
patt = set
Output: 1
Explanation: e is the character which is
present in given str "geeksforgeeks"
and is also presen in patt "set". Minimum
index of e is 1. 
Example 2:
Input:
str = adcffaet
patt = onkl
Output: -1
Explanation: There are none of the
characters which is common in patt
and str.
Your Task:
You only need to complete the function minIndexChar() that returns the index of answer in str or returns -1 in case no character of patt is present in str.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Number of distinct characters).
Constraints:
1 ≤ |str|,|patt| ≤ 10^{5 }
'a' <= str_{i}, patt_{i} <= 'z'

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    

    #Function to find the minimum indexed character.

    def minIndexChar(self,Str, pat): 

        #code here

Solutions:
class Solution:

	def minIndexChar(self, Str, pat):
		d = {}
		l = []
		for i in range(len(Str)):
			if Str[i] not in d:
				d[Str[i]] = i
		for i in pat:
			if i in d:
				l.append(d[i])
		if len(l) > 0:
			return min(l)
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		d = dict()
		min_index = 999999
		n = len(Str)
		m = len(pat)
		for i in range(n):
			if Str[i] not in d:
				d[Str[i]] = i
		for j in range(m):
			if pat[j] in d:
				if d[pat[j]] < min_index:
					min_index = d[pat[j]]
		if min_index != 999999:
			return min_index
		else:
			return -1


class Solution:

	def minIndexChar(self, Str, pat):
		for i in range(len(Str)):
			if Str[i] in pat:
				return i
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		mini = float('inf')
		str_set = set(Str)
		for i in range(len(pat)):
			ch = pat[i]
			if ch in str_set:
				x = Str.index(ch)
				mini = min(mini, x)
		if mini == float('inf'):
			return -1
		return mini


from collections import defaultdict

class Solution:

	def minIndexChar(self, Str, pat):
		dict = defaultdict(set)
		for ch in pat:
			dict[ch] = 0
		for s in range(len(Str)):
			if Str[s] in dict:
				return s
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		for (tmp_char_index, tmp_char) in enumerate(Str):
			if tmp_char in pat:
				return tmp_char_index
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		l1 = len(Str)
		l2 = len(pat)
		minimum = l1
		for i in pat:
			if i in Str:
				d = Str.index(i)
				if d < minimum:
					minimum = d
		if minimum < l1:
			return minimum
		else:
			return -1


class Solution:

	def minIndexChar(self, Str, pat):
		d = {}
		p = 0
		for i in pat:
			if i not in d:
				d[i] = 1
		for j in Str:
			p += 1
			if j in d:
				return p - 1
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		d = dict()
		for x in pat:
			if x not in d:
				d[x] = 1
		for (ind, x) in enumerate(Str):
			if x in d:
				return ind
		return '-1'


from collections import Counter

class Solution:

	def minIndexChar(self, Str, pat):
		mp = Counter(pat)
		for i in range(len(Str)):
			if mp.get(Str[i], 0) > 0:
				return i
		return -1


class Solution:

	def minIndexChar(self, str, pat):
		for i in str:
			if i in pat:
				return str.index(i)
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		for (i, c) in enumerate(Str):
			if c in pat:
				return i
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		chars = [-1] * 256
		for i in range(len(Str)):
			if chars[ord(Str[i])] != -1:
				continue
			chars[ord(Str[i])] = i
		res = len(Str)
		for ele in pat:
			if chars[ord(ele)] != -1:
				res = min(res, chars[ord(ele)])
		return res if res != len(Str) else -1


class Solution:

	def minIndexChar(self, S, pat):
		m = 0
		l = []
		for i in pat:
			if i in S:
				l.append(S.index(i))
		if l == []:
			return -1
		else:
			return min(l)


class Solution:

	def minIndexChar(self, Str, pat):
		index1 = 100
		ans = -1
		for i in pat:
			try:
				a = Str.index(i)
				if a < index1:
					index1 = a
					ans = a
			except:
				continue
		return ans


class Solution:

	def minIndexChar(self, Str, pat):
		a1 = []
		for i in pat:
			if i in Str:
				a1.append(Str.index(i))
		if len(a1) == 0:
			return -1
		else:
			return min(a1)


class Solution:

	def minIndexChar(self, Str, pat):
		ARR = []
		for i in Str:
			if i in pat:
				ARR.append(i)
		if len(ARR) == 0:
			return -1
		else:
			for i in range(len(Str)):
				if Str[i] == ARR[0]:
					return i


class Solution:

	def minIndexChar(self, Str, pat):
		min = 99999
		for i in pat:
			if i in Str and Str.index(i) < min:
				min = Str.index(i)
		if min != 99999:
			return min
		else:
			return -1


class Solution:

	def minIndexChar(self, Str, pat):
		d = {}
		for i in pat:
			d[i] = 1
		for i in range(len(Str)):
			if d.get(Str[i], 0) > 0:
				return i
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		for i in Str:
			if i in pat:
				return Str.index(i)
				break
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		(first, second) = ([x for x in Str], [x for x in pat])
		temp = list(set(first).intersection(set(second)))
		answer = [Str.index(i) for i in temp]
		if answer == []:
			return -1
		else:
			return min(answer)


import sys

class Solution:

	def minIndexChar(self, Str, pat):
		min1 = sys.maxsize
		for i in range(len(pat)):
			ch = pat[i]
			if ch in Str:
				x = Str.index(ch)
				min1 = min(min1, x)
		if min1 == sys.maxsize:
			return -1
		else:
			return min1


class Solution:

	def minIndexChar(self, Str, pat):
		mp = {}
		for i in range(len(pat)):
			if pat[i] not in mp:
				mp[pat[i]] = 1
		for j in range(len(Str)):
			if Str[j] in mp:
				return j
		return -1


from collections import defaultdict

class Solution:

	def minIndexChar(self, Str, pat):
		minind = len(Str)
		ind = len(Str)
		p = defaultdict(list)
		s = defaultdict(list)
		for i in range(len(pat)):
			p[pat[i]].append(i)
		for i in range(len(Str)):
			s[Str[i]].append(i)
		for i in p:
			if i in s:
				ind = s[i][0]
				minind = min(ind, minind)
		if minind == len(Str):
			return -1
		return minind


class Solution:

	def minIndexChar(self, Str, pat):
		c = {}
		for i in pat:
			if i in c:
				c[i] += 1
			else:
				c[i] = 1
		for j in range(len(Str)):
			if Str[j] in c:
				return j
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		d = {}
		for i in Str:
			d[i] = 1
		for j in pat:
			d[j] = 2
		for i in range(len(Str)):
			if d[Str[i]] == 2:
				return i
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		a = len(Str)
		l = []
		for k in pat:
			if k in Str:
				a = Str.index(k)
				l.append(a)
		if len(l) == 0:
			return -1
		else:
			return min(l)


class Solution:

	def minIndexChar(self, strr, pat):
		p = []
		for i in range(len(pat)):
			x = strr.find(pat[i])
			if x >= 0:
				p.append(x)
		if len(p) > 0:
			return min(p)
		else:
			return -1


class Solution:

	def minIndexChar(self, Str, pat):
		d = {}
		min_index = len(Str)
		for i in range(len(Str)):
			if Str[i] not in d:
				d[Str[i]] = i
		for i in pat:
			if i in d and d[i] < min_index:
				min_index = d[i]
		if min_index == len(Str):
			return -1
		return min_index


class Solution:

	def minIndexChar(self, Str, pat):
		myset = set([ch for ch in pat])
		for (i, ch) in enumerate(Str):
			if ch in myset:
				return i
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		min = 99999
		for i in range(len(pat)):
			ch = pat[i]
			if ch in Str:
				x = Str.index(ch)
				if min > x:
					min = x
		if min == 99999:
			return -1
		return min


class Solution:

	def minIndexChar(self, Str, pat):
		c = len(Str)
		d = 0
		for i in pat:
			if i in Str:
				c = min(Str.index(i), c)
				d = 1
		if d == 0:
			return -1
		else:
			return c


class Solution:

	def minIndexChar(self, Str, pat):
		min_index = float('inf')
		for char in Str:
			if char in pat and Str.index(char) < min_index:
				min_index = Str.index(char)
		if min_index == float('inf'):
			return -1
		return min_index


class Solution:

	def minIndexChar(self, Str, pat):
		sl = len(Str)
		pl = len(pat)
		l = 0
		flag = -1
		for i in range(len(Str)):
			if Str[i] in pat:
				flag = i
				break
		if flag == -1:
			return -1
		else:
			return flag


class Solution:

	def minIndexChar(self, Str, pat):
		patt = set(pat)
		for (i, char) in enumerate(Str):
			if char in patt:
				return i
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		d = {}
		for i in Str:
			if i not in d:
				d[i] = 1
				for j in pat:
					if i == j:
						return Str.index(i)
		return -1


class Solution:

	def minIndexChar(self, st, patt):
		um = {}
		minIndex = sys.maxsize
		m = len(st)
		n = len(patt)
		for i in range(m):
			if st[i] not in um:
				um[st[i]] = i
		for i in range(n):
			if patt[i] in um and um[patt[i]] < minIndex:
				minIndex = um[patt[i]]
		if minIndex != sys.maxsize:
			return minIndex
		else:
			return -1


class Solution:

	def minIndexChar(self, Str, pat):
		maxx = []
		y = 0
		for i in pat:
			if i in Str:
				maxx.append(Str.index(i))
				y = 1
		if y == 1:
			return min(maxx)
		return '-1'


class Solution:

	def minIndexChar(self, Str, pat):
		l = []
		for i in pat:
			if i in Str:
				l.append(Str.index(i))
		l.sort()
		if len(l) != 0:
			return l[0]
		else:
			return -1


class Solution:

	def minIndexChar(self, Str, pat):
		myset = set(pat)
		n = len(Str)
		for i in range(n):
			if Str[i] in myset:
				return i
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		str_dict = {}
		result = []
		for i in range(len(Str)):
			if Str[i] not in str_dict:
				str_dict[Str[i]] = i
		for i in range(len(pat)):
			if pat[i] in str_dict:
				result.append(str_dict[pat[i]])
			else:
				continue
		if len(result) != 0:
			return min(result)
		else:
			return -1


class Solution:

	def minIndexChar(self, Str, pat):
		index_list = []
		for each_c in pat:
			index = Str.find(each_c)
			if index != -1:
				index_list.append(index)
		if index_list:
			return min(index_list)
		else:
			return -1


class Solution:

	def minIndexChar(self, Str, pat):
		pat_char = set(pat)
		min_index = len(Str)
		for (i, c) in enumerate(Str):
			if c in pat_char:
				min_index = min(min_index, i)
				return min_index
		return -1


class Solution:

	def minIndexChar(self, Str, pat):
		d = {}
		ans = float('inf')
		n = len(Str)
		for i in range(n):
			if Str[i] not in d:
				d[Str[i]] = i
		for i in pat:
			if i in d:
				ans = min(ans, d[i])
		if ans == float('inf'):
			return -1
		return ans


class Solution:

	def minIndexChar(self, Str, pat):
		min = len(Str)
		for i in pat:
			if i in Str and Str.index(i) < min:
				min = Str.index(i)
		if min != len(Str):
			return min
		return -1