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/reverse-string/discuss/2797640/Simple-Python3-Solution
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ s[:] = s[::-1]
reverse-string
Simple Python3 Solution
vivekrajyaguru
0
2
reverse string
344
0.762
Easy
6,000
https://leetcode.com/problems/reverse-string/discuss/2797550/Python-2-Approaches
class Solution: def reverseString(self, s: List[str]) -> None: # method 1: better speed s[::]=s[::-1] # method 2: better space # l,r=0,len(s)-1 # while l<r: # s[l],s[r]=s[r],s[l] # l+=1 # r-=1
reverse-string
Python 2 Approaches
sbhupender68
0
2
reverse string
344
0.762
Easy
6,001
https://leetcode.com/problems/reverse-string/discuss/2788957/Best-Easy-Approach-oror-99.63-Acceptance-oror-TC-O(N)
class Solution: def reverseString(self, s: List[str]) -> None: i=0 j=len(s)-1 while(i<j): s[i],s[j]=s[j],s[i] i+=1 j-=1 """ Do not return anything, modify s in-place instead. """
reverse-string
Best Easy Approach || 99.63% Acceptance || TC O(N)
Kaustubhmishra
0
2
reverse string
344
0.762
Easy
6,002
https://leetcode.com/problems/reverse-string/discuss/2782891/Two-pointers-beats-86
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ l, r = 0,len(s)-1 while l < r: s[l], s[r] = s[r], s[l] l += 1 r -=1
reverse-string
Two pointers, beats 86%
haniyeka
0
3
reverse string
344
0.762
Easy
6,003
https://leetcode.com/problems/reverse-string/discuss/2782792/python-Easiest-one-line-solution-as-everyone-knows
class Solution: def reverseString(self, s: List[str]) -> None: s.reverse() # Runtime: 633 ms, faster than 5.25% of Python3 online submissions for Reverse String. # Memory Usage: 18.4 MB, less than 83.12% of Python3 online submissions for Reverse String.
reverse-string
python Easiest one-line solution as everyone knows
yutoun
0
3
reverse string
344
0.762
Easy
6,004
https://leetcode.com/problems/reverse-string/discuss/2781302/Python-2-Simple-Solutions-oror-List-comprehension-oror-Swapping
class Solution: def reverseString(self, s: List[str]) -> None: s[:] = s[::-1]
reverse-string
[Python] 2 Simple Solutions || List comprehension || Swapping
shahbaz95ansari
0
1
reverse string
344
0.762
Easy
6,005
https://leetcode.com/problems/reverse-string/discuss/2781302/Python-2-Simple-Solutions-oror-List-comprehension-oror-Swapping
class Solution: def reverseString(self, s: List[str]) -> None: l, r = 0, len(s) - 1 while l < r: s[l], s[r] = s[r], s[l] l += 1 r -= 1
reverse-string
[Python] 2 Simple Solutions || List comprehension || Swapping
shahbaz95ansari
0
1
reverse string
344
0.762
Easy
6,006
https://leetcode.com/problems/reverse-string/discuss/2779873/Easy-4-liner-Python-Solutions-Beats-96-of-Submissions
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ n = len(s) for i in range(n // 2): tmp = s[i] s[i] = s[n - i - 1] s[n - i - 1] = tmp
reverse-string
Easy 4 liner Python Solutions Beats 96% of Submissions
gpersonnat
0
1
reverse string
344
0.762
Easy
6,007
https://leetcode.com/problems/reverse-string/discuss/2778176/easy-python-solutionbeats-95-!!!
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ first=0 last=len(s)-1 for i in range(len(s)//2): s[first],s[last]=s[last],s[first] first+=1 last-=1 return s
reverse-string
easy python solution,beats 95% !!!
Harshit-chaudhary_01
0
5
reverse string
344
0.762
Easy
6,008
https://leetcode.com/problems/reverse-string/discuss/2775298/faster-than-99.66-less-than-83.23
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ first = 0 last = len(s) - 1 if last % 2 == 0: mid = first + last // 2 else: mid = first + last // 2 + 1 for i in range(0, mid): s[i], s[last - i] = s[last - i], s[i]
reverse-string
faster than 99.66%, less than 83.23%
eckyrie0921
0
3
reverse string
344
0.762
Easy
6,009
https://leetcode.com/problems/reverse-string/discuss/2772061/Reverse-String-oror-Python-Simple-and-Crisp-Two-Pointer-Approach
class Solution: def reverseString(self, s: List[str]) -> None: # Keep 2 Pointers, 1 at the start of the string and the other at the end i,j=0,len(s)-1 while i<j: s[i],s[j]=s[j],s[i] i+=1 j-=1
reverse-string
Reverse String || Python Simple and Crisp Two Pointer Approach
vedanthvbaliga
0
2
reverse string
344
0.762
Easy
6,010
https://leetcode.com/problems/reverse-string/discuss/2769725/Python-Super-Simple-beat-98.9
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ for i in range(0,int(len(s)/2)): temp = s[i] s[i] = s[-(1+i)] s[-(1+i)] = temp
reverse-string
Python Super Simple, beat 98.9%
paddyveith987
0
2
reverse string
344
0.762
Easy
6,011
https://leetcode.com/problems/reverse-string/discuss/2769086/Reverse-string
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ res = s[::-1] for i in range(0,len(s)): s[i] = res[i]
reverse-string
Reverse string
keerthikrishnakumar93
0
4
reverse string
344
0.762
Easy
6,012
https://leetcode.com/problems/reverse-string/discuss/2765025/Two-pointers-solution.
class Solution: def reverseString(self, s: List[str]) -> None: L, R = 0, len(s)-1 while L < R: s[L], s[R] = s[R], s[L] L += 1 R -= 1 # or just simply s.reverse()
reverse-string
Two pointers solution.
woora3
0
2
reverse string
344
0.762
Easy
6,013
https://leetcode.com/problems/reverse-string/discuss/2746165/python-two-line-solution-memory-beats-99
class Solution: def reverseString(self, s: List[str]) -> None: l = len(s) for i in range(l//2): s[i], s[l-1-i] = s[l-1-i],s[i]
reverse-string
python two line solution memory beats 99%
muge_zhang
0
3
reverse string
344
0.762
Easy
6,014
https://leetcode.com/problems/reverse-string/discuss/2744560/Reverse-String-3-line-solution-in-python
class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ n = len(s)-1 for i in range(n,-1,-1): s.append(s[i]) s.pop(i)
reverse-string
Reverse String 3 line solution in python
jashii96
0
4
reverse string
344
0.762
Easy
6,015
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/1164745/Python-Solution-oror-99.58-faster-oror-86.96-less-memory
class Solution: def reverseVowels(self, s: str) -> str: s = list(s) left = 0 right = len(s) - 1 m = 'aeiouAEIOU' while left < right: if s[left] in m and s[right] in m: s[left], s[right] = s[right], s[left] left += 1; right -= 1 elif s[left] not in m: left += 1 elif s[right] not in m: right -= 1 return ''.join(s)
reverse-vowels-of-a-string
Python Solution || 99.58% faster || 86.96% less memory
KiranUpase
22
1,100
reverse vowels of a string
345
0.498
Easy
6,016
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2775366/python-iterator-simple-2-lines
class Solution: def reverseVowels(self, s: str) -> str: it = (ch for ch in s[::-1] if ch.lower() in 'aeiou') return ''.join(next(it) if ch.lower() in 'aeiou' else ch for ch in s)
reverse-vowels-of-a-string
python iterator simple 2 lines
alvin-777
12
711
reverse vowels of a string
345
0.498
Easy
6,017
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2775704/easy-simple-bruteforce
class Solution: def reverseVowels(self, s: str) -> str: loc=[] s=list(s) for i in range(len(s)): if(s[i] in "aeiouAEIOU"): loc.append(i) for i in range(len(loc)//2): s[loc[i]],s[loc[-i-1]]=s[loc[-i-1]],s[loc[i]] return "".join(s)
reverse-vowels-of-a-string
easy simple bruteforce
droj
4
141
reverse vowels of a string
345
0.498
Easy
6,018
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2779022/Python-Simple-Python-Solution-97-ms
class Solution: def reverseVowels(self, s: str) -> str: l="aeiouAEIOU" s=list(s) i,j=0,len(s)-1 while(i<j): if s[i] in l and s[j] in l: s[i],s[j]=s[j],s[i] i+=1 j-=1 elif s[i] not in l: i+=1 elif s[j] not in l: j-=1 return ''.join(s)
reverse-vowels-of-a-string
[ Python ] 🐍🐍 Simple Python Solution βœ…βœ… 97 ms
sourav638
2
11
reverse vowels of a string
345
0.498
Easy
6,019
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2022664/Python-2-Pointers
class Solution: def reverseVowels(self, s: str) -> str: i, j = 0, len(s)-1 vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} s = list(s) while i < j: while i < j and s[i] not in vowels: i+=1 while i < j and s[j] not in vowels: j-=1 if i<j: s[j], s[i] = s[i], s[j] i+=1 j-=1 return ''.join(s)
reverse-vowels-of-a-string
Python 2 Pointers
constantine786
2
155
reverse vowels of a string
345
0.498
Easy
6,020
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/1264637/Easy-Python-Solution
class Solution: def reverseVowels(self, s: str) -> str: left=0 s=list(s) x=['a','e','i','o','u'] right=len(s)-1 while left<right: if(s[left].lower() in x and s[right].lower() in x): s[left],s[right]=s[right],s[left] left+=1 right-=1 if(s[left].lower() not in x): left+=1 if(s[right].lower() not in x): right-=1 s="".join(s) return s
reverse-vowels-of-a-string
Easy Python Solution
Sneh17029
2
487
reverse vowels of a string
345
0.498
Easy
6,021
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/476707/Reverse-vowels-of-a-string
class Solution: def reverseVowels(self, s: str) -> str: vowels = ["a","e","i","o","u","A","E","I","O","U"] li = list(s) i = 0 j = len(li)-1 while(i<j): if(li[i] not in vowels and li[j] not in vowels): i+=1 j-=1 if(li[i] not in vowels and li[j] in vowels): i+=1 if(li[j] not in vowels and li[i] in vowels): j-=1 if(li[i] in vowels and li[j] in vowels): li[i],li[j] = li[j],li[i] i+=1 j-=1 return "".join(li)
reverse-vowels-of-a-string
Reverse vowels of a string
qwe121
2
186
reverse vowels of a string
345
0.498
Easy
6,022
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2802052/Two-Pointer-or-Python
class Solution: def reverseVowels(self, s: str) -> str: v = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"] l, r = 0, len(s) - 1 out = list(s) while l < r: if s[l] in v and s[r] in v: out[l] = s[r] out[r] = s[l] l += 1 r -= 1 elif s[l] not in v: l += 1 elif s[r] not in v: r -= 1 return("".join(out))
reverse-vowels-of-a-string
Two Pointer | Python
jaisalShah
1
8
reverse vowels of a string
345
0.498
Easy
6,023
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2777389/FASTEST-AND-EASIEST-oror-BEATS-99-SUBMISSIONS
class Solution: def reverseVowels(self, s: str) -> str: v="aeiouAEIOU" s=list(s) l=0 r=len(s)-1 while l<r : if s[l] in v and s[r] in v : s[l],s[r]=s[r],s[l] l +=1 r -=1 elif s[l] not in v : l +=1 else : r -=1 return ''.join(s)
reverse-vowels-of-a-string
FASTEST AND EASIEST || BEATS 99% SUBMISSIONS
Pritz10
1
7
reverse vowels of a string
345
0.498
Easy
6,024
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2776981/Python-Two-Pointer-Approach-O(n)
class Solution: def reverseVowels(self, s: str) -> str: s_list = list(s) vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"] start = 0 end = len(s_list) - 1 while start < end: if (s_list[start] not in vowels): start +=1 if (s_list[end] not in vowels): end -= 1 if s_list[start] in vowels and s_list[end] in vowels: s_list[start], s_list[end] = s_list[end], s_list[start] start +=1 end -= 1 return "".join(s_list)
reverse-vowels-of-a-string
Python Two Pointer Approach O(n)
brains_Up
1
24
reverse vowels of a string
345
0.498
Easy
6,025
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2776111/Python-2-Simple-and-Easy-Way-to-Solve-or-97-Faster
class Solution: def reverseVowels(self, s: str) -> str: s = list(s) vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] vowels_in_s = [] for i, c in enumerate(s): if c in vowels: vowels_in_s.append(c) s[i] = None p = 0 vowels_in_s = vowels_in_s[::-1] for i, c in enumerate(s): if c == None: s[i] = vowels_in_s[p] p+=1 return ''.join(s)
reverse-vowels-of-a-string
βœ”οΈ Python 2 Simple and Easy Way to Solve | 97% Faster πŸ”₯
pniraj657
1
87
reverse vowels of a string
345
0.498
Easy
6,026
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2776111/Python-2-Simple-and-Easy-Way-to-Solve-or-97-Faster
class Solution: def reverseVowels(self, s: str) -> str: v="aeiouAEIOU" l=list(s) i=0 j=(len(s)-1) while i<j: while i<j and l[i] not in v: i+=1 while j>i and l[j] not in v: j-=1 l[i],l[j]=l[j],l[i] i+=1 j-=1 return "".join(l)
reverse-vowels-of-a-string
βœ”οΈ Python 2 Simple and Easy Way to Solve | 97% Faster πŸ”₯
pniraj657
1
87
reverse vowels of a string
345
0.498
Easy
6,027
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2775641/Python-easy-to-read-linear-solution
class Solution: def reverseVowels(self, s: str) -> str: vowels = ['a', 'e', 'i', 'o', 'u'] s = list(s) left, right = 0, len(s)-1 while right > left: leftS = s[left].lower() rightS = s[right].lower() if leftS not in vowels: left += 1 continue if rightS not in vowels: right -= 1 continue s[left], s[right] = s[right], s[left] left, right = left+1, right-1 return ''.join(s)
reverse-vowels-of-a-string
Python easy to read linear solution
really_cool_person
1
45
reverse vowels of a string
345
0.498
Easy
6,028
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2775401/Simple-Python-Two-pointers-Solution
class Solution: def reverseVowels(self, s: str) -> str: vowels = set('aeiouAEIOU') s = list(s) l, r = 0, len(s)-1 while l < r: while s[l] not in vowels and l < r: l+=1 while s[r] not in vowels and r > l: r-=1 s[l], s[r] = s[r], s[l] l+=1 r-=1 return "".join(s)
reverse-vowels-of-a-string
Simple Python Two pointers Solution
tragob
1
9
reverse vowels of a string
345
0.498
Easy
6,029
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/1871260/Python-easy-to-read-and-understand-or-stack
class Solution: def reverseVowels(self, s: str) -> str: vow = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] t = [] for i in s: if i in vow: t.append(i) ans = '' for i in s: if i in vow: ans += t.pop() else: ans += i return ans
reverse-vowels-of-a-string
Python easy to read and understand | stack
sanial2001
1
162
reverse vowels of a string
345
0.498
Easy
6,030
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/1579170/Runtime%3A-48-ms-faster-than-88.96-of-Python3
class Solution: def reverseVowels(self, s: str) -> str: vowel={'a','e','i','o','u','A','E','I','O','U'} i=0 j=len(s)-1 s=list(s) while i<j: if s[i] in vowel and s[j] in vowel: s[i],s[j]=s[j],s[i] i+=1 j-=1 elif s[i] in vowel and s[j] not in vowel: j-=1 elif s[i] not in vowel and s[j] in vowel: i+=1 else: i+=1 j-=1 return "".join(s)
reverse-vowels-of-a-string
Runtime: 48 ms, faster than 88.96% of Python3
topensite
1
110
reverse vowels of a string
345
0.498
Easy
6,031
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/1500820/Build-Approach-from-Scratch-greater-Thought-Process
class Solution: def reverseVowels(self, s: str) -> str: arr = list(s) #arr = ["h", "e", "l", "l", "o"] n = len(arr) vowels = {"a", "e", "i", "o", "u", "A", "E", "I", "O", "U"} #checking an element across a set is O(1) #initialising Two Pointers - one to move from front and other to move from back i = 0 #this will move from 0th index - will take care from front j = n - 1 #this will move from (n-1)th index - will take care from back #Termination Condition is i < j because as soon as i >= j, it means we have covered all the elements #from front as well as back while i < j : #if we do not find a vowel while moving from front, lets move ahead if arr[i] not in vowels: i += 1 #if we do not find a vowel while moving from back, lets move ahead if arr[j] not in vowels: j -= 1 #if we find a vowel from front as well as from behind, we have to swap them #and then, move ahead from front and move back from behind by 1 unit respectively #Note - The termination condition will take care of any eventuality if arr[i] in vowels and arr[j] in vowels: arr[i],arr[j] = arr[j],arr[i] #swapping them i += 1 j -= 1 answer = "" #we will iterate over the array and keep adding all elements to a string #as output is required in the form of a string for i in arr: answer += i return answer
reverse-vowels-of-a-string
Build Approach from Scratch --> Thought Process
aarushsharmaa
1
97
reverse vowels of a string
345
0.498
Easy
6,032
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/1392280/WEEB-DOES-PYTHON-(BEATS-99.71)
class Solution: def reverseVowels(self, s: str) -> str: vowels = set(list("AEIOUaeiou")) high, s = len(s)-1, list(s) for low in range(len(s)): if s[low] in vowels: while s[high] not in vowels: high-=1 if low == high: break # print("b4: ", s[low], s[high]) s[low], s[high] = s[high], s[low] # print("after: ", s[low], s[high]) high-=1 if low == high: break return "".join(s)
reverse-vowels-of-a-string
WEEB DOES PYTHON (BEATS 99.71%)
Skywalker5423
1
199
reverse vowels of a string
345
0.498
Easy
6,033
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/1239737/Python3-simple-solution-using-two-pointer-approach
class Solution: def reverseVowels(self, s: str) -> str: i,j=0,len(s)-1 l = list(s) vowels = ['a','e','i','o','u'] while i < j: if l[i].lower() not in vowels: i += 1 if l[j].lower() not in vowels: j -= 1 if l[i].lower() in vowels and l[j].lower() in vowels: l[i],l[j] = l[j],l[i] i += 1 j -= 1 return ''.join(l)
reverse-vowels-of-a-string
Python3 simple solution using two pointer approach
EklavyaJoshi
1
69
reverse vowels of a string
345
0.498
Easy
6,034
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/807615/simple-of-simple
class Solution: def reverseVowels(self, s: str) -> str: words = [x for x in s] vowels = [x for x in 'aeiou'] t = [] for i in range(len(words)): if words[i].lower() in vowels: t.append(words[i]) words[i] = None for i in range(len(words)): if words[i] is None: words[i] = t.pop() return ''.join(words)
reverse-vowels-of-a-string
simple of simple
seunggabi
1
57
reverse vowels of a string
345
0.498
Easy
6,035
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/737069/Python-Two-Pointers
class Solution: def reverseVowels(self, s: str) -> str: # Two pointers solution s = list(s) i, j = 0, len(s) -1 vowels = set("aeiouAEIOU") while i < j: # Swap if s[i] in vowels and s[j] in vowels: s[i], s[j] = s[j], s[i] i += 1 j -= 1 # Decrement by 1 elif s[j] not in vowels: j -= 1 # Increment by 1 elif s[i] not in vowels: i += 1 return "".join(s)
reverse-vowels-of-a-string
Python Two Pointers
adhishthite
1
202
reverse vowels of a string
345
0.498
Easy
6,036
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2842645/Python-2-Pointer-Easy-Solution
class Solution: def reverseVowels(self, s: str) -> str: s=list(s) ot="" l=[] vowel=["a","e","i","o","u","A","E","I","O","U"] for i in range(len(s)): if s[i] in vowel : l.append(i) for i in range(len(l)//2) : s[l[i]],s[l[len(l)-i-1]]=s[l[len(l)-1-i]],s[l[i]] het="" return(het.join(s))
reverse-vowels-of-a-string
Python 2 Pointer Easy Solution
patelhet050603
0
1
reverse vowels of a string
345
0.498
Easy
6,037
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2838706/Python3-Clean-Solution
class Solution: def reverseVowels(self, s: str) -> str: i = 0 j = len(s)-1 vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] s = list(s) while i <= j: ci = s[i] cj = s[j] if s[i] in vowels and s[j] in vowels: s[i], s[j] = s[j], s[i] i += 1 j -= 1 elif s[i] in vowels and s[j] not in vowels: j -= 1 elif s[i] not in vowels and s[j] in vowels: i += 1 else: j -= 1 i += 1 return "".join(s)
reverse-vowels-of-a-string
βœ… Python3 Clean Solution
Cecilia_GuoChen
0
1
reverse vowels of a string
345
0.498
Easy
6,038
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2810694/easy-solution-in-python
class Solution: def reverseVowels(self, s: str) -> str: vowels = ['a', 'e', 'i', 'o','u', 'A', 'E', 'I', 'O', 'U'] vs = '' vc = 0 res = '' for i in range(0, len(s)): if s[i] in vowels: vs+=s[i] vs = vs[::-1] for i in range(0, len(s)): if s[i] in vowels: res+=vs[vc] vc+=1 continue res+=s[i] return res
reverse-vowels-of-a-string
easy solution in python
user1366SF
0
4
reverse vowels of a string
345
0.498
Easy
6,039
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2791882/PYTHON91.54-FASTEREXPLAINED.
class Solution: def reverseVowels(self, s: str) -> str: chars = list(s) N = len(chars) left = 0 right = len(s)-1 vowels = "aeiou" while left < right: while left < N and chars[left].lower() not in vowels: left += 1 while right >=0 and chars[right].lower() not in vowels: right -= 1 if left < right: chars[left], chars[right] = chars[right], chars[left] left += 1 right -= 1 return "".join(chars)
reverse-vowels-of-a-string
PYTHON🐍91.54% FASTERβœ…EXPLAINEDπŸ’₯.
shubhamdraj
0
5
reverse vowels of a string
345
0.498
Easy
6,040
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2788241/Python-Iterative-solution-(Easy-to-understand)
class Solution: def reverseVowels(self, s: str) -> str: w = ['a', 'e', 'i', 'o','u',"A","E","I","O","U"] wl = "" tw = '' for i in range(len(s)): if(s[i] in w): tw+="=" wl+=s[i] else: tw+=s[i] wl = wl[::-1] out ="" cnt=0 for i in range(len(tw)): if(tw[i]=="="): out+=wl[cnt] cnt+=1 else: out+=tw[i] return out
reverse-vowels-of-a-string
[Python] Iterative solution (Easy to understand)
thesaderror
0
3
reverse vowels of a string
345
0.498
Easy
6,041
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2785560/Two-Pointers-Simple-or-Python
class Solution: def reverseVowels(self, s: str) -> str: s = list(s) l, r = 0, len(s)-1 vows = 'aeiouAEIOU' while l <r: while l< r and s[l] not in vows: l +=1 while l < r and s[r] not in vows: r -= 1 if l > r: break s[l],s[r] = s[r],s[l] l += 1 r -= 1 return ''.join(s)
reverse-vowels-of-a-string
Two Pointers Simple | Python
Abhi_-_-
0
2
reverse vowels of a string
345
0.498
Easy
6,042
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2782558/Python-2-pointer-approach-w.-intuition-and-approach-explanation
class Solution: def reverseVowels(self, s: str) -> str: i = 0 j = len(s) - 1 s = list(s) vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} while i < j: if s[i] in vowels and s[j] in vowels: s[i], s[j] = s[j], s[i] i += 1 j -= 1 if s[i] not in vowels: i += 1 if s[j] not in vowels: j -= 1 return "".join(s)
reverse-vowels-of-a-string
Python - 2 pointer approach w. intuition & approach explanation
abhandar
0
4
reverse vowels of a string
345
0.498
Easy
6,043
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2781403/Python3-Two-Pointers-or-Reverse-and-Replace-(2-approaches)-O(n)
class Solution: def reverseVowels(self, s: str) : ss_list = [] for item in s: ss_list.append(item) data_vowel = "aeiouAEIOU" pL,pR = 0,len(ss_list)-1 while(pL<pR): if(ss_list[pL] in data_vowel and ss_list[pR] in data_vowel): temp = ss_list[pR] ss_list[pR]=ss_list[pL] ss_list[pL]=temp pL+=1 pR-=1 continue if(ss_list[pL] not in data_vowel): pL+=1 if(ss_list[pR] not in data_vowel): pR-=1 return "".join(ss_list)
reverse-vowels-of-a-string
[Python3] Two Pointers | Reverse and Replace (2 approaches) O(n)
worachote659
0
2
reverse vowels of a string
345
0.498
Easy
6,044
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2779272/Two-Pointers-Solution-or-Python3
class Solution: def reverseVowels(self, s: str) -> str: l, r = 0, len(s) - 1 vowels = {'a', 'e', 'o', 'i', 'u', 'A', 'E', 'O', 'U', 'I'} s = list(s) if r == 0: return ''.join(s) while l < r: if s[l] in vowels and s[r] in vowels: s[l], s[r] = s[r], s[l] l += 1 r -=1 elif s[l] in vowels and s[r] not in vowels: r -= 1 elif s[l] not in vowels and s[r] in vowels: l += 1 else: l += 1 r -=1 return ''.join(s)
reverse-vowels-of-a-string
Two Pointers Solution | Python3
omniaosman4
0
6
reverse vowels of a string
345
0.498
Easy
6,045
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2779189/Python-or-LeetCode-or-345.-Reverse-Vowels-of-a-String
class Solution: def reverseVowels(self, s: str) -> str: # vowel_letters = v v = "aeiouAEIOU" left = 0 right = len(s) - 1 s = list(s) while left < right: if (s[left] in v) and (s[right] in v): s[left], s[right] = s[right], s[left] left += 1 right -= 1 elif (s[left] in v) and (not (s[right] in v)): right -= 1 elif (not (s[left] in v)) and (s[right] in v): left += 1 else: left += 1 right -= 1 return "".join(s)
reverse-vowels-of-a-string
Python | LeetCode | 345. Reverse Vowels of a String
UzbekDasturchisiman
0
4
reverse vowels of a string
345
0.498
Easy
6,046
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778900/Python-O(n)-with-stack
class Solution: def reverseVowels(self, s: str) -> str: vowels = ['a', 'e', 'i', 'o', 'u'] stack = [] [stack.append(x) for x in s if x.lower() in vowels] print(stack) result = "" for letter in s: if letter.lower() in vowels: letter = stack.pop() result += letter return result
reverse-vowels-of-a-string
Python O(n) with stack
suhika
0
5
reverse vowels of a string
345
0.498
Easy
6,047
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778885/PYTHON-159-ms
class Solution: def reverseVowels(self, s: str) -> str: i = 0 j = len(s) - 1 vowels = list("aeiouAEIOU") s = list(s) while i<=j: if s[i] in vowels: while s[j] not in vowels and j>=i: j-=1 s[i],s[j] = s[j],s[i] j-=1 i+=1 return "".join(s)
reverse-vowels-of-a-string
βœ…PYTHON 159 ms
logolica99
0
2
reverse vowels of a string
345
0.498
Easy
6,048
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778681/Simple-Two-Pointer-Approach-Using-List
class Solution: def reverseVowels(self, s: str) -> str: left, right = 0, len(s)-1 vowels = {'a','e','i','o','u','A', 'E', 'I', 'O', 'U'} s = list(s) while left < right: # get vowel from left while left < right and s[left] not in vowels: left += 1 # print('[left] found vowel: ', s[left]) # get vowel from right while left < right and s[right] not in vowels: right -= 1 # print('[right] found vowel: ', s[right]) if left < right: s[left], s[right] = s[right], s[left] left += 1 right -= 1 return ''.join(s)
reverse-vowels-of-a-string
πŸ“Œ Simple Two Pointer Approach Using List
pruthvi_hingu
0
4
reverse vowels of a string
345
0.498
Easy
6,049
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778641/Easy-Python-Approach
class Solution: def reverseVowels(self, s: str) -> str: vo = "aeiouAEIOU" sv = [] for i in s: if i in vo: sv.append(i) sv = sv[::-1] zi = 0 ns = "" for i in range(0,len(s)): if s[i] in vo: ns += sv[zi] zi+= 1 else: ns+=s[i] return ns
reverse-vowels-of-a-string
Easy Python Approach
Shagun_Mittal
0
2
reverse vowels of a string
345
0.498
Easy
6,050
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778477/Python-3-Two-Index-Solution%3A-O(n)
class Solution: def reverseVowels(self, s: str) -> str: result = [c for c in s] left = 0 right = len(s) - 1 vowels = set([v for v in "aeiouAEIOU"]) while left < right: if result[left] not in vowels: left += 1 continue if result[right] not in vowels: right -= 1 continue tmp = result[left] result[left] = result[right] result[right] = tmp left += 1 right -= 1 return ''.join(result)
reverse-vowels-of-a-string
Python 3 Two-Index Solution: O(n)
theleastinterestingman
0
2
reverse vowels of a string
345
0.498
Easy
6,051
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778364/Python-(Faster-than-greater90)-or-Two-pointers-or-Stack
class Solution: def reverseVowels(self, s: str) -> str: vowels = 'aeiouAEIOU' string = list(s) l, r = 0, len(s) - 1 while l < r: while l < len(s) and string[l] not in vowels: l += 1 while r >= 0 and string[r] not in vowels: r -= 1 if l < r: string[l], string[r] = string[r], string[l] l += 1 r -= 1 return ''.join(string)
reverse-vowels-of-a-string
Python (Faster than >90%) | Two-pointers | Stack
KevinJM17
0
3
reverse vowels of a string
345
0.498
Easy
6,052
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778364/Python-(Faster-than-greater90)-or-Two-pointers-or-Stack
class Solution: def reverseVowels(self, s: str) -> str: vowels = 'aeiouAEIOU' string = list(s) stack = [] for c in s: if c in vowels: stack.append(c) for i in range(len(string)): if string[i] in vowels: string[i] = stack.pop() return ''.join(string)
reverse-vowels-of-a-string
Python (Faster than >90%) | Two-pointers | Stack
KevinJM17
0
3
reverse vowels of a string
345
0.498
Easy
6,053
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778328/python3-2-line-solution
class Solution: def reverseVowels(self, s: str, vwls = list("aeiouAEIOU")) -> str: stack = [v for v in s if v in vwls] return "".join(c if c not in vwls else stack.pop() for c in s)
reverse-vowels-of-a-string
python3 2 line solution
avs-abhishek123
0
6
reverse vowels of a string
345
0.498
Easy
6,054
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778315/Simple-stack-solution-or-Python3
class Solution: def reverseVowels(self, s: str) -> str: vowels = set('aeiouAEIOU') s = list(s) indices = [] stack = [] for i in range(len(s)): if s[i] in vowels: indices.append(i) stack.append(s[i]) for index in indices: s[index] = stack.pop() return ''.join(s)
reverse-vowels-of-a-string
Simple stack solution | Python3
saamenerve
0
5
reverse vowels of a string
345
0.498
Easy
6,055
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778302/easy-thinking
class Solution: def reverseVowels(self, s: str) -> str: l, r = 0, len(s)-1 vowel = set(('a', 'e', 'i', 'o', 'u')) s = list(s) while l <= r: if s[r].lower() in vowel: if s[l].lower() in vowel: s[l], s[r] = s[r], s[l] l +=1 r-=1 else: l+=1 elif s[l].lower() in vowel: if s[r].lower() in vowel: s[l], s[r] = s[r], s[l] l +=1 r-=1 else: r-=1 else: r-=1 l+=1 return "".join(s)
reverse-vowels-of-a-string
easy thinking
fsubhani
0
3
reverse vowels of a string
345
0.498
Easy
6,056
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778268/Clean-solution-or-Easy-understanding
class Solution: def reverseVowels(self, s: str) -> str: volwels={'a','e','i','o','u','A','E','I','O','U'} temp=[] res="" for i in range(len(s)): if s[i] in volwels: temp.append(s[i]) for i in range(len(s)): if s[i] in volwels: res+=temp.pop() else: res+=s[i] return res
reverse-vowels-of-a-string
Clean solution | Easy understanding
sundram_somnath
0
4
reverse vowels of a string
345
0.498
Easy
6,057
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778200/Python3-Simple-Solution
class Solution: def reverseVowels(self, s: str) -> str: vowels = "aeiouAEIOU" vowel_letters = [l for l in s if l in vowels] new_word = "" idx = -1 for l in s: if l in vowels: new_word += vowel_letters[idx] idx += -1 else: new_word += l return new_word
reverse-vowels-of-a-string
Python3 Simple Solution
Nadhir_Hasan
0
1
reverse vowels of a string
345
0.498
Easy
6,058
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778159/LeetCode-345-%3A-Reverse-Vowels-of-a-String
class Solution: def reverseVowels(self, s: str) -> str: s=list(s) vowel=set(list("aeiouAEIOU")) left=0 right=len(s)-1 while left<right: while left<right and s[left] not in vowel: left+=1 while left<right and s[right] not in vowel: right-=1 s[left],s[right]=s[right],s[left] left+=1 right-=1 return ''.join(s)
reverse-vowels-of-a-string
LeetCode 345 : Reverse Vowels of a String
Nakshu_Python
0
7
reverse vowels of a string
345
0.498
Easy
6,059
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778120/Python-oror-Two-Pointers-oror-HashSet
class Solution: def reverseVowels(self, s: str) -> str: arr = [v for v in s] vowels = set(["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]) l = 0 r = len(arr) - 1 while l < r: if arr[l] in vowels and arr[r] in vowels: arr[l], arr[r] = arr[r], arr[l] l += 1 r -= 1 elif arr[l] in vowels and arr[r] not in vowels: r -= 1 elif arr[l] not in vowels and arr[r] in vowels: l += 1 else: l += 1 r -= 1 return ''.join(arr)
reverse-vowels-of-a-string
Python || Two Pointers || HashSet
Vamsi995
0
2
reverse vowels of a string
345
0.498
Easy
6,060
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778115/Simple-Solution
class Solution: def reverseVowels(self, s: str) -> str: vow=['a','e','i','o','u','A','E','I','O','U'] l=list(s) l1=[] i1=[] for i in range(len(s)): if l[i] in vow: l1.append(s[i]) i1.append(i) l1=l1[::-1] lr=l for i in range(len(i1)): lr[i1[i]]=l1[i] s1='' for i in lr: s1=s1+i return s1
reverse-vowels-of-a-string
Simple Solution
SnehaGanesh
0
2
reverse vowels of a string
345
0.498
Easy
6,061
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778067/Python%3A-Single-Pass-Two-Pointers
class Solution: def reverseVowels(self, s: str) -> str: # One time set conversion of string for o(1) look-ups vowels = set('aeiouAEIOU') # Conversion of string to list for ability to assign s = list(s) # Our two pointers left, right = 0, len(s)-1 # We exit once the two pointers are at the same point while left < right: if s[left] in vowels and s[right] in vowels: s[left], s[right] = s[right], s[left] left += 1 right -= 1 elif s[left] in vowels: right -= 1 continue else: left += 1 continue return ''.join(s)
reverse-vowels-of-a-string
Python: Single-Pass, Two-Pointers
jessewalker2010
0
3
reverse vowels of a string
345
0.498
Easy
6,062
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778059/Python
class Solution: def reverseVowels(self, s: str) -> str: vow = {"a","e","i","o","u","A","E","I","O","U"} arr=[i for i in s] lptr = 0 rptr = len(arr)-1 while lptr<=rptr: while arr[lptr] not in vow: lptr+=1 if lptr>rptr or lptr>len(arr): return "".join(arr) while arr[rptr] not in vow: rptr-=1 if lptr>rptr or rptr<0 : return "".join(arr) temp = arr[lptr] arr[lptr]=arr[rptr] arr[rptr]=temp lptr+=1 rptr-=1 return "".join(arr)
reverse-vowels-of-a-string
Python
shzaheer514
0
2
reverse vowels of a string
345
0.498
Easy
6,063
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2778015/Very-Bad-But-Easy-to-Understand
class Solution: def reverseVowels(self, s: str) -> str: found = "" vowels = "EOAUIeaiou" for i in s : if i in vowels : found += i s = s.replace(i, "*") found = found[::-1] j = 0 for i in s : if i == "*" : s = s.replace(i, found[j] , 1) j+=1 return s
reverse-vowels-of-a-string
Very Bad But Easy to Understand
nedaladham8
0
4
reverse vowels of a string
345
0.498
Easy
6,064
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2777946/Slow-but-simple-Python-O(2n)
class Solution: def reverseVowels(self, s: str) -> str: _vowels = ["a","e","i","o","u"] _v = [] for i in range(len(s)): if s[i].lower() in _vowels: _v.append(s[i]) s = s[:i]+"*"+s[i+1:] _v = _v[::-1] for i in range(len(s)): if s[i] == "*": s= s[:i] + _v[0] + s[i+1:] _v = _v[1:] return s
reverse-vowels-of-a-string
Slow but simple Python O(2n)
ATHBuys
0
2
reverse vowels of a string
345
0.498
Easy
6,065
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2777931/Py-Best-solution-Easy-Approch
class Solution: def reverseVowels(self, s: str) -> str: s=list(s) i=0 j=len(s)-1 v=["a","e","i","o","u","A","E","I","O","U"] while(i<j): if s[i] in v and s[j] in v: s[i],s[j]=s[j],s[i] i+=1 j-=1 if s[i] not in v: i+=1 if s[j] not in v: j-=1 return "".join(s)
reverse-vowels-of-a-string
Py Best solution Easy Approch πŸ’―βœ…βœ…
adarshg04
0
4
reverse vowels of a string
345
0.498
Easy
6,066
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2777758/Two-pointers-solution
class Solution: def reverseVowels(self, s: str) -> str: left = 0 right = len(s)-1 vowels = set(['a','e','i','o','u', 'A','E','I','O','U']) s = list(s) while left<right: lv = s[left] in vowels rv = s[right] in vowels if (lv and rv): s[left], s[right] = s[right], s[left] left+=1 right-=1 else: left+=int(not lv) right-=int(not rv) return "".join(s)
reverse-vowels-of-a-string
Two pointers solution
user9698Ra
0
12
reverse vowels of a string
345
0.498
Easy
6,067
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2777669/Python3-Solution-with-using-two-pointers
class Solution: def reverseVowels(self, s: str) -> str: it1, it2 = 0, len(s) - 1 vowels = set(['a', 'A', 'e', 'E', 'I', 'i', 'o', 'O', 'u', 'U']) s = list(s) while it1 < it2: while it1 < it2 and s[it1] not in vowels: it1 += 1 while it1 < it2 and s[it2] not in vowels: it2 -= 1 s[it1], s[it2] = s[it2], s[it1] it1 += 1 it2 -= 1 return ''.join(s)
reverse-vowels-of-a-string
[Python3] Solution with using two-pointers
maosipov11
0
3
reverse vowels of a string
345
0.498
Easy
6,068
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2777640/Two-pointers-approach
class Solution: def reverseVowels(self, s: str) -> str: v = ['a', 'e' , 'i' , 'o' , 'u' , 'A' , 'E' , 'I' , 'O' , 'U'] i = 0 j = len(s) - 1 while (i < j) : while (i < j) : if s[i] in v : break else : i += 1 while (i < j) : if s[j] in v : break else : j -= 1 if (i < j) : s = s[:i]+s[j]+s[i+1:j]+s[i] + s[j+1:] i += 1 j -= 1 else : break return s
reverse-vowels-of-a-string
Two pointers approach
bhavithas153
0
2
reverse vowels of a string
345
0.498
Easy
6,069
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2777587/fast
class Solution: def reverseVowels(self, s: str) -> str: yuan = 'aeiouAEIOU' s = list(s) s_yuan = [] index_list=[] index = 0 for i in s: if i in yuan: s_yuan.append(i) index_list.append(index) index += 1 s_yuan.reverse() index = 0 for c in s_yuan: s[index_list[index]]=c index+=1 return ''.join(s)
reverse-vowels-of-a-string
fast
hlwin
0
2
reverse vowels of a string
345
0.498
Easy
6,070
https://leetcode.com/problems/reverse-vowels-of-a-string/discuss/2777449/simple-python-solution-Beats-86.15-Memory-14.9-MB-Beats-89.63
class Solution: def reverseVowels(self, s: str) -> str: s = list(s) vowel = list('AEIOU') left_counter, right_counter = 0, len(s) - 1 l_val, r_val = None, None while left_counter<right_counter: if s[left_counter].upper() in vowel: l_val = left_counter elif l_val is None: left_counter += 1 if s[right_counter].upper() in vowel: r_val = right_counter elif r_val is None: right_counter -= 1 if l_val is not None and r_val is not None: s[l_val], s[r_val] = s[r_val], s[l_val] l_val = r_val = None left_counter +=1 right_counter -=1 if left_counter > right_counter: break return "".join(s)
reverse-vowels-of-a-string
simple python solution Beats 86.15% Memory 14.9 MB Beats 89.63%
lakhannawalani911
0
1
reverse vowels of a string
345
0.498
Easy
6,071
https://leetcode.com/problems/top-k-frequent-elements/discuss/1928198/Python-Simple-Python-Solution-Using-Dictionary-(-HashMap-)
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: frequency = {} for num in nums: if num not in frequency: frequency[num] = 1 else: frequency[num] = frequency[num] + 1 frequency = dict(sorted(frequency.items(), key=lambda x: x[1], reverse=True)) result = list(frequency.keys())[:k] return result
top-k-frequent-elements
[ Python ] βœ…βœ… Simple Python Solution Using Dictionary ( HashMap ) βœŒπŸ‘
ASHOK_KUMAR_MEGHVANSHI
24
2,800
top k frequent elements
347
0.648
Medium
6,072
https://leetcode.com/problems/top-k-frequent-elements/discuss/1927383/Python-one-liner-beats-98
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: return [x[0] for x in Counter(nums).most_common(k)]
top-k-frequent-elements
Python one-liner beats 98%
eduardocereto
13
2,100
top k frequent elements
347
0.648
Medium
6,073
https://leetcode.com/problems/top-k-frequent-elements/discuss/1705495/Python-or-4-Ways-of-doing-same-simple-thing
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: freq_table = Counter(nums) ans_table = freq_table.most_common() ans = [] for key, _ in ans_table: if k <= 0: break k -= 1 ans.append(key) return ans
top-k-frequent-elements
Python | 4 Ways of doing same simple thing
anCoderr
11
939
top k frequent elements
347
0.648
Medium
6,074
https://leetcode.com/problems/top-k-frequent-elements/discuss/1705495/Python-or-4-Ways-of-doing-same-simple-thing
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: freq_table = Counter(nums) heap = [] for i in freq_table.keys(): heappush(heap, (freq_table[i], i)) freq_table = nlargest(k,heap) ans = [] for i, j in freq_table: ans.append(j) return ans
top-k-frequent-elements
Python | 4 Ways of doing same simple thing
anCoderr
11
939
top k frequent elements
347
0.648
Medium
6,075
https://leetcode.com/problems/top-k-frequent-elements/discuss/1705495/Python-or-4-Ways-of-doing-same-simple-thing
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: freq_table = Counter(nums) heap = [] for i in freq_table.keys(): heappush(heap, (-freq_table[i], i)) ans = [] while k > 0: k -= 1 ans.append(heappop(heap)[1]) return ans
top-k-frequent-elements
Python | 4 Ways of doing same simple thing
anCoderr
11
939
top k frequent elements
347
0.648
Medium
6,076
https://leetcode.com/problems/top-k-frequent-elements/discuss/1705495/Python-or-4-Ways-of-doing-same-simple-thing
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: freq_table = {} for i in nums: freq_table[i] = freq_table.get(i, 0) + 1 heap = [] for i in freq_table.keys(): if len(heap) == k: # If size is k then we dont want to increase the size further heappushpop(heap, (freq_table[i], i)) else: # Size is not k then freely push values heappush(heap, (freq_table[i], i)) # After this operation the heap contains only k largest values of all the values in nums ans = [] while k > 0: k -= 1 ans.append(heappop(heap)[1]) return ans
top-k-frequent-elements
Python | 4 Ways of doing same simple thing
anCoderr
11
939
top k frequent elements
347
0.648
Medium
6,077
https://leetcode.com/problems/top-k-frequent-elements/discuss/1507137/96-faster-oror-All-three-Approaches-oror-Well-Coded
class Solution: def topKFrequent(self, nums, k): bucket = [[] for _ in range(len(nums) + 1)] Count = Counter(nums) for num, freq in Count.items(): bucket[freq].append(num) flat_list = list(chain(*bucket)) return flat_list[::-1][:k]
top-k-frequent-elements
πŸ“ŒπŸ“Œ 96% faster || All-three Approaches || Well-Coded 🐍
abhi9Rai
5
699
top k frequent elements
347
0.648
Medium
6,078
https://leetcode.com/problems/top-k-frequent-elements/discuss/1507137/96-faster-oror-All-three-Approaches-oror-Well-Coded
class Solution: def topKFrequent(self, nums: List[int], size: int) -> List[int]: dic = Counter(nums) heap = [] heapq.heapify(heap) for k in dic: if len(heap)<size: heapq.heappush(heap,[dic[k],k]) elif dic[k]>heap[0][0]: heapq.heappop(heap) heapq.heappush(heap,[dic[k],k]) res = [] while heap: res.append(heapq.heappop(heap)[1]) return res
top-k-frequent-elements
πŸ“ŒπŸ“Œ 96% faster || All-three Approaches || Well-Coded 🐍
abhi9Rai
5
699
top k frequent elements
347
0.648
Medium
6,079
https://leetcode.com/problems/top-k-frequent-elements/discuss/1507137/96-faster-oror-All-three-Approaches-oror-Well-Coded
class Solution: def topKFrequent(self, nums: List[int], size: int) -> List[int]: dic = Counter(nums) res = [] dic = sorted(dic.items(),key = lambda x:x[1],reverse = True) for k in dic: res.append(k[0]) size-=1 if size<=0: return res return res
top-k-frequent-elements
πŸ“ŒπŸ“Œ 96% faster || All-three Approaches || Well-Coded 🐍
abhi9Rai
5
699
top k frequent elements
347
0.648
Medium
6,080
https://leetcode.com/problems/top-k-frequent-elements/discuss/2133564/Python3-O(n)-Time-Optimized-Solution-Easy-to-understand
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: # nums = [1,1,1,2,2,3]; k = 2 numCountDict = {} # key = num; value = count for num in nums: if num not in numCountDict: numCountDict[num] = 1 else: numCountDict[num] += 1 # numCountDict = {1:3, 2:2, 3:1} maxCount = max(numCountDict.values()) # 3 countNumDict = {i:[] for i in range(1, maxCount+1)} # key = count; value = [num,...] for num in numCountDict: countNumDict[numCountDict[num]].append(num) # countNumDict = [3:[1], 2:[2], 1:[3]] res = [] for count in range(maxCount, 0, -1): # count = 3, 2, 1 if len(res) >= k: break if len(countNumDict[count]) > 0: res += countNumDict[count] return res # Time: O(N) # SPace: O(N)
top-k-frequent-elements
[Python3] O(n) Time Optimized Solution Easy to understand
samirpaul1
4
375
top k frequent elements
347
0.648
Medium
6,081
https://leetcode.com/problems/top-k-frequent-elements/discuss/1180163/Simple-Python-O(n)-Solution-Heaps-(Beats-99.97)
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: d = dict(collections.Counter(nums)) heap = [] for key, val in d.items(): if len(heap) == k: heapq.heappushpop(heap, (val,key)) else: heapq.heappush(heap, (val,key)) return [y for x,y in heap]
top-k-frequent-elements
Simple Python O(n) Solution - Heaps (Beats 99.97%)
zealorez
4
545
top k frequent elements
347
0.648
Medium
6,082
https://leetcode.com/problems/top-k-frequent-elements/discuss/2416406/2-Python-solutions
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: stats = {} for i in nums: if i not in stats: stats[i] = 1 else: stats[i] += 1 buckets = [[] for i in range(len(nums)+1)] for i, j in stats.items(): buckets[j].append(i) buckets = [j for i in buckets for j in i] return buckets[::-1][:k]
top-k-frequent-elements
πŸ“Œ 2 Python solutions
croatoan
2
85
top k frequent elements
347
0.648
Medium
6,083
https://leetcode.com/problems/top-k-frequent-elements/discuss/2416406/2-Python-solutions
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: return [_[0] for _ in collections.Counter(nums).most_common(k)]
top-k-frequent-elements
πŸ“Œ 2 Python solutions
croatoan
2
85
top k frequent elements
347
0.648
Medium
6,084
https://leetcode.com/problems/top-k-frequent-elements/discuss/2310619/Two-Python-Solutions-or-Heap-or-Bucket-Sort
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: num_dict = {} for num in nums: if num not in num_dict: num_dict[num] = 1 else: num_dict[num] += 1 heap = [] for key,val in num_dict.items(): heap.append((-val, key)) heapq.heapify(heap) res = [] for _ in range(k): _,ans = heapq.heappop(heap) res.append(ans) return res
top-k-frequent-elements
Two Python Solutions | Heap | Bucket Sort
PythonicLava
2
285
top k frequent elements
347
0.648
Medium
6,085
https://leetcode.com/problems/top-k-frequent-elements/discuss/2310619/Two-Python-Solutions-or-Heap-or-Bucket-Sort
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: buckets = [[] for _ in range(len(nums)+1)] num_dict = {} for num in nums: if num not in num_dict: num_dict[num] = 1 else: num_dict[num] += 1 for key,value in num_dict.items(): buckets[value].append(key) res = [] for i in range(len(buckets)-1, 0, -1): for val in buckets[i]: res.append(val) if len(res) == k: return res
top-k-frequent-elements
Two Python Solutions | Heap | Bucket Sort
PythonicLava
2
285
top k frequent elements
347
0.648
Medium
6,086
https://leetcode.com/problems/top-k-frequent-elements/discuss/740463/PythonPython3-Top-K-Frequent-Elements
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: return [x for x,_ in sorted(Counter(nums).items(), key=lambda x:-x[1])[:k]]
top-k-frequent-elements
[Python/Python3] Top K Frequent Elements
newborncoder
2
909
top k frequent elements
347
0.648
Medium
6,087
https://leetcode.com/problems/top-k-frequent-elements/discuss/2302645/Hashmap-easy-python
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: dict_count = {} for i in nums: if i in dict_count: dict_count[i]+=1 else: dict_count[i]=1 rev_count = {} for i in dict_count: if dict_count[i] in rev_count: rev_count[dict_count[i]].append(i) else: rev_count[dict_count[i]] = [i] ans = [] for c in range(len(nums),-1,-1): if c in rev_count: ans.extend(rev_count[c]) if len(ans)>=k: return ans[:k] return ans[:k]
top-k-frequent-elements
Hashmap easy python
sunakshi132
1
87
top k frequent elements
347
0.648
Medium
6,088
https://leetcode.com/problems/top-k-frequent-elements/discuss/2243515/Python-Solution
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: helper=collections.Counter(nums) elements = helper.most_common(k) result = [] for tup in elements: result.append(tup[0]) return result
top-k-frequent-elements
Python Solution
rtyagi1
1
86
top k frequent elements
347
0.648
Medium
6,089
https://leetcode.com/problems/top-k-frequent-elements/discuss/2081265/Python3-or-Heap-%2B-Counter-or-Easy-Understand
class Solution def topKFrequent(self, nums: List[int], k: int) -> List[int]: count = Counter(nums) heap = [[-cnt, value] for value, cnt in count.items()] heapify(heap) result = [] for _ in range(k): _, num = heappop(heap) result.append(num) return result
top-k-frequent-elements
Python3 | Heap + Counter | Easy Understand
itachieve
1
58
top k frequent elements
347
0.648
Medium
6,090
https://leetcode.com/problems/top-k-frequent-elements/discuss/1947831/Optimal-O(n)-Time-Solution
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: counts = defaultdict(int) freq = defaultdict(list) max_count = 0 result = [] for num in nums: counts[num] += 1 max_count = max(max_count, counts[num]) for key in counts: freq[counts[key]].append(key) while k > 0: values = freq[max_count] result += values[:k] k -= len(values) max_count -= 1 return result
top-k-frequent-elements
Optimal O(n) Time Solution
EdwinJagger
1
86
top k frequent elements
347
0.648
Medium
6,091
https://leetcode.com/problems/top-k-frequent-elements/discuss/1929249/Python-or-Buckets-or-Beats-96
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: count = Counter(nums) max_fq, min_fq = float('-inf'), float('inf') for x in count: max_fq = max(max_fq, count[x]) min_fq = min(min_fq, count[x]) listMap , ans = defaultdict(list), [] for x in count.items(): listMap[x[1]].append(x[0]) for i in range(max_fq, min_fq-1, -1): itemList = listMap.get(i) if not itemList: continue for item in itemList: ans.append(item) if len(ans) == k: return ans return ans
top-k-frequent-elements
Python | Buckets | Beats 96%
prajyotgurav
1
73
top k frequent elements
347
0.648
Medium
6,092
https://leetcode.com/problems/top-k-frequent-elements/discuss/1929004/Single-line-python-solution
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: return list(dict(sorted(collections.Counter(nums).items(),key=lambda x:x[1],reverse=True)).keys())[:k]
top-k-frequent-elements
Single line python solution
amannarayansingh10
1
47
top k frequent elements
347
0.648
Medium
6,093
https://leetcode.com/problems/top-k-frequent-elements/discuss/1928908/Python-solution-Explained-with-diagram-or-hashmap-or-Easy-understanding
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: dict={} freq=[[] for i in range(len(nums)+1)] for num in nums: dict[num]= 1+dict.get(num,0) for value,key in dict.items(): freq[key].append(value) res =[] for i in range(len(freq)-1,0,-1): for value in freq[i]: res.append(value) if len(res)==k: return(res)
top-k-frequent-elements
Python solution Explained with diagram | hashmap | Easy understanding
poojatripathi0_0
1
18
top k frequent elements
347
0.648
Medium
6,094
https://leetcode.com/problems/top-k-frequent-elements/discuss/1928411/Python3-Solution-with-using-heap
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: c = collections.Counter(nums) heap = [] for key in c: heapq.heappush(heap, (c[key], key)) if len(heap) > k: heapq.heappop(heap) return [v[1] for v in heap]
top-k-frequent-elements
[Python3] Solution with using heap
maosipov11
1
85
top k frequent elements
347
0.648
Medium
6,095
https://leetcode.com/problems/top-k-frequent-elements/discuss/1928325/Python-Solution-or-One-Liner-or-collections.Counter
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: return [element[0] for element in Counter(nums).most_common(k)]
top-k-frequent-elements
Python Solution | One Liner | collections.Counter
Gautam_ProMax
1
73
top k frequent elements
347
0.648
Medium
6,096
https://leetcode.com/problems/top-k-frequent-elements/discuss/1891992/Python-3-or-Counter-or-Heap-or-Priority-Queue-or-Easy-to-Understand
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: c, temp, ans = collections.Counter(nums), [], [] for i, j in zip(c.keys(), c.values()): temp.append((j, i)) heapq._heapify_max(temp) for i in range(k): t = heapq._heappop_max(temp) ans.append(t[1]) return ans
top-k-frequent-elements
Python 3 | Counter | Heap | Priority Queue | Easy to Understand
milannzz
1
68
top k frequent elements
347
0.648
Medium
6,097
https://leetcode.com/problems/top-k-frequent-elements/discuss/1708324/3-liner-in-Python-with-explanation
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: # (number, count) # e.g. [(1,3), (2,2), (3,1)] c = list(Counter(nums).items()) # sort by the 2nd element in each tuple c.sort(key=lambda x: x[1], reverse=True) # return the top k return [c.pop(0)[0] for _ in range(k)]
top-k-frequent-elements
3 liner in Python with explanation
tokamaka3
1
107
top k frequent elements
347
0.648
Medium
6,098
https://leetcode.com/problems/top-k-frequent-elements/discuss/1499963/Quickselect-beats-99.86-vs-minheap-beats-90.27
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: def partit(nums, l, r)->int: ''' partition and return pivot index. After execution, nums is partitial ordered, and pivot and final position. Based on JeffE's Algorithms.wtf book. ''' pi = randint(l,r) pivot = nums[pi] nums[pi], nums[r] = nums[r], nums[pi] s = l-1 # [0..s] < pivot for f in range(l,r): if nums[f][1] < pivot[1]: # XXX: only change from default lomuto partition template s += 1 nums[s], nums[f] = nums[f], nums[s] # put pivot in correct place: s+1 s += 1 nums[s], nums[r] = nums[r], nums[s] return s def qselect(nums, l, r, k)->int: ''' Based on https://en.wikipedia.org/wiki/Quickselect ''' # base if l == r: return nums[l] pi = partit(nums, l, r) if pi == k: return nums[pi] elif pi > k: return qselect(nums, l, pi-1, k) else: return qselect(nums, pi+1, r, k) ctr = Counter(nums) n2f = list(ctr.items()) qselect(n2f, 0, len(n2f)-1, len(n2f)-k) res = [] for i in range(len(n2f)-1, len(n2f)-k-1, -1): res.append(n2f[i][0]) return res
top-k-frequent-elements
[η­†θ¨˜] Quickselect beats 99.86%, vs minheap beats 90.27%
hzhang413
1
80
top k frequent elements
347
0.648
Medium
6,099