File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments