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
r... | 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
fo... | 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]
... | 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
... | 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 ... | 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
... | 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] ... | 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
... | 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 :
... | 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_l... | 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
... | 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... | 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 += ... | 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... | 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()
... | 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
... | 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 fr... | 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... | 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... | 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 ... | 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] =... | 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... | 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],... | 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)... | 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 ... | 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... | 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:
... | 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
... | 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_... | 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:
... | 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]
... | 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:
let... | 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]
... | 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 +... | 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]
... | 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
... | 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 -... | 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] = stac... | 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 i... | 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]
... | 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 v... | 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... | 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:
... | 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]... | 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
... | 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 ... | 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... | 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 == "*" :
... | 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)):
... | 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] n... | 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):
... | 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
whi... | 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
... | 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... | 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_coun... | 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(f... | 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 a... | 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... | 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])
... | 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... | 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 ... | 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
# numCount... | 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... | 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.it... | 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 ... | 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
... | 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_coun... | 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)
... | 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])
... | 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), [... | 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)
... | 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)
... | 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)
... | 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
ret... | 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,... | 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 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.