post_href
stringlengths
57
213
python_solutions
stringlengths
71
22.3k
slug
stringlengths
3
77
post_title
stringlengths
1
100
user
stringlengths
3
29
upvotes
int64
-20
1.2k
views
int64
0
60.9k
problem_title
stringlengths
3
77
number
int64
1
2.48k
acceptance
float64
0.14
0.91
difficulty
stringclasses
3 values
__index_level_0__
int64
0
34k
https://leetcode.com/problems/lexicographical-numbers/discuss/2627271/One-Liner-or-Python
class Solution: def lexicalOrder(self, n: int) -> List[int]: return sorted([str(i) for i in range(1,n+1)])
lexicographical-numbers
One Liner | Python
RajatGanguly
0
24
lexicographical numbers
386
0.608
Medium
6,600
https://leetcode.com/problems/lexicographical-numbers/discuss/2613805/Python3-easy-to-understand-recursive
class Solution: def lexicalOrder(self, n: int) -> List[int]: def order(hi: int, limit: int) -> List[int]: for lo in range(10): n = hi * 10 + lo if n > limit: break yield n if n > 0: # generates higher combination yield from order(n, limit) return list(order(0, n))[1:]
lexicographical-numbers
Python3 easy to understand recursive
xlrtx
0
26
lexicographical numbers
386
0.608
Medium
6,601
https://leetcode.com/problems/lexicographical-numbers/discuss/2608555/Python-Simple-Python-Solution-Using-Two-Approach
class Solution: def lexicalOrder(self, n: int) -> List[int]: all_numbers = [] for num in range(1, n+1): all_numbers.append(str(num)) all_numbers = sorted(all_numbers) for index in range(len(all_numbers)): all_numbers[index] = int(all_numbers[index]) return all_numbers
lexicographical-numbers
[ Python ] βœ…βœ… Simple Python Solution Using Two Approach πŸ₯³βœŒπŸ‘
ASHOK_KUMAR_MEGHVANSHI
0
27
lexicographical numbers
386
0.608
Medium
6,602
https://leetcode.com/problems/lexicographical-numbers/discuss/2608555/Python-Simple-Python-Solution-Using-Two-Approach
class Solution: def lexicalOrder(self, n: int) -> List[int]: result = [] def DFS(current_num, num): if current_num > num: return result.append(current_num) for next_digit in range(10): DFS(current_num * 10 + next_digit, num) for current_num in range(1,10): DFS(current_num,n) return result
lexicographical-numbers
[ Python ] βœ…βœ… Simple Python Solution Using Two Approach πŸ₯³βœŒπŸ‘
ASHOK_KUMAR_MEGHVANSHI
0
27
lexicographical numbers
386
0.608
Medium
6,603
https://leetcode.com/problems/lexicographical-numbers/discuss/2012572/Iterative-Python-Easy-to-understand
class Solution: def lexicalOrder(self, n: int) -> List[int]: res = [1] cn = 1 while(len(res) < n): cn *= 10 while(cn > n): cn = cn // 10 cn += 1 while (cn % 10 == 0): cn //= 10 res.append(cn) return res
lexicographical-numbers
Iterative Python, Easy to understand
alaki123
0
64
lexicographical numbers
386
0.608
Medium
6,604
https://leetcode.com/problems/lexicographical-numbers/discuss/1876540/Pyhton3-One-Liner
class Solution: def lexicalOrder(self, n: int) -> List[int]: return [int(i) for i in sorted([str(_) for _ in range(1,n+1)])]
lexicographical-numbers
Pyhton3 One Liner
eaux2002
0
29
lexicographical numbers
386
0.608
Medium
6,605
https://leetcode.com/problems/lexicographical-numbers/discuss/1627537/Python3-Recursive-dfs-solution
class Solution: def dfs(self, cur, n, res): if cur > n: return res.append(cur) for i in range(0, 10): self.dfs(cur * 10 + i, n, res) def lexicalOrder(self, n: int) -> List[int]: res = [] for i in range(1, 10): self.dfs(i, n, res) return res
lexicographical-numbers
[Python3] Recursive dfs solution
maosipov11
0
93
lexicographical numbers
386
0.608
Medium
6,606
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1793386/Python-Simple-Python-Solution-With-Two-Approach
class Solution: def firstUniqChar(self, s: str) -> int: for i in range(len(s)): if s[i] not in s[:i] and s[i] not in s[i+1:]: return i return -1
first-unique-character-in-a-string
[ Python ] βœ…βœ… Simple Python Solution With Two Approach πŸ₯³βœŒπŸ‘
ASHOK_KUMAR_MEGHVANSHI
28
1,600
first unique character in a string
387
0.59
Easy
6,607
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1793386/Python-Simple-Python-Solution-With-Two-Approach
class Solution: def firstUniqChar(self, s: str) -> int: frequency = {} for char in s: if char not in frequency: frequency[char] = 1 else: frequency[char] = frequency[char] + 1 for index in range(len(s)): if frequency[s[index]] == 1: return index return -1
first-unique-character-in-a-string
[ Python ] βœ…βœ… Simple Python Solution With Two Approach πŸ₯³βœŒπŸ‘
ASHOK_KUMAR_MEGHVANSHI
28
1,600
first unique character in a string
387
0.59
Easy
6,608
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2500495/Very-Easy-oror-100-oror-Fully-Explained-oror-Java-C%2B%2B-Python-JavaScript-Python3
class Solution(object): def firstUniqChar(self, s): hset = collections.Counter(s); # Traverse the string from the beginning... for idx in range(len(s)): # If the count is equal to 1, it is the first distinct character in the list. if hset[s[idx]] == 1: return idx return -1 # If no character appeared exactly once...
first-unique-character-in-a-string
Very Easy || 100% || Fully Explained || Java, C++, Python, JavaScript, Python3
PratikSen07
21
1,700
first unique character in a string
387
0.59
Easy
6,609
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2431434/Python-using-Counter
class Solution: def firstUniqChar(self, s: str) -> int: counter = Counter(s) for i, c in enumerate(s): if counter[c] == 1: return i return -1
first-unique-character-in-a-string
Python, using Counter
blue_sky5
4
366
first unique character in a string
387
0.59
Easy
6,610
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1070121/Easy-JavaPython-Solution-With-Comments
class Solution: def firstUniqChar(self, s: str) -> int: ascii = [0] * 26 for i in s: ascii[ord(i) - ord('a')] += 1 for k, v in enumerate(s): if ascii[ord(v) - ord('a')] == 1: return k return -1
first-unique-character-in-a-string
Easy Java/Python Solution [With Comments]
gogagubi
3
344
first unique character in a string
387
0.59
Easy
6,611
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2799992/Simple-Python-Solution-with-hashmap
class Solution: def firstUniqChar(self, s: str) -> int: hashmap = {} for c in s: hashmap[c] = hashmap.get(c, 0) + 1 for i, c in enumerate(s): if hashmap[c] == 1: return i return -1 Time: O(n) Space: O(n)
first-unique-character-in-a-string
Simple Python Solution with hashmap
tragob
2
280
first unique character in a string
387
0.59
Easy
6,612
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2436123/Python-simple-and-easy-solution-oror-O(N)-Time-Complexity
class Solution: def firstUniqChar(self, s: str) -> int: counterMap = collections.Counter(s) for i in range(len(s)): if counterMap[s[i]] == 1: return i return -1
first-unique-character-in-a-string
Python - simple and easy solution || O(N) Time Complexity
dayaniravi123
2
43
first unique character in a string
387
0.59
Easy
6,613
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2432550/Python-solution-2-different-approaches.-With-explanation-in-comments-between-the-code.
class Solution: def firstUniqChar(self, s: str) -> int: # Creating an empty dictionary dic = {} # Looping in the string till the end for char in s: # Mentioning each letter occurence dic[char] = 1+dic.get(char,0) # Again looping till the length of string for i in range(len(s)): # Checking in the dictionary we've created above if the occurence of the letter is 1 if(dic.get(s[i],0)==1): # We've to return that index return i # If the whole string ends and I'm not able to find the letter that is unique having occurence as 1 then I'll return -1. return -1
first-unique-character-in-a-string
Python solution 2 different approaches. With explanation in comments between the code.
yashkumarjha
2
112
first unique character in a string
387
0.59
Easy
6,614
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2432550/Python-solution-2-different-approaches.-With-explanation-in-comments-between-the-code.
class Solution: def firstUniqChar(self, s: str) -> int: # Using the inbuilt keyword counter to find the occurence of each letter in the string given and storing it in a variable c. c = Counter(s) # Now looping till the end of the string in such a way that it will take both keys and values. Here i and j enumerate means taking keys and values pair. for i,j in enumerate(s): # Now checking if j i.e. the values is equal to 1 means the occurence of that letter is unique. if(c[j]==1): # Then we'll return that index. return i # If after looping till end not able to find the unique then return -1. return -1
first-unique-character-in-a-string
Python solution 2 different approaches. With explanation in comments between the code.
yashkumarjha
2
112
first unique character in a string
387
0.59
Easy
6,615
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2043586/Python-2-Ways-One-liner-Beats-100-(54ms)-or-Beginner-Notes
class Solution: def firstUniqChar(self, s: str) -> int: dict = Counter(s) for i, char in enumerate(s): if dict[char] == 1: return i return -1
first-unique-character-in-a-string
[Python] 2 Ways One-liner Beats 100% (54ms) | Beginner Notes
ziaiz-zythoniz
2
146
first unique character in a string
387
0.59
Easy
6,616
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2043586/Python-2-Ways-One-liner-Beats-100-(54ms)-or-Beginner-Notes
class Solution: def firstUniqChar(self, s: str) -> int: return min([s.find(char) for char in string.ascii_lowercase if s.count(char)==1], default = -1)
first-unique-character-in-a-string
[Python] 2 Ways One-liner Beats 100% (54ms) | Beginner Notes
ziaiz-zythoniz
2
146
first unique character in a string
387
0.59
Easy
6,617
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1931971/python3-easy-hashmap-solution
class Solution: def firstUniqChar(self, s: str) -> int: counter = {} for i, ch in enumerate(s): if ch in counter: counter[ch] = -1 else: counter[ch] = i for ch in s: if counter[ch] !=-1: return counter[ch] return -1
first-unique-character-in-a-string
python3 easy hashmap solution
emerald19
2
131
first unique character in a string
387
0.59
Easy
6,618
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1587664/Simple-Python-3-With-Video-Explanation
class Solution: def firstUniqChar(self, s: str) -> int: x = Counter(s).items() for j,k in x: if k == 1: return s.index(j) return -1
first-unique-character-in-a-string
Simple Python 3 - With Video Explanation
hudsonh
2
262
first unique character in a string
387
0.59
Easy
6,619
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1058958/Easy-dictionary-solution-python-or-O(N)
class Solution: def firstUniqChar(self, s: str) -> int: d = {} for i in s: if i in d: d[i] += 1 else: d[i] = 1 for i in range(len(s)): if d[s[i]] == 1: return i return -1
first-unique-character-in-a-string
Easy dictionary solution python | O(N)
vanigupta20024
2
676
first unique character in a string
387
0.59
Easy
6,620
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/615268/Easy-understand-or-O(n)-or-Simple-Solution
class Solution: def firstUniqChar(self, s: str) -> int: hashmap = dict() # enumerate string, store the number of each letter for c in s: if c not in hashmap: hashmap[c] = 1 else: hashmap[c] +=1 # since we store it in order, so enumerate it, return the index when the value == 1 for k,v in hashmap.items(): if v==1: return s.index(k) # didn't find it, return -1 return -1
first-unique-character-in-a-string
πŸ”₯Easy-understand | O(n) | Simple SolutionπŸ”₯
Get-Schwifty
2
465
first unique character in a string
387
0.59
Easy
6,621
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/614918/Python3-a-few-approaches
class Solution: def firstUniqChar(self, s: str) -> int: return next((i for i, c in enumerate(s) if s.count(c) == 1), -1)
first-unique-character-in-a-string
[Python3] a few approaches
ye15
2
66
first unique character in a string
387
0.59
Easy
6,622
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/614918/Python3-a-few-approaches
class Solution: def firstUniqChar(self, s: str) -> int: freq = Counter(s) return next((i for i, ch in enumerate(s) if freq[ch] == 1), -1)
first-unique-character-in-a-string
[Python3] a few approaches
ye15
2
66
first unique character in a string
387
0.59
Easy
6,623
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/614918/Python3-a-few-approaches
class Solution: def firstUniqChar(self, s: str) -> int: freq = dict() for c in s: freq[c] = 1 + freq.get(c, 0) return next((i for i, c in enumerate(s) if freq[c] == 1), -1)
first-unique-character-in-a-string
[Python3] a few approaches
ye15
2
66
first unique character in a string
387
0.59
Easy
6,624
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/614918/Python3-a-few-approaches
class Solution: def firstUniqChar(self, s: str) -> int: uniq, seen = dict(), set() for i, c in enumerate(s): if c in uniq: uniq.pop(c) if c not in seen: uniq[c] = i seen.add(c) return next((v for k, v in uniq.items()), -1)
first-unique-character-in-a-string
[Python3] a few approaches
ye15
2
66
first unique character in a string
387
0.59
Easy
6,625
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2433528/Python-or-TM%3A-99.9196.20-or-Easy-Solution-Explained
class Solution: def firstUniqChar(self, s: str) -> int: st = Counter(s) for k, v in st.most_common(): if v == 1: return s.index(k) return -1
first-unique-character-in-a-string
Python | T/M: 99.91/96.20 | Easy Solution Explained
vrutikparvadiya01
1
36
first unique character in a string
387
0.59
Easy
6,626
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2158732/Python-Simple-and-Easy-Solution
class Solution: def firstUniqChar(self, s: str) -> int: for i in dict.fromkeys(s): if s.count(i)==1: return s.index(i) else: return -1
first-unique-character-in-a-string
Python Simple and Easy Solution
pruthashouche
1
110
first unique character in a string
387
0.59
Easy
6,627
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2137594/Python-Solution
class Solution: def firstUniqChar(self, s: str) -> int: d={i: s.count(i) for i in set(s) if s.count(i)==1} #creating dictionary of all non repeative words in string for i in s: if i in d: return s.index(i) return -1
first-unique-character-in-a-string
Python Solution
Samm_22
1
64
first unique character in a string
387
0.59
Easy
6,628
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2100453/Python-2-line-solution-(beats-99)
class Solution: def firstUniqChar(self, s: str) -> int: unique_letters = list(Counter({k: c for k, c in Counter(s).items() if c == 1}).keys()) return s.find(unique_letters[0]) if len(unique_letters) else -1
first-unique-character-in-a-string
[Python] 2 line solution (beats 99%)
FedMartinez
1
107
first unique character in a string
387
0.59
Easy
6,629
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1980357/Python3-Easy-and-Efficient-solution
class Solution: def firstUniqChar(self, s: str) -> int: letters='abcdefghijklmnopqrstuvwxyz' index = [] for l in letters: if s.count(l) == 1: index.append(s.index(l)) if len(index)>0: return min(index) else: return -1
first-unique-character-in-a-string
Python3-Easy and Efficient solution
Akhil_akhi
1
52
first unique character in a string
387
0.59
Easy
6,630
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1978207/Python-Approach-for-Infinite-Character-Stream
class Solution: def firstUniqChar(self, string: str) -> int: import sys hashMap = dict() for i in range(len(string)): if string[i] not in hashMap: hashMap[string[i]] = i else: hashMap[string[i]] = -1 minIndex = sys.maxsize for code in range(ord('a'),ord('z') + 1): char = chr(code) if char in hashMap and hashMap[char] != -1: minIndex = min(minIndex,hashMap[char]) return -1 if minIndex == sys.maxsize else minIndex
first-unique-character-in-a-string
Python Approach for Infinite Character Stream
chacha_chowdhary
1
44
first unique character in a string
387
0.59
Easy
6,631
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1817034/Python-Easiest-Solution-With-Explanation-oror-HashMap-oror-Beg-to-Adv
class Solution: def firstUniqChar(self, s: str) -> int: hashmap = {} # First we`ll add all the elements in the hashmap with its count for i in s: if i not in hashmap: hashmap[i] = 1 #if we dont have a element in hashmap we are inserting it there. else: hashmap[i] += 1 #if we have element in hashmap increase its count. for index, n in enumerate(s): if hashmap[n] == 1: # comparing the value with one as we need the first element of the string that occurres once. return index # returning index of the first element of the string that occurres once return -1 # returning -1 if we dont have any element which is there in the string only once.
first-unique-character-in-a-string
Python Easiest Solution With Explanation || HashMap || Beg to Adv
rlakshay14
1
156
first unique character in a string
387
0.59
Easy
6,632
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1696125/Simple-Python-using-set-oror-O(n)-time-complexity-oror-100ms
class Solution: def firstUniqChar(self, s: str) -> int: temp=set() for i in range(len(s)): if ((s[i] not in temp) and (s[i] in s[i+1::])): #If the repeating letter has the first occurence temp.add(s[i]) elif(s[i] in temp): #If repeating letter is occured at least once continue else: return i #First non-repeating letter return -1
first-unique-character-in-a-string
Simple Python using set || O(n) time complexity || 100ms
HimanshuGupta_p1
1
82
first unique character in a string
387
0.59
Easy
6,633
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1534203/Python-96%2B-Faster-with-Counter
class Solution: def firstUniqChar(self, s: str) -> int: c = collections.Counter(s) for k , v in c.items(): if v == 1: return s.index(k) else: return -1
first-unique-character-in-a-string
Python 96%+ Faster with Counter
aaffriya
1
274
first unique character in a string
387
0.59
Easy
6,634
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1514937/python-3-oror-dictionary-oror-easy
class Solution: def firstUniqChar(self, s: str) -> int: d={} for ch in s: if ch in d: d[ch]+=1 else: d[ch]=1 for key,value in d.items(): if value==1: return s.index(key) return -1
first-unique-character-in-a-string
python 3 || dictionary || easy
minato_namikaze
1
245
first unique character in a string
387
0.59
Easy
6,635
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1307050/Simple-Python-solution-without-Counter.-Faster-than-95
class Solution: def firstUniqChar(self, s: str) -> int: st = set() ans = None for index, ch in enumerate(s): if ch not in st and ch not in s[index+1:]: ans = index break st.add(ch) return -1 if ans is None else ans
first-unique-character-in-a-string
Simple Python solution without Counter. Faster than 95%
shuneihayakawa
1
186
first unique character in a string
387
0.59
Easy
6,636
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1296730/Python3-runtime-beats-92.25
class Solution: def firstUniqChar(self, s: str) -> int: arr = [] index = [] hashset = set() for i in range(len(s)): if s[i] in hashset: continue elif s[i] not in arr: arr.append(s[i]) index.append(i) elif s[i] in arr: indexOfCha = arr.index(s[i]) arr.remove(s[i]) index.pop(indexOfCha) hashset.add(s[i]) if len(arr) == 0: return -1 return index[0]
first-unique-character-in-a-string
Python3 - runtime beats 92.25 %
CC_CheeseCake
1
124
first unique character in a string
387
0.59
Easy
6,637
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1260475/Python3-dollarolution
class Solution: from collections import Counter def firstUniqChar(self, s: str) -> int: s = list(s) a = Counter(s) for i in a: if a[i] == 1: return s.index(i) else: pass return -1
first-unique-character-in-a-string
Python3 $olution
AakRay
1
243
first unique character in a string
387
0.59
Easy
6,638
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1206519/Easy-Python-Solution%3A-With-Comments
class Solution: def firstUniqChar(self, s: str) -> int: dicts={} count=[] #create a dictionary with letters in the string and it's count for i in s: if i in dicts: dicts[i] +=1 else: dicts[i] =1 #print(dicts) #get the index of letter which only counted once i.e. first unique character for i in range(len(s)): if dicts[s[i]] == 1: return(i) #if there are no unique character then return -1 return ('-1')
first-unique-character-in-a-string
Easy Python Solution: With Comments
YashashriShiral
1
174
first unique character in a string
387
0.59
Easy
6,639
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/1044361/Python-Three-Liners
class Solution: def firstUniqChar(self, s: str) -> int: c = collections.Counter(s) unique = [x for x in c if c[x] == 1] return min([s.index(x) for x in unique] or [-1])
first-unique-character-in-a-string
Python Three Liners
A88888
1
134
first unique character in a string
387
0.59
Easy
6,640
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/614492/Python-3-Accepted-intuitive-sol.-91.57(84ms)
class Solution: def firstUniqChar(self, s: str) -> int: count = {} for ch in set(s): count[ch] = s.count(ch) for idx in range(len(s)): if count[s[idx]] == 1: return idx return -1
first-unique-character-in-a-string
[Python 3] Accepted intuitive sol. 91.57%(84ms)
Ashley_guagua
1
227
first unique character in a string
387
0.59
Easy
6,641
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2848539/python3
class Solution: def firstUniqChar(self, s: str) -> int: d = {} # store letters we've checked for i in range(len(s)): # if we haven't checked this letter yet if (c := s[i]) not in d: # check it and add to d if s.count(c) == 1: return i d[c] = True # got through without finding a unique char return -1
first-unique-character-in-a-string
python3
wduf
0
1
first unique character in a string
387
0.59
Easy
6,642
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2847435/python-solution-O(n)-O(n)
class Solution: def firstUniqChar(self, s: str) -> int: hash = collections.defaultdict(list) for i , v in enumerate(s): hash[v].append(i) for i in range(len(s)): if len(hash[s[i]])==1: return hash[s[i]][0] return -1
first-unique-character-in-a-string
python solution O(n) , O(n)
sintin1310
0
1
first unique character in a string
387
0.59
Easy
6,643
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2845593/Simple-solution-in-Python-3
class Solution: def firstUniqChar(self, s: str) -> int: counts = Counter(s) for i in counts: if counts[i] == 1: for j in range(len(s)): if i == s[j]: return j return -1
first-unique-character-in-a-string
Simple solution in Python 3
sukumaran1004
0
3
first unique character in a string
387
0.59
Easy
6,644
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2840494/Python-simplest-solution-(time-and-space-95)
class Solution: def firstUniqChar(self, s: str) -> int: dict = {} for i,char in enumerate(s): if char in dict: dict[char][1] = False else: dict[char] = [i,True] for char in dict: if dict[char][1] == True: return dict[char][0] return -1
first-unique-character-in-a-string
Python simplest solution (time and space 95%)
AlexTaylorCoder
0
3
first unique character in a string
387
0.59
Easy
6,645
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2823737/Python-dict-and-list-solution-oror-Faster-than-90.51
class Solution: def firstUniqChar(self, s: str) -> int: exdict = {} #create empty dictionary for i in s: #append all items from s into dict if i not in exdict: exdict[i] = 1 else: exdict[i] += 1 #if i is already in dict, then increase count by 1 temp = [] # create empty list for key, val in exdict.items(): if val == 1: temp.append(key) #only append letters that appear once to the list if len(temp) == 0: return -1 # if there is no single count letters in the word, return -1 for count, i in enumerate(s): #enumerate and iterate over s if i in temp: return count #returns the first value in temp, and thus first in s
first-unique-character-in-a-string
Python dict and list solution || Faster than 90.51%
inusyd
0
4
first unique character in a string
387
0.59
Easy
6,646
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2811293/Python-O(n)-oror-Simple-and-Easy-using-Counter
class Solution: def firstUniqChar(self, s: str) -> int: cnt = Counter(s) for i, ch in enumerate(s): if cnt[ch] == 1: return i return -1
first-unique-character-in-a-string
[Python] O(n) || Simple and Easy using Counter
shahbaz95ansari
0
7
first unique character in a string
387
0.59
Easy
6,647
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2798951/lesspythongreater-EASIEST-with-Example-and-comments
class Solution: def firstUniqChar(self, s: str) -> int: c=Counter(s) for i in c: if c[i]==1: return s.index(i) return -1 #eg: leetcode #here,counter will return occurance of characters # l e t c o d # [1 3 1 1 1 1] #hence, l is the first non repeating character #so, we'll return its index i.e., 0
first-unique-character-in-a-string
<python> EASIEST with Example and comments
user9516zM
0
3
first unique character in a string
387
0.59
Easy
6,648
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2797362/Python3-or-387.-First-Unique-Character-in-a-String
class Solution: def firstUniqChar(self, s: str) -> int: d = {} for i in s: if not i in d: d[i] = 1 else: d[i] += 1 for i in range(len(s)): if d[s[i]] == 1: return i return -1
first-unique-character-in-a-string
Python3 | 387. First Unique Character in a String
AndrewMitchell25
0
2
first unique character in a string
387
0.59
Easy
6,649
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2796438/First-unique-char-with-dictionary
class Solution: def firstUniqChar(self, s: str) -> int: counts = dict() for c in s: if c not in counts: counts[c] = 1 else: counts[c] += 1 for i in range(len(s)): if counts[s[i]] == 1: return i return -1
first-unique-character-in-a-string
First unique char with dictionary
Morphy232
0
2
first unique character in a string
387
0.59
Easy
6,650
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2791152/python-solution
class Solution: def firstUniqChar(self, s: str) -> int: from collections import Counter c = Counter(s) for k,v in c.items(): if v == 1: return s.index(k) return -1
first-unique-character-in-a-string
python solution
radhikapadia31
0
2
first unique character in a string
387
0.59
Easy
6,651
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2781295/Python-O(n)-oror-Easy-4-line-Solution
class Solution: def firstUniqChar(self, s: str) -> int: for i, c in enumerate(s): if c not in s[:i] + s[i+1:]: return i return -1
first-unique-character-in-a-string
[Python] O(n) || Easy 4 line Solution
shahbaz95ansari
0
4
first unique character in a string
387
0.59
Easy
6,652
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2768018/Python-Solution-or-Runtime-beats-99-or-Dictionary-Counter
class Solution: def firstUniqChar(self, s: str) -> int: an = [] ans = [] c = Counter(s) for key,value in c.items(): if(value == 1): an.append(key) if(an == []): return -1 else: for i in an: ans.append(s.index(i)) ans.sort() return ans[0]
first-unique-character-in-a-string
Python Solution | Runtime beats 99% | Dictionary Counter
anshu71
0
15
first unique character in a string
387
0.59
Easy
6,653
https://leetcode.com/problems/first-unique-character-in-a-string/discuss/2766997/Python-Dictionary-Solution-Beats-89-%2B-SUPER-Slow-Intuitive-Solution
class Solution: def firstUniqChar(self, s: str) -> int: counts = dict() for letter in s: counts[letter] = counts.get(letter, 0) + 1 for i in range(len(s)): if counts[s[i]] == 1: return i return -1 ''' # SUPER slow solution for i in range(len(s)): if s.count(s[i]) == 1: return i return -1 '''
first-unique-character-in-a-string
Python Dictionary Solution Beats 89% + SUPER Slow Intuitive Solution
gequalspisquared
0
3
first unique character in a string
387
0.59
Easy
6,654
https://leetcode.com/problems/longest-absolute-file-path/discuss/812407/Python-3-or-Stack-or-Explanation
class Solution: def lengthLongestPath(self, s: str) -> int: paths, stack, ans = s.split('\n'), [], 0 for path in paths: p = path.split('\t') depth, name = len(p) - 1, p[-1] l = len(name) while stack and stack[-1][1] >= depth: stack.pop() if not stack: stack.append((l, depth)) else: stack.append((l+stack[-1][0], depth)) if '.' in name: ans = max(ans, stack[-1][0] + stack[-1][1]) return ans
longest-absolute-file-path
Python 3 | Stack | Explanation
idontknoooo
23
1,900
longest absolute file path
388
0.465
Medium
6,655
https://leetcode.com/problems/longest-absolute-file-path/discuss/489939/Python3-easy-solution-using-a-dict()
class Solution: def lengthLongestPath(self, input: str) -> int: max_len,ht = 0,{} for p in input.split("\n"): key=p.count("\t") if "." not in p: value = len(p.replace("\t","")) ht[key]=value else: temp_len = key + len(p.replace("\t","")) for ky in ht.keys(): if ky < key: temp_len += ht[ky] max_len=max(max_len,temp_len) return max_len
longest-absolute-file-path
Python3 easy solution using a dict()
jb07
6
698
longest absolute file path
388
0.465
Medium
6,656
https://leetcode.com/problems/longest-absolute-file-path/discuss/601155/Python-O(n)-solution-with-stack
class Solution: def lengthLongestPath(self, inputStr: str) -> int: """ Clarifications: 1. \n means we are still at the same folder as the previous one. 2. \t means we are entering a sub folder of the previous one. Example: 1. dir\n(4-space) file.txt The longest is " file.txt", where the file is stored on the same level of dir. 2. dir\n\t(4-space) file.txt The longest is "dir/ file.text", where the file is stored under dir. """ stack = [(-1, 0)] # (current level, length of the current path) foundFile = False nextLevel = currLevel = currLen = maxLen = 0 i, n = 0, len(inputStr) while i < n: c = inputStr[i] if c == '\n': # Found a file in the previous item, calculate its path length. if foundFile: maxLen = max(maxLen, currLen) foundFile = False # Check the level for the next item. nextLevel = 0 while inputStr[i + 1] == '\t': nextLevel += 1 i += 1 if currLevel < nextLevel: # Go down. currLen += 1 # '/' takes one pisition in the path. stack.append((currLevel, currLen)) else: # Stay on the same or go up. while stack[-1][0] >= nextLevel: stack.pop() currLen = stack[-1][-1] currLevel = nextLevel else: if c == '.': foundFile = True currLen += 1 i += 1 # Process the next char. if foundFile: # Process the last file if any. maxLen = max(maxLen, currLen) return maxLen
longest-absolute-file-path
Python O(n) solution with stack
eroneko
4
558
longest absolute file path
388
0.465
Medium
6,657
https://leetcode.com/problems/longest-absolute-file-path/discuss/1703911/Python-or-Stack-or-9-Lines-or-Simple
class Solution: def lengthLongestPath(self, input: str) -> int: input = input.split('\n') path, ans, total = [], 0, 0 for line in input: tabs = line.count('\t') while len(path) > tabs: total -= path.pop() path.append(len(line) - tabs) total += path[-1] if '.' in line: ans = max(ans, total + len(path) - 1) return ans
longest-absolute-file-path
Python | Stack | 9 Lines | Simple
leeteatsleep
2
373
longest absolute file path
388
0.465
Medium
6,658
https://leetcode.com/problems/longest-absolute-file-path/discuss/1105723/Python-Stack-Solution
class Solution: def lengthLongestPath(self, input: str) -> int: """ We split string by \n and than check the level of each subdirectory/file. If level bigger than previus one we add to the stack tuple with it( cur_level, directory(file_name) ), else pop from it. Before pop we check if the current stack values contains filepath and compare with longest value. We put tuple (level_size, path_name) in the stack on each iteration. """ s = [] longest = 0 for dir in input.split("\n"): cur_level = dir.count('\t') while s and s[-1][0] >= cur_level: #Before pop element check if this a file path if '.' in s[-1][1]: longest = max(longest, len("/".join([dir_file_name for _, dir_file_name in s]))) s.pop() s.append((cur_level, dir.replace('\t',''))) #(level_size, path_name) if s and '.' in s[-1][1]: longest = max(longest, len("/".join([dir_file_name for _, dir_file_name in s]))) return longest
longest-absolute-file-path
Python Stack Solution
Hrabryi
1
517
longest absolute file path
388
0.465
Medium
6,659
https://leetcode.com/problems/longest-absolute-file-path/discuss/823830/Python3-via-prefix-sum
class Solution: def lengthLongestPath(self, input: str) -> int: ans = 0 prefix = [0] for x in input.split("\n"): # split string into sub-dirs k = x.count("\t") x = x.lstrip("\t") if "." in x: ans = max(ans, prefix[k] + len(x)) # x is file else: if len(prefix) == k+1: prefix.append(prefix[-1] + 1 + len(x)) else: prefix[k+1] = prefix[k] + 1 + len(x) return ans
longest-absolute-file-path
[Python3] via prefix sum
ye15
1
335
longest absolute file path
388
0.465
Medium
6,660
https://leetcode.com/problems/longest-absolute-file-path/discuss/823830/Python3-via-prefix-sum
class Solution: def lengthLongestPath(self, input: str) -> int: ans = 0 prefix = {-1: 0} for subd in input.split("\n"): # sub-directory depth = subd.count("\t") prefix[depth] = prefix[depth-1] + len(subd) - depth # not including delimiter if "." in subd: ans = max(ans, prefix[depth] + depth) # including delimiter return ans
longest-absolute-file-path
[Python3] via prefix sum
ye15
1
335
longest absolute file path
388
0.465
Medium
6,661
https://leetcode.com/problems/longest-absolute-file-path/discuss/311380/Python-3-Solution-using-a-stack-beats-93
class Solution: def lengthLongestPath(self, paths: str) -> int: paths = paths.split('\n') max_path = 0 stack = [(0, -1)] # init the stack to allow 'peek'. for element in paths: level = element.count('\t') element = element[level:] # pop until I found my parent while level <= stack[-1][1]: stack.pop() # if '.' is a possible solution if '.' in element: total = stack[-1][0] max_path = max(max_path, total + len(element)) continue # add my current len to the total total = stack[-1][0] + len(element) + 1 stack.append((total, level)) return max_path
longest-absolute-file-path
Python 3 Solution using a stack, beats 93%
agmezr
1
346
longest absolute file path
388
0.465
Medium
6,662
https://leetcode.com/problems/longest-absolute-file-path/discuss/2848637/Python-solution-with-printed-absolute-path
class Solution: def lengthLongestPath(self, input: str) -> int: inp = input.split('\n') curpath = '' path = '' ind = 0 for i in inp: if '.' in i: curpath = '' tab = i.count('\t') - 1 # num of tab for parent dir for j in reversed(inp[:ind]): # loop to find parent dir bottom up if j.count('\t') == tab: curpath = '/'+ j + curpath tab -= 1 # format str curpath += '/' + i curpath = ''.join(curpath.split('\t')) curpath = curpath.strip('/') if len(curpath) > len(path): path = curpath ind += 1 print(path) return len(path)
longest-absolute-file-path
Python solution with printed absolute path
yoyo10
0
2
longest absolute file path
388
0.465
Medium
6,663
https://leetcode.com/problems/longest-absolute-file-path/discuss/2801067/Simple-intuitive-stack-based-approach
class Solution: def lengthLongestPath(self, input: str) -> int: if input.find(".") == -1: return 0 d = input.split('\n') st = [] t = 0 m = 0 while d: if d[0].startswith('\t' * t): st.append(d[0]) d.pop(0) t += 1 a = "/".join(st).replace("\t", "") if len(a) > m and "." in a: print("nxt\n",a) m = len(a) else: st.pop() t -= 1 return(m)
longest-absolute-file-path
Simple intuitive stack based approach
jaisalShah
0
5
longest absolute file path
388
0.465
Medium
6,664
https://leetcode.com/problems/longest-absolute-file-path/discuss/2666741/Simple-Solution-with-Python3
class Solution: def lengthLongestPath(self, input: str) -> int: lines=input.split("\n") maxlength=0 depth_map={0:0} for line in lines: path=line.split("\t")[-1] depth=len(line)-len(path) if '.' in path: maxlength=max(maxlength,depth_map[depth]+len(path)) else: depth_map[depth+1]=depth_map[depth]+len(path)+1 return maxlength
longest-absolute-file-path
Simple Solution with Python3
Motaharozzaman1996
0
15
longest absolute file path
388
0.465
Medium
6,665
https://leetcode.com/problems/longest-absolute-file-path/discuss/2624913/Python-simple-stack-solution
class Solution: def lengthLongestPath(self, input: str) -> int: lines = input.split("\n") longest = 0 stack = [] for line in lines: d = 0 while line[d] == "\t": d += 1 size = len(line[d:]) while d < len(stack): stack = stack[:-1] stack.append(size) if "." in line: longest = max(sum(stack) + len(stack)-1, longest) return longest
longest-absolute-file-path
Python simple stack solution
pxmachine
0
38
longest absolute file path
388
0.465
Medium
6,666
https://leetcode.com/problems/longest-absolute-file-path/discuss/2477163/Python3%3A-Hashmap-with-explaination
class Solution: def lengthLongestPath(self, input: str) -> int: # Stores the max length observed so far in the iteration max_len = 0 # map to store level and length so far. Initialized with 0 level with 0 length level_len_map = {0:0} # After splitting by '\n' all tokens are either folder or file for tkn in input.split('\n'): # number of '\t' denotes level level = tkn.count('\t') # Note: '\t' is of length 1, so subtracting count of '\t' from tkn length will give lenght of file or folder name name_len = len(tkn) - level # Its a file if '.' in tkn: # update max length max_len = max(max_len, level_len_map[level] + name_len) else: # Update depth map level_len_map[level+1] = level_len_map[level] + name_len + 1 return max_len
longest-absolute-file-path
Python3: Hashmap with explaination
nainybits
0
50
longest absolute file path
388
0.465
Medium
6,667
https://leetcode.com/problems/longest-absolute-file-path/discuss/2273150/No-traversal-algo-Used-38ms
class Solution: def lengthLongestPath(self, input: str) -> int: if "." not in input: return 0 a=input.split("\n") files=[] for i in a: if "." in i: files.append(i) final=[] for i in range(len(files)): file=files[i] lvl=file.count("\t") idx=a.index(file)-1 save=[files[i].replace("\t","")] for j in range(lvl): while a[idx].count("\t")!=lvl-1: idx-=1 lvl=a[idx].count("\t") save.append(a[idx].replace("\t","")) idx-=1 final.append(save) final=list(map("/".join,final)) return len(max(final,key=len))
longest-absolute-file-path
No traversal algo Used 38ms
user3609V
0
67
longest absolute file path
388
0.465
Medium
6,668
https://leetcode.com/problems/longest-absolute-file-path/discuss/1846744/Python-Stack-one-pass-no-copy-(28-ms-13.9-MB-)
class Solution: def lengthLongestPath(self, input: str) -> int: # Analyses line starting at index i (counts tabs and chars, checks if file) def analize(i): n_tabs = 0 is_file = False size = 0 while i < len(input) and input[i] != '\n': if input[i] == '.': is_file = True if input[i] == '\t': n_tabs += 1 else: size += 1 i += 1 return n_tabs, size, is_file, i + 1 stack = [] max_len = 0 i = 0 while i < len(input): ntabs, size, is_file, i = analize(i) stack = stack[:ntabs] stack.append(size) if is_file: max_len = max(max_len, sum(x + 1 for x in stack) - 1) return max_len
longest-absolute-file-path
Python Stack, one pass, no copy (28 ms, 13.9 MB )
Lrnx
0
220
longest absolute file path
388
0.465
Medium
6,669
https://leetcode.com/problems/longest-absolute-file-path/discuss/1709363/Python-or-Monotonic-Stack
class Solution: def lengthLongestPath(self, input: str) -> int: #Splits all lines filePath = input.split('\n') st = [(-1,0)] maxLen = 0 for file in filePath: #counts how deep the file is levels = file.count('\t') #pop until you are able to add to stack while levels <= st[-1][0]: st.pop() #append new file, along with length st.append((levels, st[-1][1] + len(file) - levels)) if '.' in file: #check let's us confirm if it is a file and not a directory maxLen = max(maxLen, st[-1][1] + len(st)-2) return maxLen
longest-absolute-file-path
Python | Monotonic Stack
anonme_
0
255
longest absolute file path
388
0.465
Medium
6,670
https://leetcode.com/problems/longest-absolute-file-path/discuss/1477923/Python3-beats-100-traversing-a-given-string-only-once
class Solution: def lengthLongestPath(self, input: str) -> int: level = 0 # e.g. 'file1.ext' elem = '' is_file = False # e.g. ['dir', 'subdir1'] dir_paths = [] ans = 0 for ch in input: if ch == '\n': if is_file: ans = max(ans, len('/'.join(dir_paths[:level] + [elem]))) else: if level == len(dir_paths): dir_paths.append(elem) else: dir_paths[level] = elem # reset variables level = 0 elem = '' is_file = False continue if ch == '\t': level += 1 continue if ch == '.': is_file = True elem += ch if is_file: ans = max(ans, len('/'.join(dir_paths[:level] + [elem]))) return ans
longest-absolute-file-path
Python3 - beats 100% - traversing a given string only once
mnogu
0
487
longest absolute file path
388
0.465
Medium
6,671
https://leetcode.com/problems/longest-absolute-file-path/discuss/482739/Python-3-stack-of-lengths-one-pass.-(24ms-faster-than-84.94-12.5-MB-less-than-100.00.)
class Solution: def lengthLongestPath(self, input: str) -> int: longest = 0 path = [] i = 0 while i < len(input): length = 0 depth = 0 is_file = False in_path_component = False whitespaces = 0 while i < len(input) and input[i] != '\n': if input[i] == '\t': depth += 1 elif input[i] == ' ': if in_path_component: # We may have whitespaces in folder and file names: length += 1 else: # Some test cases seem to use 4 whitespaces instead of one tab character: whitespaces += 1 if whitespaces == 4: depth += 1 whitespaces = 0 elif input[i] == '.': in_path_component = True is_file = True length += 1 elif input[i] != '\n' and input[i] != '\t': in_path_component = True length += 1 i += 1 # Move back up the file tree to where we currently are: while depth < len(path): path.pop() if is_file: # We may have leading whitespaces which could be mistaken with depth. Add these too. length += (depth-len(path))*4 # We may have leading whitespaces in case we saw a number of whitespaces which was not a multiple of 4. Add these too. length += whitespaces longest = max(longest, sum(path) + length) else: path.append(length+1) # +1 as we need to take the separator "/" into account. i += 1 return longest
longest-absolute-file-path
[Python 3] stack of lengths one pass. (24ms, faster than 84.94%; 12.5 MB, less than 100.00%.)
marccarre
0
271
longest absolute file path
388
0.465
Medium
6,672
https://leetcode.com/problems/find-the-difference/discuss/379846/Three-Solutions-in-Python-3
class Solution: def findTheDifference(self, s: str, t: str) -> str: s, t = sorted(s), sorted(t) for i,j in enumerate(s): if j != t[i]: return t[i] return t[-1]
find-the-difference
Three Solutions in Python 3
junaidmansuri
23
2,300
find the difference
389
0.603
Easy
6,673
https://leetcode.com/problems/find-the-difference/discuss/379846/Three-Solutions-in-Python-3
class Solution: def findTheDifference(self, s: str, t: str) -> str: for i in set(t): if s.count(i) != t.count(i): return i
find-the-difference
Three Solutions in Python 3
junaidmansuri
23
2,300
find the difference
389
0.603
Easy
6,674
https://leetcode.com/problems/find-the-difference/discuss/2080501/Python-one-liner-solution-oror-Simple-oror-Easy
class Solution: def findTheDifference(self, s: str, t: str) -> str: return list((Counter(t) - Counter(s)).keys())[0]
find-the-difference
Python one liner solution || Simple || Easy
Shivam_Raj_Sharma
8
243
find the difference
389
0.603
Easy
6,675
https://leetcode.com/problems/find-the-difference/discuss/1751342/Python-Bit-Manipulation
class Solution: def findTheDifference(self, s: str, t: str) -> str: ans = 0 for i in range(len(s)): ans = ans ^ ord(s[i]) ^ ord(t[i]) ans ^= ord(t[-1]) return chr(ans)
find-the-difference
Python Bit Manipulation
aunpatiphan
7
300
find the difference
389
0.603
Easy
6,676
https://leetcode.com/problems/find-the-difference/discuss/1845160/Python-Clean-and-Concise!-Multiple-Solutions!
class Solution: def findTheDifference(self, s, t): r = [0] * 26 for c in s: r[ord(c) - ord('a')] -= 1 for c in t: r[ord(c) - ord('a')] += 1 for i,n in enumerate(r): if n > 0: return chr(i + ord('a'))
find-the-difference
Python - Clean and Concise! Multiple Solutions!
domthedeveloper
4
111
find the difference
389
0.603
Easy
6,677
https://leetcode.com/problems/find-the-difference/discuss/1845160/Python-Clean-and-Concise!-Multiple-Solutions!
class Solution: def findTheDifference(self, s, t): s, t = list(s), list(t) s.sort() t.sort() for i,c in enumerate(s): if c != t[i]: return t[i] return t[-1]
find-the-difference
Python - Clean and Concise! Multiple Solutions!
domthedeveloper
4
111
find the difference
389
0.603
Easy
6,678
https://leetcode.com/problems/find-the-difference/discuss/1845160/Python-Clean-and-Concise!-Multiple-Solutions!
class Solution: def findTheDifference(self, s, t): for c in set(t): if s.count(c) != t.count(c): return c
find-the-difference
Python - Clean and Concise! Multiple Solutions!
domthedeveloper
4
111
find the difference
389
0.603
Easy
6,679
https://leetcode.com/problems/find-the-difference/discuss/1845160/Python-Clean-and-Concise!-Multiple-Solutions!
class Solution: def findTheDifference(self, s, t): return next(iter(Counter(t) - Counter(s)))
find-the-difference
Python - Clean and Concise! Multiple Solutions!
domthedeveloper
4
111
find the difference
389
0.603
Easy
6,680
https://leetcode.com/problems/find-the-difference/discuss/1845160/Python-Clean-and-Concise!-Multiple-Solutions!
class Solution: def findTheDifference(self, s, t): ans = 0 for c in s: ans -= ord(c) for c in t: ans += ord(c) return chr(ans)
find-the-difference
Python - Clean and Concise! Multiple Solutions!
domthedeveloper
4
111
find the difference
389
0.603
Easy
6,681
https://leetcode.com/problems/find-the-difference/discuss/1845160/Python-Clean-and-Concise!-Multiple-Solutions!
class Solution: def findTheDifference(self, s, t): ans = 0 for c in s: ans ^= ord(c) for c in t: ans ^= ord(c) return chr(ans)
find-the-difference
Python - Clean and Concise! Multiple Solutions!
domthedeveloper
4
111
find the difference
389
0.603
Easy
6,682
https://leetcode.com/problems/find-the-difference/discuss/1845160/Python-Clean-and-Concise!-Multiple-Solutions!
class Solution: def findTheDifference(self, s, t): return chr(reduce(lambda a,b : a^ord(b), s+t, 0))
find-the-difference
Python - Clean and Concise! Multiple Solutions!
domthedeveloper
4
111
find the difference
389
0.603
Easy
6,683
https://leetcode.com/problems/find-the-difference/discuss/1845160/Python-Clean-and-Concise!-Multiple-Solutions!
class Solution: def findTheDifference(self, s, t): return chr(reduce(operator.xor,map(ord,s+t)))
find-the-difference
Python - Clean and Concise! Multiple Solutions!
domthedeveloper
4
111
find the difference
389
0.603
Easy
6,684
https://leetcode.com/problems/find-the-difference/discuss/1845160/Python-Clean-and-Concise!-Multiple-Solutions!
class Solution: def findTheDifference(self, s, t): return chr(sum(map(ord,t))-sum(map(ord,s)))
find-the-difference
Python - Clean and Concise! Multiple Solutions!
domthedeveloper
4
111
find the difference
389
0.603
Easy
6,685
https://leetcode.com/problems/find-the-difference/discuss/297178/Python-faster-than-99-16-ms
class Solution(object): def findTheDifference(self, s, t): """ :type s: str :type t: str :rtype: str """ s = sorted(list(s)) t = sorted(list(t)) for i in range(len(s)): if s[i]!=t[i]: return t[i] return t[-1]
find-the-difference
Python - faster than 99%, 16 ms
il_buono
4
615
find the difference
389
0.603
Easy
6,686
https://leetcode.com/problems/find-the-difference/discuss/2646443/Beginner-friendly-PYTHON-solution
class Solution: def findTheDifference(self, s: str, t: str) -> str: for i in t: if i not in s or s.count(i)<t.count(i): return i
find-the-difference
Beginner friendly PYTHON solution
envyTheClouds
3
199
find the difference
389
0.603
Easy
6,687
https://leetcode.com/problems/find-the-difference/discuss/1736401/Python-3-(50ms)-or-One-Liner-Dictionary-Solution-using-Counter
class Solution: def findTheDifference(self, s: str, t: str) -> str: return list((Counter(t)-Counter(s)))[0]
find-the-difference
Python 3 (50ms) | One-Liner Dictionary Solution using Counter
MrShobhit
3
256
find the difference
389
0.603
Easy
6,688
https://leetcode.com/problems/find-the-difference/discuss/1686901/85-faster-easy-python-solution
class Solution: def findTheDifference(self, s: str, t: str) -> str: lst1=sorted(list(s)) lst2=sorted(list(t)) try: for i in range(len(lst2)): if lst2[i]!=lst1[i]: return lst2[i] except: #checking for the last index in 't' string return lst2[len(lst2)-1]
find-the-difference
85% faster easy python solution
amannarayansingh10
2
134
find the difference
389
0.603
Easy
6,689
https://leetcode.com/problems/find-the-difference/discuss/1248551/Easy-Python-Solution
class Solution: def findTheDifference(self, s: str, t: str) -> str: s=Counter(s) t=Counter(t) for i,v in t.items(): if(s[i]==v): continue return i
find-the-difference
Easy Python Solution
Sneh17029
2
441
find the difference
389
0.603
Easy
6,690
https://leetcode.com/problems/find-the-difference/discuss/407656/Python-Simple-Solution
class Solution: def findTheDifference(self, s: str, t: str) -> str: t = list(t) s = list(s) for i in s: t.remove(i) return t[0]
find-the-difference
Python Simple Solution
saffi
2
469
find the difference
389
0.603
Easy
6,691
https://leetcode.com/problems/find-the-difference/discuss/2673023/Faster-than-98.11-Solution
class Solution: def findTheDifference(self, s: str, t: str) -> str: ds = {} for i in range(len(s)): ds[s[i]] = 1 + ds.get(s[i],0) dt = {} for i in range(len(t)): dt[t[i]] = 1 + dt.get(t[i], 0) for char in t: if char not in ds or dt[char] != ds[char]: return char
find-the-difference
Faster than 98.11% Solution
baeyong
1
341
find the difference
389
0.603
Easy
6,692
https://leetcode.com/problems/find-the-difference/discuss/2469897/Python-simple-solution-using-counter
class Solution: def findTheDifference(self, s: str, t: str) -> str: dic = collections.Counter(t) for i in s: if i in dic: dic[i] -= 1 for k,v in dic.items(): if dic[k] == 1: return k
find-the-difference
Python simple solution using counter
aruj900
1
78
find the difference
389
0.603
Easy
6,693
https://leetcode.com/problems/find-the-difference/discuss/2354699/Solution
class Solution: def findTheDifference(self, s: str, t: str) -> str: if s == "": return t s = sorted(s) t = sorted(t) for i in range(len(s)): if s[i] != t[i]: return t[i] return t[len(t)-1]
find-the-difference
Solution
fiqbal997
1
31
find the difference
389
0.603
Easy
6,694
https://leetcode.com/problems/find-the-difference/discuss/2243649/Python3-solution-using-xor
class Solution: def findTheDifference(self, s: str, t: str) -> str: if not s: return t c= ord(s[0]) for i in range(1,len(s)): c= c ^ ord(s[i]) for i in range(len(t)): c= c ^ ord(t[i]) return chr(c)
find-the-difference
πŸ“Œ Python3 solution using xor
Dark_wolf_jss
1
17
find the difference
389
0.603
Easy
6,695
https://leetcode.com/problems/find-the-difference/discuss/2163479/Python-One-Liner-Solution-Counter
class Solution: def findTheDifference(self, s: str, t: str) -> str: return list(Counter(t)-Counter(s))[0]
find-the-difference
Python One Liner Solution - Counter
pruthashouche
1
48
find the difference
389
0.603
Easy
6,696
https://leetcode.com/problems/find-the-difference/discuss/2137879/Find-the-Difference
class Solution(object): def findTheDifference(self, s, t): """ :type s: str :type t: str :rtype: str """ s = sorted(s) t = sorted(t) for i in range(len(s)): if t[i] != s[i]: return t[i] return t[-1]
find-the-difference
Find the Difference
abhinav0904
1
90
find the difference
389
0.603
Easy
6,697
https://leetcode.com/problems/find-the-difference/discuss/2130999/Python3-Solution-easily-explained.
class Solution: def findTheDifference(self, s: str, t: str) -> str: # so first we look at the easy understandable solution: # we convert both our strings into lists and sort them # and since we know t has only +1 of length of s # we add an empty string ("") to s to avoid index out # of range error. # Now we check if i index value of s and t are not similar # we return our t[i] value since it's what added more to t. s = list(s) t = list(t) s.sort() t.sort() s.append("") for i in range(len(t)): if s[i] != t[i]: return t[i]
find-the-difference
Python3 Solution, easily explained.
shubhamdraj
1
137
find the difference
389
0.603
Easy
6,698
https://leetcode.com/problems/find-the-difference/discuss/2130999/Python3-Solution-easily-explained.
class Solution: def findTheDifference(self, s: str, t: str) -> str: # using collections.Counter() function it gives # a dictionary of having every character as key # and the occurrence of that character as value of that key. # Now we look at every character in t string see if it's not present # in dictionary's keys or if that character has 0 occurrences in cnt(the dict) # if yes then that's the character we're looking for. # Else we reduce every other character's occurences by 1 to avoid confusion. import collections cnt = collections.Counter(s) for c in t: if c not in cnt or cnt[c] == 0: return c else: cnt[c] -= 1
find-the-difference
Python3 Solution, easily explained.
shubhamdraj
1
137
find the difference
389
0.603
Easy
6,699