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/greatest-english-letter-in-upper-and-lower-case/discuss/2526027/Python-1-liner
|
class Solution:
def greatestLetter(self, s: str) -> str:
return max(Counter(s) & Counter(s.swapcase()), default="").upper()
|
greatest-english-letter-in-upper-and-lower-case
|
Python 1-liner
|
russellizadi
| 1
| 63
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,800
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2320798/easy-python-solution
|
class Solution:
def greatestLetter(self, s: str) -> str:
letter_list, small_letter, capital_letter = [], [], []
for i in range(len(s)) :
if ord(s[i]) <= 90 :
if s[i] not in capital_letter :
capital_letter.append(s[i])
letter_list.append(s[i].lower())
else :
if s[i] not in small_letter :
small_letter.append(s[i])
letter_list.append(s[i])
max_letter, max_ord = "", 0
for letter in letter_list :
if letter_list.count(letter) >= 2 :
if ord(letter) > max_ord :
max_ord = ord(letter)
max_letter = letter.upper()
return max_letter
|
greatest-english-letter-in-upper-and-lower-case
|
easy python solution
|
sghorai
| 1
| 55
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,801
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2303661/Simple-Python3-Solution
|
class Solution:
def greatestLetter(self, s: str) -> str:
chars = set(s)
l = []
for i in chars:
if i.upper() in chars and i.lower() in chars:
l.append(i)
if l:
l.sort()
return l[-1].upper()
else:
return ""
|
greatest-english-letter-in-upper-and-lower-case
|
Simple Python3 Solution
|
vem5688
| 1
| 62
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,802
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2201214/Python-fast-and-simple
|
class Solution:
def greatestLetter(self, s: str) -> str:
from string import ascii_lowercase as al, ascii_uppercase as au
for i in range(len(al)-1,-1,-1):
if al[i] in s and au[i] in s:
return au[i]
else:
return ''
|
greatest-english-letter-in-upper-and-lower-case
|
Python fast and simple
|
StikS32
| 1
| 63
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,803
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2168120/Easy-Python-Solution
|
class Solution:
def greatestLetter(self, s: str) -> str:
if not any(char.islower() for char in s):
return ""
if not any(char.isupper() for char in s):
return ""
res = ''
for char in s:
if char.lower() in s and char.upper() in s:
if char.isupper():
res += char
res = "".join(sorted(res))
if not res:
return ""
else:
return res[-1]
|
greatest-english-letter-in-upper-and-lower-case
|
Easy Python Solution
|
MiKueen
| 1
| 132
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,804
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2847076/PYTHON-Simple-ascii-solution-oror-less-memory-usage-than-99.8-submissions
|
class Solution:
def greatestLetter(self, s: str) -> str:
both=[]
for i in range(len(s)):
if chr(ord(s[i])+32) in s:
both.append(s[i])
if len(both)>0:
return (max(both))
else:
return ""
|
greatest-english-letter-in-upper-and-lower-case
|
PYTHON - Simple ascii solution || less memory usage than 99.8% submissions
|
envyTheClouds
| 0
| 1
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,805
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2842683/Python-Solution
|
class Solution:
def greatestLetter(self, s: str) -> str:
res=""
s=set(s)
s="".join(s)
for i in range(len(s)):
for j in range(len(s)):
if(i!=j and (s[i]==s[j].lower() or s[i]==s[j].upper())):
if(res=="" or res<s[i]):
res=s[i]
return(res.upper())
|
greatest-english-letter-in-upper-and-lower-case
|
Python Solution
|
CEOSRICHARAN
| 0
| 1
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,806
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2820678/Simple-python-solution
|
class Solution:
def greatestLetter(self, s: str) -> str:
s = ''.join(sorted(s))
res = ''
for i in s:
if i.lower() in s and i.upper() in s:
res = i.upper()
return res
|
greatest-english-letter-in-upper-and-lower-case
|
Simple python solution
|
aruj900
| 0
| 1
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,807
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2818329/Python-easy-solution-hashset
|
class Solution:
def greatestLetter(self, s: str) -> str:
sSet = set(s)
max_letter = ''
for c in sSet:
if c.isupper() and c.swapcase() in sSet:
if max_letter < c:
max_letter = c
return max_letter
|
greatest-english-letter-in-upper-and-lower-case
|
Python easy solution hashset
|
Yauhenish
| 0
| 1
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,808
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2816207/Greatest-English-Letter-in-Upper-and-Lower-Case(Solution)
|
class Solution:
def greatestLetter(self, s: str) -> str:
res = []
for i in s :
if i>='A' and i<='Z' :
if chr(ord(i)+32) in s : res.append(i)
elif i>='a' and i<='z' :
if chr(ord(i)-32) in s : res.append(chr(ord(i)-32))
res.sort()
if len(res)==0 : return ""
return res[len(res)-1]
|
greatest-english-letter-in-upper-and-lower-case
|
Greatest English Letter in Upper and Lower Case(Solution)
|
NIKHILTEJA
| 0
| 2
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,809
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2813802/Python-easy-solution-using-hashmap-O(1)
|
class Solution:
def greatestLetter(self, s: str) -> str:
map = {}
a = []
for i in s:
if i not in map:
map[i] = 1
else:
map[i]+=1
for i in map:
if chr(ord(i)+32) in map:
a.append(i)
return max(a) if a else ""
|
greatest-english-letter-in-upper-and-lower-case
|
Python easy solution using hashmap O(1)
|
Harish2004
| 0
| 1
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,810
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2796148/Python3-solution-Runtime-45-ms-Beats-89.55-Memory-13.9-MB-Beats-71.36
|
class Solution:
def greatestLetter(self, s: str) -> str:
chars = "abcdefghijklmnopqrstuvwxyz"
result = ""
for ch in s:
if ch.islower() and ch.upper() in s:
if (result == ""):
result = ch
elif chars.index(result) < chars.index(ch):
result = ch
return result.upper()
|
greatest-english-letter-in-upper-and-lower-case
|
Python3 solution Runtime 45 ms Beats 89.55% Memory 13.9 MB Beats 71.36%
|
SupriyaArali
| 0
| 2
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,811
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2778592/python
|
class Solution:
def greatestLetter(self, s: str) -> str:
r=[]
for i in s:
if i.upper() in s and i.lower() in s:
r+=[i.upper()]
if len(r) == 0:
return ''
return max(r)
|
greatest-english-letter-in-upper-and-lower-case
|
python
|
sarvajnya_18
| 0
| 2
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,812
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2766812/Python.Easy.2309
|
class Solution:
def greatestLetter(self, r: str) -> str:
st ="abcdefghijklmnopqrstuvwxyz"
ans = []
for i in st:
if i in r and i.upper() in r:
ans.append(i.upper())
if len(ans) ==0:
return ""
ans.sort()
return ans[-1]
|
greatest-english-letter-in-upper-and-lower-case
|
🍏Python.Easy.2309
|
ilyenko
| 0
| 1
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,813
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2736545/Fast-and-Easy-Solution-using-Dictionary
|
class Solution:
def greatestLetter(self, s: str) -> str:
result = ""
d = {chr(i+64):chr(i+96) for i in range(1,27)}
for letter in s:
if ord(letter) < 91:
if d[letter] in s:
result = max(result, letter)
return result
|
greatest-english-letter-in-upper-and-lower-case
|
Fast and Easy Solution using Dictionary
|
user6770yv
| 0
| 3
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,814
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2722498/Python-3-Hashset-implementation
|
class Solution:
def greatestLetter(self, s: str) -> str:
d = set()
maxletter = ''
for c in s:
if c.islower() and c.upper() in d:
maxletter = max(maxletter, c.upper())
elif c.isupper() and c.lower() in d:
maxletter = max(maxletter, c.upper())
else:
d.add(c)
return maxletter.upper()
|
greatest-english-letter-in-upper-and-lower-case
|
Python 3 Hashset implementation
|
theReal007
| 0
| 1
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,815
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2704164/Clean-and-super-Fast-Python-and-Java
|
class Solution:
def greatestLetter(self, s: str) -> str:
s = set(s)
print(s)
for letter in range(ord('Z'), ord('A') -1, -1): # Iterate from Z to A(right side open so A-1)
if chr(letter) in s and chr(letter+32) in s: # Corresponding lower ord is upper+32
return chr(letter) # The 1st fitting should be returned since reverse iterate
return ''
|
greatest-english-letter-in-upper-and-lower-case
|
Clean and super Fast Python and Java
|
milkwaystar
| 0
| 4
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,816
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2652042/Python3-Scan-Using-Set
|
class Solution:
def greatestLetter(self, s: str) -> str:
u, l, t = string.ascii_uppercase, string.ascii_lowercase, set(s)
for i in reversed(range(26)):
if u[i] in t and l[i] in t:
return u[i]
return ""
|
greatest-english-letter-in-upper-and-lower-case
|
Python3 Scan Using Set
|
godshiva
| 0
| 2
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,817
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2459469/Easy-understandable-python-solution
|
class Solution:
def greatestLetter(self, s: str) -> str:
l = []
upper = re.sub(r'[^A-Z]',r'',s)
for x in upper:
if x.lower() in s:
l.append(x)
if not l: return ""
l.sort()
return l[-1]
|
greatest-english-letter-in-upper-and-lower-case
|
Easy understandable python solution
|
pro6igy
| 0
| 19
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,818
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2450727/34-MS-Faster-than-94-Solutions-oror-EASY
|
class Solution:
def greatestLetter(self, s: str) -> str:
if s.isupper() or s.islower(): return ""
else:
lst=[]
string=list(set(s.upper()))
for i in string:
if i in s and i.lower() in s:
lst.append(i)
lst.sort()
if len(lst)==0: return ""
else: return lst[-1]
|
greatest-english-letter-in-upper-and-lower-case
|
34 MS Faster than 94% Solutions || EASY
|
keertika27
| 0
| 19
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,819
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2417655/python-3-easily-understandable-solution
|
class Solution(object):
def greatestLetter(self, s):
"""
:type s: str
:rtype: str
"""
temp = []
res = ""
counter = 0
for char in s:
if char.isupper():
if char.lower() in s:
temp.append(char)
if len(temp) == 0:
return ""
else:
for item in temp:
if ord(item) > counter:
counter = ord(item)
res = item
return res
|
greatest-english-letter-in-upper-and-lower-case
|
python 3 easily understandable solution
|
Gilbert770
| 0
| 13
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,820
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2367622/Very-easy-hashset-solution-with-explanation
|
class Solution:
def greatestLetter(self, s: str) -> str:
s=set(s) #convert s to hashset for O(1) lookup
max_=""
for i in s:
if i.swapcase() in s: #if i is lowercase, look for uppercase in s and vice versa
max_=max(max_,i.upper()) #keep highest letter that has both uppercase and lowercase presence
return max_
|
greatest-english-letter-in-upper-and-lower-case
|
Very easy hashset solution with explanation
|
sunakshi132
| 0
| 31
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,821
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2361786/Intuitive-approach-O(N)-time-O(1)-space
|
class Solution:
def greatestLetter(self, s: str) -> str:
# iterate the string once
# store the greatest english letter as a variable
# see if the string is upper or lower
# based on it being upper or lower, determine if its opposite is within the string
# if it is set it to the greast english letter variable using max
# Time O(N) space: O(1)
res = 0
for letter in s:
if letter.isupper():
if letter.lower() in s:
res = max(res, ord(letter))
else:
if letter.upper() in s:
res = max(res, ord(letter.upper()))
return chr(res) if res > 0 else ''
|
greatest-english-letter-in-upper-and-lower-case
|
Intuitive approach O(N) time O(1) space
|
andrewnerdimo
| 0
| 48
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,822
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2272041/Easy-Python-Solution-O(n)-or-Simple-and-Efficient-Solution-Beginner-Friendly
|
class Solution:
def greatestLetter(self, s: str) -> str:
maxchar = 'A'
char = ''
res=''
for i in s:
ss = ""
if i.isalpha():
if i.islower():
if i.upper() in s:
indx = s.find(i.upper())
ss = i+s[indx]
char = i.upper()
s = s.replace(i, '%')
s = s.replace(s[indx], '%')
else:
if i.lower() in s:
indx = s.find(i.lower())
ss = i+s[indx]
char = i
s = s.replace(i, '%')
s = s.replace(s[indx], '%')
if maxchar <= char and char != '':
maxchar = char
res = maxchar
elif maxchar >= char and char != '':
res = maxchar
elif char=='':
res = char
return(res)
|
greatest-english-letter-in-upper-and-lower-case
|
Easy Python Solution O(n) | ✅Simple and Efficient Solution [Beginner-Friendly]
|
RatnaPriya
| 0
| 41
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,823
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2231396/Easy-solution-using-Counter-and-sort
|
class Solution:
def greatestLetter(self, s: str) -> str:
counts = collections.Counter(s)
uppers = [c for c in counts.keys() if c.isupper()]
uppers.sort()
greatest = ''
for c in uppers[::-1]:
if c.lower() in counts.keys():
greatest = c
break
return greatest
|
greatest-english-letter-in-upper-and-lower-case
|
Easy solution using Counter & sort
|
casshsu
| 0
| 24
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,824
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2216743/Easy-solution-beginners-friendly
|
class Solution:
def greatestLetter(self, s: str) -> str:
A=[0]*26 #For Upper case
B=[0]*26 #For lower case
for i in s:
if i.isupper():
A[ord(i)-ord('A')]=1
else:
B[ord(i)-ord('a')]=1
for i in range(25,-1,-1):
if A[i] and B[i]:
return chr(i+ord('A'))
return ""
|
greatest-english-letter-in-upper-and-lower-case
|
Easy solution , beginners friendly
|
Aniket_liar07
| 0
| 36
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,825
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2189687/Two-python-solution
|
class Solution:
def greatestLetter(self, s: str) -> str:
set_ = set(s)
return max((ch.upper() for ch in set_ if ch.swapcase() in set_), default ="")
|
greatest-english-letter-in-upper-and-lower-case
|
Two python solution
|
writemeom
| 0
| 35
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,826
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2189687/Two-python-solution
|
class Solution:
def greatestLetter(self, s: str) -> str:
cnt = Counter(s)
return next((u for u in reversed(ascii_uppercase) if cnt[u] and cnt[u.lower()]),"")
|
greatest-english-letter-in-upper-and-lower-case
|
Two python solution
|
writemeom
| 0
| 35
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,827
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2186791/Python-oror-Easy-to-understand
|
class Solution:
def greatestLetter(self, s: str) -> str:
capital=""
small=""
ans=""
#Adding Uppercase and Lowercase char from s in capital and small
for i in s:
if 'a'<=i<='z':
small+=i
else:
capital+=i
#Comparing capital and small string
for j in capital:
if j in small.upper() and (len(ans)==0 or j>ans):
ans=j
return ans
|
greatest-english-letter-in-upper-and-lower-case
|
Python || Easy to understand
|
dhruva3223
| 0
| 46
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,828
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2182240/Python3-simple-solution
|
class Solution:
def greatestLetter(self, s: str) -> str:
d = {}
ans = ''
for i in range(len(s)):
if 97 <= ord(s[i]) <= 122:
x = s[i].upper()
d[x] = d.get(x,[1,0])
d[x][0] = 1
elif 65 <= ord(s[i]) <= 90:
x = s[i].upper()
d[x] = d.get(x,[0,1])
d[x][1] = 1
for i,j in d.items():
if j == [1,1]:
if ans == '' or ans < i:
ans = i
return ans
|
greatest-english-letter-in-upper-and-lower-case
|
Python3 simple solution
|
EklavyaJoshi
| 0
| 19
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,829
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2177721/Simple-and-Easy-to-Understand-Solution
|
class Solution:
def greatestLetter(self, s: str) -> str:
temp={}
val=0
for i in set(s):
if ord(i)> 64 and ord(i) < 91:
val=ord(i)-64
if val not in temp:
temp[val]=1
val=0
else:
temp[val]+=1
if ord(i)> 96 and ord(i) < 123:
val=ord(i)-96
if val not in temp:
temp[val]=1
val=0
else:
temp[val]+=1
val=0
result=[]
for k,v in temp.items():
if v>1:
result.append(k)
if len(result)>0:
return chr(64 + max(result))
else:
return ""
|
greatest-english-letter-in-upper-and-lower-case
|
Simple and Easy to Understand Solution
|
sangam92
| 0
| 25
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,830
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2176313/Easy-Python-Solution
|
class Solution:
def greatestLetter(self, s: str) -> str:
d=Counter(s)
ls=[]
for i in s:
if ord(i)<91 and (chr(ord(i)+32) in s) :
ls.append(i)
return max(ls) if len(ls)!=0 else ''
|
greatest-english-letter-in-upper-and-lower-case
|
Easy Python Solution
|
nileshporwal
| 0
| 26
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,831
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2172103/Python-or-easy-solution-or-using-ord-and-chr
|
class Solution(object):
def greatestLetter(self, s):
"""
:type s: str
:rtype: str
"""
ans = []
f =''
x = set()
y = set()
for i in s:
if i.islower():
x.add(i)
elif i.isupper():
y.add(i)
for j in s:
if j.lower() in x:
if j.upper() in y:
if j.upper() in ans:
continue
else:
ans.append(ord(j.upper()))
if ans == []:
return f
else:
return chr(max(ans))
```
|
greatest-english-letter-in-upper-and-lower-case
|
Python | easy solution | using ord and chr
|
aayushcode07
| 0
| 14
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,832
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2171986/Python-2-lines
|
class Solution:
def greatestLetter(self, s: str) -> str:
letters = set(ch for ch in s if ch.upper() in s and ch.lower() in s)
return "" if not letters else max(letters).upper()
|
greatest-english-letter-in-upper-and-lower-case
|
Python 2 lines
|
SmittyWerbenjagermanjensen
| 0
| 20
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,833
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2171563/Checking-ordered-letters-in-the-set
|
class Solution:
letters = [('Z', 'z'), ('Y', 'y'), ('X', 'x'), ('W', 'w'), ('V', 'v'),
('U', 'u'), ('T', 't'), ('S', 's'), ('R', 'r'), ('Q', 'q'),
('P', 'p'), ('O', 'o'), ('N', 'n'), ('M', 'm'), ('L', 'l'),
('K', 'k'), ('J', 'j'), ('I', 'i'), ('H', 'h'), ('G', 'g'),
('F', 'f'), ('E', 'e'), ('D', 'd'), ('C', 'c'), ('B', 'b'),
('A', 'a')]
def greatestLetter(self, s: str) -> str:
set_s = set(s)
for up_char, lo_char in Solution.letters:
if up_char in set_s and lo_char in set_s:
return up_char
return ""
|
greatest-english-letter-in-upper-and-lower-case
|
Checking ordered letters in the set
|
EvgenySH
| 0
| 9
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,834
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2171360/2309.-Greatest-English-Letter-in-Upper-and-Lower-Case
|
class Solution:
def greatestLetter(self, s: str) -> str:
d = {}
l = []
for i in s:
if i.islower():
if i.upper() in d.keys():
l.append(i.upper())
else:
d[i] = False
elif i.isupper():
if i.lower() in d.keys():
l.append(i.upper())
else:
d[i] = False
if len(l) > 0:
return sorted(l)[-1]
else:
return ""
|
greatest-english-letter-in-upper-and-lower-case
|
2309. Greatest English Letter in Upper and Lower Case
|
bodasaieswar
| 0
| 9
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,835
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2170586/Linear-time-and-Constant-space
|
class Solution:
def greatestLetter(self, s: str) -> str:
res = ""
for i in s:
if i.isupper() and i.lower() in s:
if i >res:
res = i
return res
|
greatest-english-letter-in-upper-and-lower-case
|
Linear time and Constant space
|
tuan882612
| 0
| 30
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,836
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2170505/Python-Solution
|
class Solution:
def greatestLetter(self, s: str) -> str:
lowers = {c.upper() for c in s if c.islower()}
uppers = {c for c in s if c.isupper()}
both = lowers.intersection(uppers)
return sorted(both, reverse=True)[0] if both else ''
|
greatest-english-letter-in-upper-and-lower-case
|
Python Solution
|
siavrez
| 0
| 17
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,837
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2169671/PYTHON-Linear-Time-and-Constant-Space-with-Explaination
|
class Solution:
def greatestLetter(self, s: str) -> str:
map = [0]*26
for i in s:
if i.isupper():# check for uppercase
map[ord(i)-ord('A')] |= 1
else:# for lowercase
map[ord(i) - ord('a')] |= 2
for i in range(25,-1,-1):
if map[i] == 3:
return chr(i+ord("A"))# return greatest letter
return ''# return empty string if not found
|
greatest-english-letter-in-upper-and-lower-case
|
[PYTHON] Linear Time and Constant Space with Explaination
|
feelme
| 0
| 17
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,838
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2169350/Weekly-Contest-or-Python-or-Easy-inderstading
|
class Solution:
def greatestLetter(self, s: str) -> str:
max_index = -1
lower = [0] * 26
upper = [0] * 26
for c in s:
if c.isupper():
upper[ord(c) - ord('A')] = 1
else:
lower[ord(c) - ord('a')] = 1
for i, item in enumerate(zip(lower, upper)):
if item[0] == 1 and item[1] == 1:
max_index = i
return chr(65 + max_index) if max_index != -1 else ""
|
greatest-english-letter-in-upper-and-lower-case
|
Weekly Contest | Python | Easy-inderstading
|
JavanMyna
| 0
| 10
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,839
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2168840/Python-Solution-100-working-with-O(26)-complexity
|
class Solution:
def greatestLetter(self, s: str) -> str:
st="abcdefghijklmnopqrstuvwxyz"
st2="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
r=[]
for i in range(len(st)):
if st[i] in s and st2[i] in s:
r.append(st2[i])
if len(r)==0:
return ""
r.sort()
return r[-1]
|
greatest-english-letter-in-upper-and-lower-case
|
Python Solution-100% working ✅ with O(26) complexity
|
T1n1_B0x1
| 0
| 13
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,840
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2168816/Python-Easy-to-understand
|
class Solution:
def greatestLetter(self, s: str) -> str:
if s.isupper() or s.islower():
return ''
greatest = ''
for char in s:
if char.upper() in s and char.lower() in s:
if char>greatest:
greatest=char
return greatest.upper()
|
greatest-english-letter-in-upper-and-lower-case
|
Python, Easy to understand
|
AJsenpai
| 0
| 17
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,841
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2168640/Python3-check-uppercase
|
class Solution:
def greatestLetter(self, s: str) -> str:
seen = set(s)
for ch in reversed(ascii_uppercase):
if ch in seen and ch.swapcase() in seen: return ch
return ""
|
greatest-english-letter-in-upper-and-lower-case
|
[Python3] check uppercase
|
ye15
| 0
| 20
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,842
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2168496/Python-oror-Easy-Approach
|
class Solution:
def greatestLetter(self, s: str) -> str:
ordans = 0
flag = 0
n = len(s)
for x in s:
if x.isupper() == True and x.lower() in s:
flag = 1
ordans = max(ordans, ord(x))
if flag == 0:
return ""
return chr(ordans)
|
greatest-english-letter-in-upper-and-lower-case
|
✅Python || Easy Approach
|
chuhonghao01
| 0
| 16
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,843
|
https://leetcode.com/problems/greatest-english-letter-in-upper-and-lower-case/discuss/2168302/Python-Solution-or-O(N)-time-complexity-using-Dictionary-or-Easy-to-understand
|
class Solution:
def greatestLetter(self, s: str) -> str:
d = {}
ans = ""
for i in s:
if i in d:
ans = max(ans,i.upper())
else:
if i.islower():
d[i.upper()] = i
else:
d[i.lower()] = i
return ans
|
greatest-english-letter-in-upper-and-lower-case
|
Python Solution | O(N) time complexity using Dictionary | Easy to understand
|
AkashHooda
| 0
| 26
|
greatest english letter in upper and lower case
| 2,309
| 0.686
|
Easy
| 31,844
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2168546/Python-oror-Easy-Approach-oror-beats-90.00-Both-Runtime-and-Memory-oror-Remainder
|
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
if num == 0:
return 0
if num < k:
return -1
if num == k:
return 1
ans = -1
i = 1
while i <= 10:
if (num - i * k) % 10 == 0 and i * k <= num:
return i
i += 1
return ans
|
sum-of-numbers-with-units-digit-k
|
✅Python || Easy Approach || beats 90.00% Both Runtime & Memory || Remainder
|
chuhonghao01
| 1
| 43
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,845
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2168286/Python-oror-Intuitive-Solution-oror-Look-for-smallest-multiple-having-same-number-at-unit-place
|
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
if k%2 == 0 and num%2 != 0:
return -1
if num == 0:
return 0
elif k == 0 and num%10 == 0:
return 1
elif k == 0:
return -1
i = 1
while True:
if i*k > num:
return -1
if (i*k)%10 == num%10:
return i
i += 1
|
sum-of-numbers-with-units-digit-k
|
Python || Intuitive Solution || Look for smallest multiple having same number at unit place
|
arjuhooda
| 1
| 86
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,846
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2168276/Python-solution-or-Constant-Time-Complexity-or-Simple-Solution-or-No-DP-nor-Greedy-or
|
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
if num==0:
return 0
if k==0:
if num%10==0:
return 1
return -1
if num<k:
return -1
d = {
1:[1,2,3,4,5,6,7,8,9,0],
2:[2,4,6,8,0],
3:[3,6,9,2,5,8,1,4,7,0],
4:[4,8,2,6,0],
5:[5,0],
6:[6,2,8,4,0],
7:[7,4,1,8,5,2,9,6,3,0],
8:[8,6,4,2,0],
9:[9,8,7,6,5,4,3,2,1,0]
}
if num%10 not in d[k]:
return -1
else:
i = d[k].index(num%10) + 1
if num<i*k:
return -1
return i
|
sum-of-numbers-with-units-digit-k
|
Python solution | Constant Time-Complexity | Simple Solution | No DP nor Greedy |
|
AkashHooda
| 1
| 51
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,847
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2168262/Python-Math-Solution-or-Only-Looking-at-Unit-Digit-or-Clean-and-Concise
|
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
if num == 0:
return 0
unit = num % 10
for i in range(1, 11):
if (i * k) % 10 == unit:
if i * k <= num:
return i
else:
break
return -1
|
sum-of-numbers-with-units-digit-k
|
Python Math Solution | Only Looking at Unit Digit | Clean & Concise
|
xil899
| 1
| 45
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,848
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2168201/Python-or-With-Explanation-or-Beats-100
|
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
# only consider the last digit!!
# use visited to exit the while loop
if num == 0:
return 0
target_last_digit = num % 10
cur_sum = k
min_size = 1
visited = set()
while cur_sum <= num:
cur_last_digit = cur_sum % 10
if cur_last_digit == target_last_digit:
return min_size
else:
if cur_last_digit in visited:
return -1
visited.add(cur_last_digit)
cur_sum += k
min_size += 1
return -1
|
sum-of-numbers-with-units-digit-k
|
Python | With Explanation | Beats 100%
|
Mikey98
| 1
| 68
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,849
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2816120/Python-(Simple-Maths)
|
class Solution:
def minimumNumbers(self, num, k):
if num == 0:
return 0
if num < k:
return -1
for i in range(1,11):
if (i*k)%10 == num%10 and i*k <= num:
return i
return -1
|
sum-of-numbers-with-units-digit-k
|
Python (Simple Maths)
|
rnotappl
| 0
| 2
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,850
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2767259/Python-or-Wacky-O(1)-Maths-Solution-No-Loops
|
class Solution:
def __init__ (self):
self.params = [None, (1,10), (3,5), (7,10), (4,5), None, (1,5), (3,10), (2,5), (9,10)]
def minimumNumbers(self, num: int, k: int) -> int:
if num == 0:
return 0
# Is there a solution?
d = gcd(k,10)
if num % d:
return -1
if k == 0:
return 1
if k == 5:
if num % 10 == 0:
return 2
else:
return 1
min_needed = (num * self.params[k][0]) % self.params[k][1]
if min_needed == 0:
min_needed = self.params[k][1]
return min_needed if min_needed * k <= num else -1
|
sum-of-numbers-with-units-digit-k
|
Python | Wacky O(1) Maths Solution, No Loops
|
on_danse_encore_on_rit_encore
| 0
| 4
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,851
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2497181/PYTHON-straight-to-the-point-solution-T%3A-O(n)-S%3A-O(1)
|
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
if num==0:
return 0
if k==0 and num%10!=0:
return -1
else:
rez=1
l=k
#we don't care about the other digits so we check only the last digit
while (l%10 !=k or l==k) and l%10 !=num%10: #checking if we entered a loop
l+=k
rez+=1
if l%10==num%10 and l<=num:
'''
if we did reach a possible value we also have to check if it isn't bigger than nums ex: 17 9 - l will reack value 27, it's unit digit is 7 but 27 is bigger than 9
'''
return rez
return -1
|
sum-of-numbers-with-units-digit-k
|
PYTHON straight to the point solution T: O(n) S: O(1)
|
thunder34
| 0
| 52
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,852
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2176348/Python3-dp-and-math
|
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
@cache
def fn(x):
"""Return min size for x."""
if x == 0: return 0
ans = inf
for cand in range(1, x+1):
if cand % 10 == k: ans = min(ans, 1 + fn(x-cand))
return ans
ans = fn(num)
return ans if ans < inf else -1
|
sum-of-numbers-with-units-digit-k
|
[Python3] dp & math
|
ye15
| 0
| 27
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,853
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2176348/Python3-dp-and-math
|
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
if num == 0: return 0
for x in range(1, 11):
if x*k <= num and (num - x*k) % 10 == 0: return x
return -1
|
sum-of-numbers-with-units-digit-k
|
[Python3] dp & math
|
ye15
| 0
| 27
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,854
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2175367/Python-Simple-Solution
|
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
arr = []
if num == 0:
return 0
for i in range(1, 11):
if num % 10 == (i * k) % 10 and i * k <= num:
return i
return -1
|
sum-of-numbers-with-units-digit-k
|
Python Simple Solution
|
user6397p
| 0
| 21
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,855
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2170355/Easy-Mathematical-Solution
|
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
if num == 0:
return 0
arr =[]
for i in range(1, 11):
arr.append(i*k)
for i in range(0, len(arr)):
if (num-arr[i])%10 == 0 and (num-arr[i])>=0:
return i + 1
return -1
|
sum-of-numbers-with-units-digit-k
|
Easy Mathematical Solution
|
Zikai_Lian
| 0
| 14
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,856
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2169389/Weekly-Contest-or-Python-or-Easy-understanding
|
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
if num == 0:
return 0
if num < k:
return -1
if num == k:
return 1
numDict = {0: [0],
1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
2: [2, 4, 6, 8, 0],
3: [3, 6, 9, 2, 5, 8, 1, 4, 7, 0],
4: [4, 8, 2, 6, 0],
5: [5, 0],
6: [6, 2, 8, 4, 0],
7: [7, 4, 1, 8, 5, 2, 9, 6, 3, 0],
8: [8, 6, 4, 2, 0],
9: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]}
last_digit = num % 10
# print(last_digit)
# print(numDict[k])
if last_digit not in numDict[k]:
return -1
else:
index = numDict[k].index(last_digit) + 1
if index * k <= num:
# if 10 * index <= num - index * k:
return index
else:
return -1
```
|
sum-of-numbers-with-units-digit-k
|
Weekly Contest | Python | Easy-understanding
|
JavanMyna
| 0
| 8
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,857
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2168839/python3-iteration-solution-using-math-for-reference
|
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
cnt = 0
# needs handling upfront since k = 0 does not help iteration !
if k == 0 and num > 0:
return 1 if num%10 == k else -1
if num == 0:
return 0
while num >= 0:
if num % 10 == k:
return cnt + 1
cnt += 1
num -= k
return -1
|
sum-of-numbers-with-units-digit-k
|
[python3] iteration solution using math for reference
|
vadhri_venkat
| 0
| 9
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,858
|
https://leetcode.com/problems/sum-of-numbers-with-units-digit-k/discuss/2168375/coin-change-variation-oror-bottom-up-dp-oror-python3
|
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
if num==0:
return 0
if num<k:
return -1
nums=[]
while k<=num:
nums.append(k)
k+=10
amount=num
n=len(nums)
dp=[[10**9]*(num+1)for _ in range(n)]
for i in range(n):
dp[i][0]=0
if nums[0]<=amount:
if nums[0]!=0 and amount%nums[0]==0:
dp[0][nums[0]]=amount//nums[0]
for i in range(n):
for j in range(1,num+1):
take=10**9
if nums[i]<=j:
take=1+dp[i][j-nums[i]]
not_take=dp[i-1][j]
dp[i][j]=min(take,not_take)
ans=dp[i][j]
if ans!=10**9:
return ans
return -1
|
sum-of-numbers-with-units-digit-k
|
coin change variation || bottom-up dp || python3
|
akshat12199
| 0
| 15
|
sum of numbers with units digit k
| 2,310
| 0.255
|
Medium
| 31,859
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2168527/PythonororGreedyororFastororEasy-to-undestandoror-With-explanations
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
n = len(s)
ones = []
# Notice how I reversed the string,
# because the binary representation is written from greatest value of 2**n
for i, val in enumerate(s[::-1]):
if val == '1':
ones.append(i)
# Initialize ans, there are already number of zeroes (num_of_zeroes = len(nums) - len(ones)
ans = n - len(ones)
i = 0
# imagine k == 5 and binary string 001011
# ones = [0, 1, 3]
# first loop: 5 - 2**0 -> 4, ans += 1
# second loop: 4 - 2**1 -> 2, ans +=1
# Third loop does not occur because 2 - 2**3 -> -6 which is less than zero
# So the ans is 3 + 2 = 5
while i < len(ones) and k - 2 ** ones[i] >= 0:
ans += 1
k -= 2 ** ones[i]
i += 1
return ans
|
longest-binary-subsequence-less-than-or-equal-to-k
|
Python||Greedy||Fast||Easy to undestand|| With explanations
|
muctep_k
| 5
| 127
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,860
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2168557/Python-oror-Easy-Approach-oror-beats-90.00-Runtime
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
ans = 0
n = len(s)
if k > int(s, 2):
return n
for i in range(n):
if int(s[n - 1 - i:], 2) > k:
curr = i
break
if i == n - 1:
return n
ans = i + s[:n - i].count("0")
return ans
|
longest-binary-subsequence-less-than-or-equal-to-k
|
✅Python || Easy Approach || beats 90.00% Runtime
|
chuhonghao01
| 2
| 59
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,861
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2168303/Python-O(N)-Solution-or-Count-0-and-Working-Backwards
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
n = len(s)
total0 = 0
for char in s:
if char == '0':
total0 += 1
if total0 == n:
return n
curr, temp, total1 = 0, 1, 0
for i in range(n - 1, -1, -1):
if s[i] == '1':
curr += temp
if curr > k:
return total0 + total1
total1 += 1
temp *= 2
return n
|
longest-binary-subsequence-less-than-or-equal-to-k
|
Python O(N) Solution | Count 0 and Working Backwards
|
xil899
| 1
| 40
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,862
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2168224/Python-oror-Intuitive-linear-Solution-oror-O(n)
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
b = bin(k)[2:]
print(b)
ind = []
for i in range(len(b)):
if b[i] == '1':
ind.append(len(b)-i-1)
flag = True
l = 0
for i in s[::-1]:
if i == '0':
l += 1
else:
while ind and l > ind[-1]:
ind.pop()
flag = True
if ind and ind[-1] == l and not flag:
ind.pop()
if ind:
l += 1
flag = False
return l
|
longest-binary-subsequence-less-than-or-equal-to-k
|
Python || Intuitive linear Solution || O(n)
|
arjuhooda
| 1
| 57
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,863
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2697626/O(n)-Simple-and-Fast
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
t = f"{k:b}"
if len(t) > len(s):
return len(s)
out = s[:-len(t)].count('0')
remove = 0
for i in range(len(t)):
c1, c2 = t[i], s[-len(t) + i]
if c1 == '1' and c2 == '0': # s[:-len(t)] smaller than t
break
if c1 == '0' and c2 == '1': # s[:-len(t) - 1] < t < s[:-len(t)]
remove = 1
break
return out + len(t) - remove
|
longest-binary-subsequence-less-than-or-equal-to-k
|
O(n) Simple and Fast
|
mysong105
| 0
| 7
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,864
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2250154/Python3-clean-code-recursion-%2B-memoization
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
def dp(s, index, k, val, memo, power):
if index == -1:
return 0
if (index, val) in memo:
return memo[(index, val)]
ans = -math.inf
new_val = 2**power * int(s[index])
# Choose to include this value
if val + new_val <= k:
loc_ans = 1 + dp(s, index - 1, k, val + new_val, memo, power + 1)
ans = max(loc_ans, ans)
# Skip this value
else:
loc_ans = dp(s, index - 1, k, val, memo, power)
ans = max(loc_ans, ans)
memo[(index, val)] = ans
return ans
return dp(s, len(s) - 1, k, 0, {}, 0)
|
longest-binary-subsequence-less-than-or-equal-to-k
|
Python3 clean code - recursion + memoization
|
myvanillaexistence
| 0
| 73
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,865
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2197651/Python3-Greedy
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
cur = s[-1]
for idx in range(len(s) - 1)[::-1]:
if int(s[idx] + cur, 2) <= k:
cur = s[idx] + cur
return len(cur)
|
longest-binary-subsequence-less-than-or-equal-to-k
|
[Python3] Greedy
|
0xRoxas
| 0
| 54
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,866
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2176338/Python3-greedy
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
ans = val = 0
for i, ch in enumerate(reversed(s)):
if ch == '0': ans += 1
elif i < 30 and val + (1<<i) <= k:
ans += 1
val += 1<<i
return ans
|
longest-binary-subsequence-less-than-or-equal-to-k
|
[Python3] greedy
|
ye15
| 0
| 19
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,867
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2171055/Python-3DP-with-memorization
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
k_bit = bin(k)[2:]
m = len(k_bit)
n = len(s)
@lru_cache(None)
# flag is to indicate whether '1' included
def dp(i, j, flag):
if i == m or j == n:
return 0
if s[j] == '0':
if not flag:
return 1 + dp(i, j + 1, flag) # include as many zero as possible if not leading "1"
else:
# if current target bit is "1" then takes rest of the strings since it will guarantee smaller than k
return 1 + dp(i + 1, j + 1, flag) if k_bit[i] == '0' else min(m - i, n - j)
elif k_bit[i] == '1':
return max(1 + dp(i + 1, j + 1, True), dp(i, j + 1, flag))
else:
# include one less digit is only way to make smaller if digit in s is '1' and in k is '0'
return max(dp(i, j + 1, flag), min(m - i - 1, n - j))
if '1' not in s or m > n: return n
return dp(0, 0, False)
|
longest-binary-subsequence-less-than-or-equal-to-k
|
[Python 3]DP with memorization
|
chestnut890123
| 0
| 40
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,868
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2169641/python3-Iteration-sol-for-reference.
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
N = len(s)
ans = 0
value = 0
total_zeros = s.count('0')
for x in range(N-1, -1, -1):
value = (int(s[x]) << (N-x-1)) + value
if s[x] == '0':
total_zeros -= 1
if value <= k:
ans = max(ans, total_zeros+(N-x))
return ans
|
longest-binary-subsequence-less-than-or-equal-to-k
|
[python3] Iteration sol for reference.
|
vadhri_venkat
| 0
| 4
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,869
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2168556/Python-O(N)-Sliding-Window-Solution
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
l = 0
r = len(s)-1
while l <= r and int(s, 2) > k:
if s[l] == '1':
s = s[0:l]+s[l+1:]
else:
l += 1
return len(s)
|
longest-binary-subsequence-less-than-or-equal-to-k
|
[Python] O(N) Sliding Window Solution
|
dogogo
| 0
| 31
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,870
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2168455/python-dynamic-programming
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
# DP = smallest number with a length of i
dp = [0]
for c in reversed(s):
thisDigit = 0 if c == '0' else 1
if thisDigit == 0:
dp = [0] + dp
else:
dp.append(dp[-1] + 2**(len(dp)-1))
for i in range(len(dp)-1, -1, -1):
if dp[i] <= k:
return i
|
longest-binary-subsequence-less-than-or-equal-to-k
|
python dynamic programming
|
shepherd_a
| 0
| 63
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,871
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2168426/Python-simple-intuitive-solution
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
n=len(s)
res=""
for i in range(n-1,-1,-1):
tmp=s[i]+res
if int(tmp,2)<=k:
res=s[i]+res
return len(res)
|
longest-binary-subsequence-less-than-or-equal-to-k
|
Python simple intuitive solution
|
atm1504
| 0
| 22
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,872
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2168235/Python-Solution-or-Easy-to-Understand-or-Iteration-from-right
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
x =bin(k)[2:]
ind = []
for i in range(len(x)):
if x[i]=="1":
ind.append(len(x)-i-1)
f = True
l = 0
for i in s[::-1]:
if i=="0":
l+=1
else:
while ind and l>ind[-1]:
ind.pop()
f = True
if ind and ind[-1]==l and not f:
ind.pop()
if ind:
l+=1
f = False
return l
|
longest-binary-subsequence-less-than-or-equal-to-k
|
Python Solution | Easy to Understand | Iteration from right
|
AkashHooda
| 0
| 25
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,873
|
https://leetcode.com/problems/longest-binary-subsequence-less-than-or-equal-to-k/discuss/2168165/Python-or-With-Explanation-or-Easy-to-Understand
|
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
# count all '0' first
# traverse the s from back to front, try each digit '1'
target = bin(k)[2:]
result = s.count('0')
n = len(s)
cur_sum = 0
for i in range(n-1, -1, -1):
char = s[i]
if char == '1':
cur_num = 2 ** ((n-1)-i)
cur_sum += cur_num
if cur_sum > k:
return result
else:
result += 1
return result
|
longest-binary-subsequence-less-than-or-equal-to-k
|
Python | With Explanation | Easy to Understand
|
Mikey98
| 0
| 45
|
longest binary subsequence less than or equal to k
| 2,311
| 0.364
|
Medium
| 31,874
|
https://leetcode.com/problems/selling-pieces-of-wood/discuss/2194345/Python-bottom-up-DP-faster-than-99
|
class Solution:
def sellingWood(self, m: int, n: int, prices: List[List[int]]) -> int:
dp = [[0]*(n+1) for _ in range(m+1)]
for h, w, p in prices:
dp[h][w] = p
for i in range(1, m+1):
for j in range(1, n+1):
v = max(dp[k][j] + dp[i - k][j] for k in range(1, i // 2 + 1)) if i > 1 else 0
h = max(dp[i][k] + dp[i][j - k] for k in range(1, j // 2 + 1)) if j > 1 else 0
dp[i][j] = max(dp[i][j], v, h)
return dp[m][n]
|
selling-pieces-of-wood
|
Python bottom up DP faster than 99%
|
metaphysicalist
| 1
| 75
|
selling pieces of wood
| 2,312
| 0.482
|
Hard
| 31,875
|
https://leetcode.com/problems/selling-pieces-of-wood/discuss/2176350/Python3-dp
|
class Solution:
def sellingWood(self, m: int, n: int, prices: List[List[int]]) -> int:
mp = {(h, w) : p for h, w, p in prices}
@cache
def fn(m, n):
"""Return max money of a mxn piece of wood."""
if m == 0 or n == 0: return 0
ans = 0
if (m, n) in mp: ans = mp[m, n]
if m > 1: ans = max(ans, max(fn(i, n) + fn(m-i, n) for i in range(1, m//2+1)))
if n > 1: ans = max(ans, max(fn(m, j) + fn(m, n-j) for j in range(1, n//2+1)))
return ans
return fn(m, n)
|
selling-pieces-of-wood
|
[Python3] dp
|
ye15
| 0
| 23
|
selling pieces of wood
| 2,312
| 0.482
|
Hard
| 31,876
|
https://leetcode.com/problems/count-asterisks/discuss/2484633/Python-Elegant-and-Short-or-Two-solutions-or-One-pass-One-line
|
class Solution:
"""
Time: O(n)
Memory: O(1)
"""
def countAsterisks(self, s: str) -> int:
is_closed = True
count = 0
for c in s:
count += is_closed * c == '*'
is_closed ^= c == '|'
return count
class Solution:
"""
Time: O(n)
Memory: O(n)
"""
def countAsterisks(self, s: str) -> int:
return sum(chunk.count('*') for chunk in s.split('|')[0::2])
|
count-asterisks
|
Python Elegant & Short | Two solutions | One pass / One line
|
Kyrylo-Ktl
| 3
| 137
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,877
|
https://leetcode.com/problems/count-asterisks/discuss/2332609/Python-O(n)-oror-O(1)-Runtime%3A-47ms-64.61-oror-Memory%3A-13.7mb-97.39
|
class Solution:
# O(n) || O(1)
# Runtime: 47ms 64.61% || Memory: 13.7mb 97.39%
def countAsterisks(self, string: str) -> int:
if not string:
return 0
isBetweenBar = False
asteriskCount = 0
for char in string:
if char == '|' and not isBetweenBar:
isBetweenBar = True
elif char == '|' and isBetweenBar:
isBetweenBar = False
if not isBetweenBar and char == '*':
asteriskCount += 1
return asteriskCount
|
count-asterisks
|
Python O(n) || O(1) # Runtime: 47ms 64.61% || Memory: 13.7mb 97.39%
|
arshergon
| 2
| 106
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,878
|
https://leetcode.com/problems/count-asterisks/discuss/2831596/Python3-solution-using-for-loop-and-continue
|
class Solution:
def countAsterisks(self, s: str) -> int:
l = []
temp = ''
for i in s:
if i == '|':
temp += i
# reset the temp, if a pair is closed
if temp.count('|') == 2:
temp = ''
# ignore the characters in between a pair
if '|' in temp:
continue
# if none of the above conditions are met append the character into the list
elif i != '|':
l.append(i)
return ''.join(l).count('*')
|
count-asterisks
|
Python3 solution using for loop and continue
|
kschaitanya2001
| 1
| 27
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,879
|
https://leetcode.com/problems/count-asterisks/discuss/2455815/Python-easy-and-simple-solution
|
class Solution:
def countAsterisks(self, s: str) -> int:
if '*' not in s:
return 0
else:
c=0
bars=0
for i in range(0,len(s)):
if s[i]=="|":
bars=bars+1
if bars%2==0 and s[i]=="*":
c=c+1
return c
|
count-asterisks
|
Python easy and simple solution
|
keertika27
| 1
| 57
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,880
|
https://leetcode.com/problems/count-asterisks/discuss/2430449/Count-Asterisks
|
class Solution:
def countAsterisks(self, s: str) -> int:
tempCount = 0
count=0
for i in s :
if i =="|":
tempCount+=1
if tempCount==2:
tempCount = 0
if tempCount==0 and i =="*":
count+=1
return(count)
|
count-asterisks
|
Count Asterisks
|
dhananjayaduttmishra
| 1
| 25
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,881
|
https://leetcode.com/problems/count-asterisks/discuss/2241765/Python-oror-Easy-to-understand-oror-SIMPLE
|
class Solution:
def countAsterisks(self, s: str) -> int:
count = 0
flag = True
for i in s:
if i=='|':
flag = not flag
if i=='*' and flag:
count+=1
return count
```
|
count-asterisks
|
Python || Easy to understand || SIMPLE
|
Airodragon
| 1
| 53
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,882
|
https://leetcode.com/problems/count-asterisks/discuss/2199925/One-pass-100-speed
|
class Solution:
def countAsterisks(self, s: str) -> int:
count_stars, count = True, 0
for c in s:
if c == "|":
count_stars = not count_stars
elif count_stars and c == "*":
count += 1
return count
|
count-asterisks
|
One pass, 100% speed
|
EvgenySH
| 1
| 44
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,883
|
https://leetcode.com/problems/count-asterisks/discuss/2195867/Python3-1-line
|
class Solution:
def countAsterisks(self, s: str) -> int:
return sum(w.count('*') for i, w in enumerate(s.split('|')) if not i&1)
|
count-asterisks
|
[Python3] 1-line
|
ye15
| 1
| 23
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,884
|
https://leetcode.com/problems/count-asterisks/discuss/2195847/Python-linear-solution-or-comments
|
class Solution:
def countAsterisks(self, s: str) -> int:
n = len(s)
res = 0
isPair = False # tracks pair of "|"
for i in range(n):
# if * is outside of a pair add to result
if not isPair and s[i] == "*":
res += 1
# track "|" pair by boolean toggle
if s[i] == "|":
isPair = not isPair
return res
|
count-asterisks
|
Python linear solution | comments
|
abkc1221
| 1
| 38
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,885
|
https://leetcode.com/problems/count-asterisks/discuss/2835853/python-easy-one-pass-(for-loop-only-no-split())
|
class Solution:
def countAsterisks(self, s: str) -> int:
count,check=0,0
for i in s:
if i =="|" and check==0:
check=1
elif i=="|" and check==1:
check=0
if check==0 and i=="*":
count+=1
return count
|
count-asterisks
|
python easy one pass (for loop only, no split())
|
ATHBuys
| 0
| 2
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,886
|
https://leetcode.com/problems/count-asterisks/discuss/2816360/Fast-and-Simple-Solution-Python
|
class Solution:
def countAsterisks(self, s: str) -> int:
count1, count2 = 0, 0
for i in s:
if count1 % 2 == 0 and i =="*":
count2 += 1
elif i == "|":
count1 += 1
return count2
|
count-asterisks
|
Fast and Simple Solution - Python
|
PranavBhatt
| 0
| 1
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,887
|
https://leetcode.com/problems/count-asterisks/discuss/2788215/python-solution
|
class Solution:
def countAsterisks(self, s: str) -> int:
ans = 0
check = 0
for i in s:
if check == 0 and i == '*':
ans += 1
elif i == '|':
check += 1
if check == 2:
check = 0
return ans
|
count-asterisks
|
python solution
|
shingnapure_shilpa17
| 0
| 3
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,888
|
https://leetcode.com/problems/count-asterisks/discuss/2785223/Python-Solution
|
class Solution:
def countAsterisks(self, s: str) -> int:
s=s.split("|")
c=0
for i in range(0,len(s),2):
c+=s[i].count("*")
return(c)
|
count-asterisks
|
Python Solution
|
CEOSRICHARAN
| 0
| 2
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,889
|
https://leetcode.com/problems/count-asterisks/discuss/2782866/Python-one-pass-with-O(N)-time-and-O(1)-space-complexity
|
class Solution:
def countAsterisks(self, s: str) -> int:
flag = 0
sum = 0
for letter in s:
if letter == '|' and flag == 0:
flag = 1
elif letter == '|' and flag == 1:
flag = 0
elif flag == 0 and letter == '*':
sum += 1
return sum
|
count-asterisks
|
Python one pass with O(N) time and O(1) space complexity
|
deysouvik107
| 0
| 2
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,890
|
https://leetcode.com/problems/count-asterisks/discuss/2760917/python-solution-90-faster
|
class Solution:
def countAsterisks(self, s: str) -> int:
c_asterisks = 0
count_bars = 0
for ch in s:
if count_bars == 0 and ch =="*":
c_asterisks += 1
if count_bars == 0 and ch == "|":
count_bars += 1
elif count_bars != 0 and ch == "|":
count_bars -= 1
return c_asterisks
|
count-asterisks
|
python solution 90% faster
|
samanehghafouri
| 0
| 4
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,891
|
https://leetcode.com/problems/count-asterisks/discuss/2726998/Python3-Solution
|
class Solution:
def countAsterisks(self, s: str) -> int:
consider = True
count = 0
for c in s:
if c == "|":
consider = True^consider
continue
if consider == True and c == '*':
count += 1
return count
|
count-asterisks
|
Python3 Solution
|
sipi09
| 0
| 2
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,892
|
https://leetcode.com/problems/count-asterisks/discuss/2724090/Easy-Python-Solution-in-O(n)-time.
|
class Solution:
def countAsterisks(self, s: str) -> int:
lst = s.split("|")
res = ""
for i in range(len(lst)):
if i%2 == 0:
res += lst[i]
cnt = 0
for i in range(len(res)):
if res[i] == '*':
cnt +=1
return cnt
|
count-asterisks
|
Easy Python Solution in O(n) time.
|
Vidhiiii
| 0
| 2
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,893
|
https://leetcode.com/problems/count-asterisks/discuss/2714677/Beginner
|
class Solution:
def countAsterisks(self, s: str) -> int:
flag = True
cnt = 0
for i in s:
if i=='|':
flag = not flag // to toggle flag
elif i=='*':
if flag:
cnt+=1
return cnt
|
count-asterisks
|
Beginner
|
Tushar_051
| 0
| 3
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,894
|
https://leetcode.com/problems/count-asterisks/discuss/2663988/PYTHON-simple-solution-or-Logic-Based
|
class Solution:
def countAsterisks(self, s: str) -> int:
count,ans=0,0
for i in s:
if i=='|':
count+=1
if i=='*' and count%2==0:
ans+=1
return ans
|
count-asterisks
|
PYTHON simple solution | Logic Based
|
utsa_gupta
| 0
| 3
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,895
|
https://leetcode.com/problems/count-asterisks/discuss/2570823/SIMPLE-PYTHON3-SOLUTION-95-FASter
|
class Solution:
def countAsterisks(self, s: str) -> int:
within_pair = False
asterisk_count = 0
for ch in s:
if ch == '|':
within_pair = not within_pair
elif ch == '*' and not within_pair:
asterisk_count += 1
return asterisk_count
|
count-asterisks
|
✅✔ SIMPLE PYTHON3 SOLUTION ✅✔ 95% FASter
|
rajukommula
| 0
| 18
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,896
|
https://leetcode.com/problems/count-asterisks/discuss/2478843/Simple-python-solution
|
class Solution:
def countAsterisks(self, s: str) -> int:
s_list = s.split("|")
count = 0
for i in range(0,len(s_list),2):
count += s_list[i].count("*")
return count
|
count-asterisks
|
Simple python solution
|
aruj900
| 0
| 24
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,897
|
https://leetcode.com/problems/count-asterisks/discuss/2476549/Python-Simple-Python-Solution-Using-Odd-Even-Index-Concept
|
class Solution:
def countAsterisks(self, s: str) -> int:
total_asterik = s.count('*')
result = 0
odd_index = -1
even_index = -1
count = 0
for i in range(len(s)):
if s[i] == '|':
if count % 2 == 0:
even_index = i
else:
odd_index = i
count = count + 1
if even_index != -1 and odd_index != -1:
for j in range(even_index, odd_index+1):
if s[j] == '*':
result = result + 1
count = 0
even_index, odd_index = -1, -1
return total_asterik - result
|
count-asterisks
|
[ Python ] ✅✅ Simple Python Solution Using Odd, Even Index Concept 🥳✌👍
|
ASHOK_KUMAR_MEGHVANSHI
| 0
| 16
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,898
|
https://leetcode.com/problems/count-asterisks/discuss/2469460/Python3-or-with-assert
|
class Solution:
def countAsterisks(self, s: str) -> int:
count = 0
if '|' not in s:
count += s.count('*')
while '|' in s:
split_list = s.split('|', 2)
count += split_list[0].count('*')
s = split_list[2]
if '|' not in split_list[2]:
count += split_list[2].count('*')
return count
# assert Solution().countAsterisks('"yo|uar|e**|b|e***au|tifu|l"') == 5
# assert Solution().countAsterisks('iamprogrammer') == 0
# assert Solution().countAsterisks('l|*e*et|c**o|*de|') == 2
# assert Solution().countAsterisks("||*") == 1
# assert Solution().countAsterisks("|*|") == 0
# assert Solution().countAsterisks("||") == 0
# assert Solution().countAsterisks("*||") == 1
# assert Solution().countAsterisks("*") == 1
|
count-asterisks
|
Python3 | with assert
|
Sergei_Gusev
| 0
| 17
|
count asterisks
| 2,315
| 0.825
|
Easy
| 31,899
|
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.