Given a string S. The task is to find the first repeated character in it. We need to find the character that occurs more than once and whose index of second occurrence is smallest. S contains only lowercase letters.
 
Example 1:
Input: S="geeksforgeeks"
Output: e
Explanation: 'e' repeats at third position.
 
Example 2:
Input: S="hellogeeks"
Output: l
Explanation: 'l' repeats at fourth position.
 
Example 3:
Input: S="abc"
Output: -1
Explanation: There is no repeated character.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function firstRepChar() which accepts a string S as input parameter and returns a string containing first repeated character in it. If there is no repeated character in the string S then return "-1".
 
Expected Time Complexity: O(|S|) 
Expected Auxiliary Space: O(1)
where |S| denotes the length of string S.

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:



    def firstRepChar(self, s):

        # code here

Solutions:
class Solution:

	def firstRepChar(self, s):
		a = s
		for i in range(0, len(a)):
			list = a[:i]
			if a[i] in list:
				return a[i]
		return '-1'


class Solution:

	def firstRepChar(self, S):
		container = {}
		for i in range(len(S)):
			if S[i] in container:
				return S[i]
			else:
				container[S[i]] = 1
		return -1


from collections import Counter

class Solution:

	def firstRepChar(self, s):
		a = {}
		for i in s:
			if a.get(i) != None:
				return i
			a[i] = 1
		return -1


class Solution:

	def firstRepChar(self, s):
		char_arr = [0] * 26
		for i in s:
			i_in_ascii = ord(i) - ord('a')
			if char_arr[i_in_ascii]:
				return i
			else:
				char_arr[i_in_ascii] = 1
		return '-1'


class Solution:

	def firstRepChar(self, s):
		seen = []
		for let in s:
			if let in seen:
				return let
			seen.append(let)
		return -1


class Solution:

	def firstRepChar(self, s):
		st_l = set()
		for ch in s:
			if ch in st_l:
				return ch
			else:
				st_l.add(ch)
		return -1


class Solution:

	def firstRepChar(self, s):
		for i in range(len(s)):
			chk_lst = s[:i]
			if s[i] in chk_lst:
				return s[i]
		return -1


class Solution:

	def firstRepChar(self, s):
		diction = {}
		for ch in s:
			if ch in diction:
				diction[ch] += 1
				if diction[ch] == 2:
					return ch
			else:
				diction[ch] = 1
		return -1


class Solution:

	def firstRepChar(self, s):
		li = [s[0]]
		for i in range(1, len(s)):
			if s[i] not in li:
				li.append(s[i])
			else:
				return s[i]
		return -1


class Solution:

	def firstRepChar(self, s):
		d = {}
		for i in s:
			if i in d:
				return i
			else:
				d[i] = 1
		return -1


class Solution:

	def firstRepChar(self, s):
		save = {}
		for el in s:
			if el not in save:
				save[el] = 1
				continue
			return el
		return -1


class Solution:

	def firstRepChar(self, s):
		seen = set()
		for char in s:
			if char in seen:
				return char
			seen.add(char)
		return -1


class Solution:

	def firstRepChar(self, s):
		r = ''
		for i in s:
			if i in r:
				return i
			else:
				r = r + i
		if len(s) == len(r):
			return -1
		else:
			pass


class Solution:

	def firstRepChar(self, s):
		d = dict()
		for x in s:
			if x not in d:
				d[x] = 1
			else:
				return x
		return '-1'


class Solution:

	def firstRepChar(self, s):
		if len(set(s)) == len(s):
			return -1
		first_position_dict = {}
		for (char_tmp_index, char_tmp) in enumerate(s):
			if first_position_dict.get(char_tmp):
				return char_tmp
			else:
				first_position_dict[char_tmp] = 1
		return -1


class Solution:

	def firstRepChar(self, s):
		stack = []
		stack1 = []
		Output = ''
		for i in s:
			if i in stack:
				stack1.append(i)
			else:
				stack.append(i)
		if len(stack1) == 0:
			Output = '-1'
		else:
			Output = stack1[0]
		return Output


class Solution:

	def firstRepChar(self, s):
		k = 2
		s_set = set(s)
		lista = []
		for i in range(len(s_set)):
			nn = s_set.pop()
			cc = s.count(nn)
			lista.append((nn, cc))
		lista1 = []
		for i in range(len(lista)):
			if lista[i][1] >= k:
				lista1.append([lista[i][0], 0])
		break2 = 0
		if not lista1:
			return -1
		else:
			lista2 = []
			for i in s:
				for ii in range(len(lista1)):
					if i == lista1[ii][0]:
						lista1[ii][1] = lista1[ii][1] + 1
					if lista1[ii][1] == k:
						return lista1[ii][0]
						break2 = 1
						break
				if break2 == 1:
					break


class Solution:

	def firstRepChar(self, s):
		v = set()
		for i in range(len(s)):
			if s[i] in v:
				return s[i]
			else:
				v.add(s[i])
		return -1


class Solution:

	def firstRepChar(self, s):
		store = ''
		check = ''
		for a in s:
			if a in store:
				check = a
				break
			else:
				store += a
		if check == '':
			check = -1
		return check


class Solution:

	def firstRepChar(self, s):
		temp = set()
		for i in s:
			if i in temp:
				return i
			if i not in temp:
				temp.add(i)
		return -1


class Solution:

	def firstRepChar(self, s):
		h = {}
		for i in s:
			if i in h:
				return i
			else:
				h[i] = 0
		return -1


class Solution:

	def firstRepChar(self, s):
		chars = [0] * 256
		for ele in s:
			if chars[ord(ele)] != 0:
				return ele
			chars[ord(ele)] += 1
		return -1


class Solution:

	def firstRepChar(self, s):
		l = []
		for i in list(s):
			if i not in l:
				l.append(i)
			else:
				return i
				break
		return -1


class Solution:

	def firstRepChar(self, s):
		a = []
		for i in range(len(s)):
			if s[i] not in a:
				a.append(s[i])
			elif s[i] in a:
				return s[i]
			if len(a) == len(s):
				return -1


class Solution:

	def firstRepChar(self, s):
		map = {}
		minDistance = float('inf')
		ans = -1
		for i in range(len(s)):
			if s[i] in map:
				return s[i]
			else:
				map[s[i]] = i
		return ans


class Solution:

	def firstRepChar(self, s):
		hashmap = {}
		for i in s:
			if i not in hashmap:
				hashmap[i] = 1
			else:
				return i
		return -1


class Solution:

	def firstRepChar(self, s):
		d = ''
		for i in range(0, len(s)):
			if s[i] in d:
				return s[i]
			d += s[i]
		return -1


class Solution:

	def firstRepChar(self, s):
		chars = []
		for ch in s:
			if ch in chars:
				return ch
			chars.append(ch)
		return -1


class Solution:

	def firstRepChar(self, s):
		S = {}
		for i in s:
			if i in S:
				return i
			S[i] = 1
		return -1


class Solution:

	def firstRepChar(self, s):
		char_count = {}
		for char in s:
			if char in char_count:
				return char
			char_count[char] = 1
		return -1


class Solution:

	def firstRepChar(self, s):
		seen = set()
		for i in range(len(s)):
			if s[i] in seen:
				return s[i]
			seen.add(s[i])
		return -1


class Solution:

	def firstRepChar(self, s):
		h = {}
		for ch in s:
			if ch in h:
				return ch
			else:
				h[ch] = 1
		return -1


class Solution:

	def firstRepChar(self, s):
		ans = []
		for i in s:
			if i in ans:
				return i
			else:
				ans.append(i)
		return -1


class Solution:

	def firstRepChar(self, s):
		l = []
		c = 0
		for i in range(len(s)):
			if s[i] not in l:
				l.append(s[i])
			elif s[i] in l:
				c = 1
				return s[i]
		if c == 0:
			return -1


class Solution:

	def firstRepChar(self, s):
		d = dict()
		for i in s:
			if i in d:
				return i
			if i not in d:
				d.update({i: 1})
		return -1


class Solution:

	def firstRepChar(self, s):
		rep = set()
		for i in s:
			if i in rep:
				return i
			else:
				rep.add(i)
		return -1


from collections import Counter

class Solution:

	def firstRepChar(self, s):
		k = []
		for i in s:
			if i in k:
				return i
			else:
				k.append(i)
		return -1


class Solution:

	def firstRepChar(self, s):
		count = {}
		for i in s:
			if count.get(i):
				return i
			else:
				count[i] = '1'
		return -1


class Solution:

	def firstRepChar(self, s):
		answer = []
		temp = [i for i in s]
		for i in temp:
			if i not in answer:
				answer.append(i)
			else:
				return i
		return -1


class Solution:

	def firstRepChar(self, s):
		dic = {}
		for i in range(len(s)):
			if dic.get(s[i]) == None:
				dic[s[i]] = [1, i]
			elif dic[s[i]][0] == 1:
				dic[s[i]][0] += 1
				dic[s[i]][1] = i
		mini = len(s)
		k = ''
		c = 0
		for i in s:
			if dic[i][0] > 1 and dic[i][1] < mini:
				c = 1
				mini = dic[i][1]
				k = i
		if c == 1:
			return k
		return -1


class Solution:

	def firstRepChar(self, s):
		lst = []
		for i in s:
			if i not in lst:
				lst.append(i)
			else:
				return i
		return -1


class Solution:

	def firstRepChar(self, s):
		p = ''
		for i in range(len(s)):
			if s[i] not in p:
				p += s[i]
			else:
				char = s[i]
				return char
		return -1


class Solution:

	def firstRepChar(self, s):
		c = []
		for i in s:
			if i in c:
				return i
			c.append(i)
		return -1


from collections import Counter

class Solution:

	def firstRepChar(self, s):
		r = set()
		for i in s:
			if i in r:
				return i
			else:
				r.add(i)
		return -1


class Solution:

	def firstRepChar(self, s):
		visited = [False] * 26
		for i in s:
			if visited[ord('a') - ord(i)]:
				return i
			else:
				visited[ord('a') - ord(i)] = True
		return -1


class Solution:

	def firstRepChar(self, s):
		dict2 = {}
		dict1 = {}
		for i in range(len(s)):
			if s[i] not in dict1:
				dict1[s[i]] = 1
			else:
				dict1[s[i]] += 1
				if s[i] not in dict2:
					dict2[s[i]] = i
		if not dict2:
			return -1
		for i in dict2:
			if dict2[i] == min(dict2.values()):
				return i


class Solution:

	def firstRepChar(self, s):
		dic = {}
		for i in s:
			if i in dic:
				dic[i] += 1
				if dic[i] == 2:
					return i
			else:
				dic[i] = 1
		return -1


class Solution:

	def firstRepChar(self, s):
		explorado = ''
		for x in s:
			if explorado.count(x) == 0:
				explorado += x
			else:
				return x
		return -1