Skip to content

Commit d5d8524

Browse files
committed
Adding EfficientSolution for Problem - 387 - First Uniqe Char String
1 parent 0fb22e4 commit d5d8524

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 387
6+
## Problem Name: First Unique Character in a String
7+
##===================================
8+
#
9+
#Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
10+
#
11+
#Examples:
12+
#
13+
#s = "leetcode"
14+
#return 0.
15+
#
16+
#s = "loveleetcode",
17+
#return 2.
18+
#Note: You may assume the string contain only lowercase letters.
19+
from collections import Counter as c #Import Counter
20+
class Solution:
21+
def firstUniqChar(self, s):
22+
tmp = c(s) #Initialize tmp
23+
tmpChar = [key for key, val in tmp.items() if val == 1] #Initialize tmpChar which will strore the key's in which the value will be one for each character
24+
if s == "": #Condition-check: If string is empty
25+
return -1 #We'll return -1
26+
else: #Condition-check: Else
27+
if tmpChar == []: #Condition-check: if tmpChar is empty
28+
return -1 #We'll return -1
29+
else: #Condition-check: Else
30+
return s.find(tmpChar[0]) #We'll return the index of the first tmpChar in string s

0 commit comments

Comments
 (0)