Given a string S consisting of lowercase Latin Letters. Return the first non-repeating character in S. If there is no non-repeating character, return '$'.
Example 1:
Input:
S = hello
Output: h
Explanation: In the given string, the
first character which is non-repeating
is h, as it appears first and there is
no other 'h' in the string.
Example 2:
Input:
S = zxvczbtxyzvy
Output: c
Explanation: In the given string, 'c' is
the character which is non-repeating. 
Your Task:
You only need to complete the function nonrepeatingCharacter() that takes string S as a parameter and returns the character. If there is no non-repeating character then return '$' .
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Number of distinct characters).
Note: N = |S|
Constraints:
1 <= N <= 10^{3}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    

    #Function to find the first non-repeating character in a string.

    def nonrepeatingCharacter(self,s):

        #code here

    

    

Solutions:
class Solution:

	def nonrepeatingCharacter(self, s):
		dict = {}
		l = []
		for i in range(len(s)):
			if s[i] not in dict:
				dict[s[i]] = 1
				l.append(s[i])
			elif s[i] in l:
				l.remove(s[i])
		if len(l) == 0:
			return '$'
		return l[0][0]


class Solution:

	def nonrepeatingCharacter(self, s):
		dict = {}
		l = []
		for i in range(len(s)):
			if s[i] not in dict:
				dict[s[i]] = 1
			else:
				dict[s[i]] += 1
		for i in s:
			if dict[i] == 1:
				return i
		return '$'


class Solution:

	def nonrepeatingCharacter(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 '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		from collections import OrderedDict
		dic = OrderedDict()
		for ele in s:
			dic[ele] = dic.get(ele, 0) + 1
		for key in dic:
			if dic[key] == 1:
				return key
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		hash_map = {}
		check_str = ''
		for i in s:
			hash_map[i] = 1 + hash_map.get(i, 0)
		for i in s:
			if hash_map[i] == 1:
				check_str += i
				return i
		if not check_str:
			return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		d = {}
		for i in range(len(s)):
			if s[i] in d:
				d[s[i]] += 1
			else:
				d[s[i]] = 1
		for i in s:
			if d[i] == 1:
				return i
				break
		return -1


class Solution:

	def nonrepeatingCharacter(self, s):
		d = {}
		for i in s:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in s:
			if d[i] == 1:
				return i
		return '$'


from collections import OrderedDict

class Solution:

	def nonrepeatingCharacter(self, s):
		d = OrderedDict()
		for c in s:
			d[c] = d.get(c, 0) + 1
		for l in d.keys():
			if d[l] == 1:
				return l
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		hash = {}
		for char in s:
			if char not in hash:
				hash[char] = 1
			else:
				hash[char] += 1
		for char in s:
			if hash[char] == 1:
				return char
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		l = [-1] * 256
		res = float('inf')
		for i in range(len(s)):
			if l[ord(s[i])] == -1:
				l[ord(s[i])] = i
			else:
				l[ord(s[i])] = -2
		for x in l:
			if x > -1:
				res = min(res, x)
		if res == float('inf'):
			return '$'
		return s[res]


class Solution:

	def nonrepeatingCharacter(self, s):
		freq = {}
		for i in s:
			freq[i] = 1 + freq.get(i, 0)
		for i in s:
			if freq[i] == 1:
				return i
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		dic = dict()
		for i in range(len(s)):
			if s[i] in dic:
				dic[s[i]] += 1
			else:
				dic[s[i]] = 1
		d = {k: v for (k, v) in dic.items() if v == 1}
		for i in range(len(s)):
			if s[i] in d:
				return s[i]
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		l = [0] * 26
		for x in s:
			l[ord(x) - 97] += 1
		for x in s:
			if l[ord(x) - 97] == 1:
				return x
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		freq = {}
		for i in range(len(s)):
			char = s[i]
			if char in freq:
				freq[char] = -1
			else:
				freq[char] = i
		min_index = len(s)
		for key in freq:
			if freq[key] != -1 and freq[key] < min_index:
				min_index = freq[key]
				result_char = key
		if min_index == len(s):
			return '$'
		else:
			return result_char


import collections

class Solution:

	def nonrepeatingCharacter(self, s):
		cnt = collections.defaultdict(lambda : 0)
		for i in s:
			cnt[i] += 1
		for i in s:
			if cnt[i] == 1:
				return i
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		if len(s) < 2:
			return s
		for i in range(len(s) - 1):
			if s[i] not in s[i + 1:] and s[i] not in s[:i]:
				return s[i]
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		occurences = [0 for i in range(256)]
		for char in s:
			occurences[ord(char)] += 1
		for i in range(len(s)):
			if occurences[ord(s[i])] == 1:
				return s[i]
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		char_dict_unq = {}
		char_dict_dup = {}
		for (i, v) in enumerate(s):
			if char_dict_unq.get(v) is None and char_dict_dup.get(v) is None:
				char_dict_unq[v] = i
				char_dict_dup[v] = i
			else:
				try:
					char_dict_unq.pop(v)
				except:
					continue
		if len(char_dict_unq) == 0:
			return '$'
		else:
			return s[min(list(char_dict_unq.values()))]


class Solution:

	def nonrepeatingCharacter(self, s):
		dictChar = {}
		repeatflag = False
		for i in s:
			if i not in dictChar.keys():
				count = 1
			else:
				count += 1
			dictChar[i] = count
		for i in s:
			if dictChar[i] == 1:
				repeatflag = False
				return i
				break
			else:
				repeatflag = True
		if repeatflag:
			return '$'


from collections import Counter

class Solution:

	def nonrepeatingCharacter(self, s):
		h = Counter(s)
		for i in s:
			if i in h:
				if h[i] == 1:
					return i
				else:
					continue
		return -1


class Solution:

	def nonrepeatingCharacter(self, s):
		hashmap = {}
		index = 0
		for i in s:
			if i in hashmap:
				hashmap[i] = -1
			else:
				hashmap[i] = index
			index = index + 1
		index = len(s)
		for map in hashmap:
			if hashmap[map] < index and hashmap[map] != -1:
				index = hashmap[map]
		return '$' if index == len(s) else s[index]


from collections import Counter, OrderedDict

class Solution:

	def nonrepeatingCharacter(self, s):
		k = OrderedDict()
		for c in s:
			if c in k:
				k[c] += 1
			else:
				k[c] = 1
		for (key, value) in k.items():
			if value == 1:
				return key
		return '$'


from collections import Counter

class Solution:

	def nonrepeatingCharacter(self, s):
		l = Counter(s)
		for i in s:
			if l[i] == 1:
				return i
		return -1


from collections import OrderedDict

class Solution:

	def nonrepeatingCharacter(self, s):
		chars_dict = OrderedDict()
		for char in s:
			if chars_dict.get(char):
				chars_dict[char] += 1
			else:
				chars_dict[char] = 1
		for (key, val) in chars_dict.items():
			if val == 1:
				return key
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		hmp = {}
		for i in s:
			if i in hmp:
				hmp[i] += 1
			else:
				hmp[i] = 1
		for i in s:
			if hmp[i] == 1:
				return i
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		for i in range(len(s)):
			if s[i] not in s[:i] and s[i] not in s[i + 1:]:
				return s[i]
				break
		else:
			return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		d = {}
		lst = []
		for i in s:
			d[i] = 0
		for i in s:
			d[i] = d[i] + 1
		for i in s:
			if d[i] == 1:
				return i
		return -1


class Solution:

	def nonrepeatingCharacter(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 '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		a = [0] * 26
		for i in s:
			a[ord(i) - ord('a')] += 1
		for i in s:
			if a[ord(i) - ord('a')] == 1:
				return i
		return '$'


import collections

class Solution:

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


class Solution:

	def nonrepeatingCharacter(self, s):
		dict1 = {}
		res = [0] * len(s)
		for i in range(len(s)):
			if s[i] not in dict1:
				dict1[s[i]] = i
				res[i] += 1
			else:
				res[dict1[s[i]]] += 1
		for i in range(len(res)):
			if res[i] == 1:
				return s[i]
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		di = {}
		for i in s:
			di[i] = di.get(i, 0) + 1
		for key in s:
			if di[key] == 1:
				return key
		return -1


class Solution:

	def nonrepeatingCharacter(self, s):
		Count = {}
		for i in s:
			if i not in Count:
				Count.update({i: s.count(i)})
			if Count[i] == 1:
				return i
		return -1


class Solution:

	def nonrepeatingCharacter(self, s):
		p = {}
		for i in s:
			if i not in p:
				p[i] = 0
			p[i] += 1
		for i in s:
			if p.get(i) == 1:
				return i
		return '$'


from collections import *

class Solution:

	def nonrepeatingCharacter(self, s):
		d = defaultdict(int)
		l = []
		for i in s:
			d[i] += 1
		for i in s:
			if d[i] < 2:
				l.append(i)
		if len(l) == 0:
			return '$'
		else:
			return l[0]


class Solution:

	def nonrepeatingCharacter(self, s):
		m = {}
		x = list(s)
		for el in x:
			if el in m:
				m[el] += 1
			else:
				m[el] = 1
		for el in x:
			if m[el] == 1:
				return el
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		s = list(s)
		a = {}
		for i in s:
			if i in a:
				a[i] += 1
			else:
				a[i] = 1
		for i in s:
			if a[i] == 1:
				return i
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		temp = {}
		for i in range(len(s)):
			if s[i] in temp:
				temp[s[i]] += 1
			else:
				temp[s[i]] = 1
		for i in range(len(s)):
			if temp[s[i]] == 1:
				return s[i]
		return -1


class Solution:

	def nonrepeatingCharacter(self, s):
		count_str = [-1 for i in range(256)]
		base = sys.maxsize
		for (i, j) in enumerate(s):
			if count_str[ord(j)] == -1:
				count_str[ord(j)] = i
			else:
				count_str[ord(j)] = -2
		for (i, j) in enumerate(count_str):
			if j > -1 and base >= j:
				base = j
		if base == sys.maxsize:
			return '$'
		else:
			return s[base]


class Solution:

	def nonrepeatingCharacter(self, s):
		x = list(set(s))
		li = []
		for i in range(len(x)):
			c = s.count(x[i])
			if c == 1:
				li.append(x[i])
		for j in s:
			if j in li:
				return j
		if len(li) == 0:
			return '$'


from collections import defaultdict

class Solution:

	def nonrepeatingCharacter(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 '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		for i in s:
			if s.find(i, s.find(i) + 1) == -1:
				return i
				break
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		d = {}
		nrc = ''
		for i in range(len(s)):
			if s[i] in d:
				d[s[i]] = [0, -1]
			else:
				d[s[i]] = [1, i]
		min_index = len(s)
		for i in d:
			if d[i][0]:
				if min_index >= d[i][1]:
					min_index = d[i][1]
					nrc = ''
					nrc = i
		if nrc:
			return nrc
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		dic = {}
		flag = False
		for i in s:
			dic[i] = 0
		for i in s:
			dic[i] += 1
		for i in s:
			if dic[i] == 1:
				flag = True
				return i
		if flag == False:
			return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		char_count = {}
		for c in s:
			char_count[c] = char_count.get(c, 0) + 1
		for c in s:
			if char_count[c] == 1:
				return c
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		a = set()
		b = set()
		for i in s:
			if i not in a:
				a.add(i)
			else:
				b.add(i)
		c = a - b
		for j in s:
			if j in c:
				return j
		if len(c) == 0:
			return -1


from collections import OrderedDict

class Solution:

	def nonrepeatingCharacter(self, s):
		dict1 = OrderedDict()
		ans = []
		for i in s:
			if i not in dict1:
				dict1[i] = 1
			elif i in dict1:
				c = dict1[i]
				dict1[i] = c + 1
		ans = [a for a in dict1 if dict1[a] == 1]
		if ans:
			return ans[0]
		else:
			return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		mp = dict()
		for ele in s:
			if ele in mp.keys():
				mp[ele] += 1
			else:
				mp[ele] = 1
		for ele in s:
			if mp[ele] == 1:
				return ele
		return '$'


import collections

class Solution:

	def nonrepeatingCharacter(self, s):
		freqs = collections.Counter(s)
		if 1 not in freqs.values():
			return -1
		else:
			for c in s:
				if freqs[c] == 1:
					return c


class Solution:

	def nonrepeatingCharacter(self, s):
		alpha = [0] * 26
		for i in s:
			alpha[ord(i) - 97] += 1
		for i in s:
			if alpha[ord(i) - 97] == 1:
				return i
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		dic = {}
		arr = []
		for i in s:
			if i not in dic:
				dic[i] = 0
				arr.append(i)
			dic[i] += 1
		res = '$'
		for i in arr:
			if dic[i] == 1:
				res = i
				break
		return res


class Solution:

	def nonrepeatingCharacter(self, s):
		dic = {}
		for i in range(len(s)):
			if s[i] not in dic:
				dic[s[i]] = 1
			else:
				dic[s[i]] += 1
		for i in range(len(s)):
			if dic[s[i]] < 2:
				return s[i]
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		hash_map = {}
		for i in s:
			hash_map[i] = hash_map.get(i, 0) + 1
		for i in range(len(s)):
			if hash_map[s[i]] == 1:
				return s[i]
		return '$'


class Solution:

	def nonrepeatingCharacter(self, s):
		non_repeat = -1
		hash = {}
		for n in s:
			if n not in hash:
				hash[n] = 1
			else:
				hash[n] += 1
		for k in s:
			if hash[k] == 1:
				non_repeat = k
				break
		return non_repeat