Given a string consisting of lowercase english alphabets. Find the repeated character present first in the string.
Example 1:
Input:
S = "geeksforgeeks"
Output: g
Explanation: g, e, k and s are the repeating
characters. Out of these, g occurs first. 
Example 2:
Input: 
S = "abcde"
Output: -1
Explanation: No repeating character present.
Your Task:
You don't need to read input or print anything. Your task is to complete the function firstRep() which takes the string S as input and returns the the first repeating character in the string. In case there's no repeating character present, return '#'.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).
Constraints:
1<=|S|<=10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def firstRep(self, s):

        # code here

Solutions:
class Solution:

	def firstRep(self, s):
		k = {}
		l = []
		for i in s:
			if i not in k:
				k[i] = 1
				l.append(i)
			else:
				k[i] += 1
		for i in l:
			if k[i] > 1:
				return i
		return -1


class Solution:

	def firstRep(self, s):
		for i in s:
			if s.count(i) > 1:
				return i
		return -1


class Solution:

	def firstRep(self, s):
		from collections import Counter
		c = Counter(s)
		for ele in s:
			if c[ele] >= 2:
				return ele
				break
		return '#'


from collections import OrderedDict

class Solution:

	def firstRep(self, s):
		g = OrderedDict()
		for i in range(0, len(s)):
			c = s[i]
			if c not in g:
				g[c] = 1
			else:
				g[c] += 1
		for x in g:
			if g[x] > 1:
				return x
		return -1


class Solution:

	def firstRep(self, s):
		d = {}
		for i in range(len(s)):
			if s[i] in d:
				d[s[i]] += 1
			else:
				d[s[i]] = 1
		x = ''
		for j in range(len(s)):
			if d[s[j]] > 1:
				x = str(s[j])
				break
		if x == '':
			return -1
		else:
			return x


class Solution:

	def firstRep(self, s):
		for i in range(len(s)):
			if s[i] in s[i + 1:]:
				return s[i]
		return -1


class Solution:

	def firstRep(self, s):
		j = 0
		for i in s:
			j += 1
			if i in s[j:]:
				return i
		return '-1'


class Solution:

	def firstRep(self, s):
		for i in range(len(s)):
			c = 0
			k = 0
			c = s.count(s[i])
			if c > 1:
				return s[i]
				break
				k += 1
		if k == 0:
			return -1


from collections import Counter

class Solution:

	def firstRep(self, s):
		for i in range(len(s)):
			for j in range(i + 1, len(s)):
				if s[i] == s[j]:
					return s[i]
		return -1


class Solution:

	def firstRep(self, s):
		record = ''
		for char in s:
			if s.count(char) > 1:
				return char
		return -1


class Solution:

	def firstRep(self, s):
		list1 = []
		for i in range(len(s)):
			for j in range(i + 1, len(s)):
				if s[i] == s[j]:
					return s[i]
					break
		return -1


class Solution:

	def firstRep(self, s):
		dic = {}
		for i in s:
			dic[i] = dic.get(i, 0) + 1
		for i in s:
			if dic[i] > 1:
				return i
		return -1


from collections import defaultdict

class Solution:

	def firstRep(self, s):
		hmap = defaultdict(int)
		for i in range(0, len(s)):
			if s[i] not in hmap:
				hmap[s[i]] = 1
			else:
				hmap[s[i]] += 1
		for i in range(0, len(s)):
			if hmap[s[i]] > 1:
				return s[i]
		return -1


class Solution:

	def firstRep(self, s):
		sl = []
		for i in range(len(s)):
			if len(sl) == 0:
				sl.append(s[i])
			elif sl[-1] in s[i:]:
				return sl[-1]
			else:
				sl.append(s[i])
		return -1


class Solution:

	def firstRep(self, s):
		dict1 = {}
		for i in s:
			if i not in dict1:
				dict1[i] = 1
			else:
				dict1[i] += 1
		for i in s:
			if dict1[i] > 1:
				return i
		else:
			return -1


class Solution:

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


class Solution:

	def firstRep(self, s):
		char_count = {}
		for c in s:
			if c in char_count:
				char_count[c] += 1
			else:
				char_count[c] = 1
		for c in s:
			if char_count[c] > 1:
				return c
		return -1


class Solution:

	def firstRep(self, s):
		d = {}
		for i in s:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		m_id = len(s)
		for i in d.keys():
			if d[i] > 1:
				m_id = min(m_id, s.index(i))
		if m_id != len(s):
			return s[m_id]
		return -1


from collections import Counter

class Solution:

	def firstRep(self, s):
		for char in s:
			if s.count(char) > 1:
				return char
		return -1


class Solution:

	def firstRep(self, s):
		l = []
		d = {}
		for i in s:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in s:
			if d[i] >= 2:
				return i
		return -1


class Solution:

	def firstRep(self, s):
		freq = {}
		for ch in s:
			if ch in freq:
				freq[ch] += 1
			else:
				freq[ch] = 1
		for ch in s:
			if freq[ch] > 1:
				return ch
		return -1


class Solution:

	def firstRep(self, s):
		repchar = set()
		for ch in s:
			if s.count(ch) > 1:
				return ch
		else:
			return -1


class Solution:

	def firstRep(self, s):
		s = list(s)
		for i in range(len(s)):
			char = s[i]
			if char in s[:i] or char in s[i + 1:]:
				return char
		return -1


class Solution:

	def firstRep(self, s):
		freq = {}
		for i in s:
			if i not in freq:
				freq[i] = 0
			freq[i] += 1
		for i in s:
			if freq[i] > 1:
				return i
		return -1


import collections

class Solution:

	def firstRep(self, s):
		dict1 = collections.Counter(s)
		for i in s:
			if dict1[i] > 1:
				return i
		return -1


class Solution:

	def firstRep(self, s):
		d = {}
		d_i = {}
		temp = 0
		for i in s:
			if i in d:
				d[i] += 1
			else:
				temp += 1
				d[i] = 1
				d_i[i] = temp
		m = len(s)
		ans = None
		for i in d:
			if d[i] > 1 and d_i[i] < m:
				ans = i
				m = d_i[i]
		if ans != None:
			return ans
		return -1


from collections import OrderedDict

class Solution:

	def firstRep(self, s):
		a = OrderedDict()
		for i in s:
			if i in a:
				a[i] += 1
			else:
				a[i] = 1
		for i in a:
			if a[i] > 1:
				return i
		else:
			return -1


class Solution:

	def firstRep(self, s):
		x = {}
		for char in s:
			if char not in x:
				x[char] = 1
			else:
				x[char] += 1
		for char in s:
			if x[char] > 1:
				return char
		return -1


from collections import OrderedDict

class Solution:

	def firstRep(self, s):
		d = OrderedDict()
		for i in s:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in d:
			if d[i] > 1:
				return i
				break
		else:
			return -1


from collections import Counter

class Solution:

	def firstRep(self, s):
		s = list(s)
		t = Counter(s)
		for i in s:
			if t[i] > 1:
				return i
		return -1


from collections import OrderedDict

class Solution:

	def firstRep(self, s):
		l = []
		for i in range(len(s)):
			x = s.count(s[i])
			l.append(x)
			if l[i] > 1:
				return s[i]
				break
		return -1


class Solution:

	def firstRep(self, s):
		d = {}
		l = []
		for i in s:
			if i in d.keys():
				d[i] += 1
				l.append(i)
			else:
				d[i] = 1
		for i in s:
			for j in l:
				if i == j:
					return i
		return '#'


class Solution:

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


class Solution:

	def firstRep(self, s):
		arr = [False for i in range(26)]
		res = 30
		for i in range(len(s) - 1, -1, -1):
			if arr[ord(s[i]) - 97] == False:
				arr[ord(s[i]) - 97] = True
			else:
				res = s[i]
		if res == 30:
			return -1
		return res


class Solution:

	def firstRep(self, s):
		arr = [0 for i in range(26)]
		for i in s:
			arr[ord(i) - 97] += 1
		for i in s:
			if arr[ord(i) - 97] > 1:
				return i
		return -1


class Solution:

	def firstRep(self, s):
		countchar = [False] * 256
		res = float('inf')
		for i in range(len(s) - 1, -1, -1):
			if countchar[ord(s[i])] == False:
				countchar[ord(s[i])] = True
			else:
				res = min(res, i)
		if res != float('inf'):
			return s[res]
		else:
			return -1


class Solution:

	def firstRep(self, s):
		occ = {x: 0 for x in s}
		repeat = list()
		for x in s:
			occ[x] += 1
			if occ[x] > 1:
				repeat.append(x)
		for x in s:
			if x in repeat:
				return x
		return -1


class Solution:

	def firstRep(self, s):
		l = []
		for i in s:
			l.append(i)
		for i in s:
			if l.count(i) > 1:
				return i
				break
		return -1


class Solution:

	def firstRep(self, s):
		flag = 0
		for i in range(len(s)):
			for k in range(i + 1, len(s)):
				if s[i] == s[k]:
					flag = 1
					return s[i]
		if flag == 0:
			return -1