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/check-if-two-string-arrays-are-equivalent/discuss/2539356/Python-Solution | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
w1=''
w2=''
for i in word1:
print(i)
w1+=i
for j in word2:
print(j)
w2+=j
if w1==w2:
return True
else:
return False | check-if-two-string-arrays-are-equivalent | Python Solution | Sneh713 | 1 | 45 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,000 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2103100/Python-One-Line-or-98.7-Efficient-or-Simple | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return ("".join(word1) == "".join(word2)) | check-if-two-string-arrays-are-equivalent | Python One Line | 98.7% Efficient | Simple | nikhitamore | 1 | 83 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,001 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/1839426/Easy-Python-simple-Solution-or-Faster-than-92.20 | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
s1="".join(word1)
s2="".join(word2)
if s1==s2:
return True
else:
return False
``` | check-if-two-string-arrays-are-equivalent | Easy Python simple Solution | Faster than 92.20 % | manav023 | 1 | 70 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,002 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/1455478/Join-arrays-into-strings-99-speed | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return "".join(word1) == "".join(word2) | check-if-two-string-arrays-are-equivalent | Join arrays into strings, 99% speed | EvgenySH | 1 | 218 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,003 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/1266781/3-Lines-of-code-and-Woah!!-Python | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
wrd1,wrd2 = ''.join(word1),''.join(word2)
if wrd1 == wrd2: return True
else: return False | check-if-two-string-arrays-are-equivalent | 3 Lines of code and Woah!! Python | Yodagaz | 1 | 67 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,004 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/1236449/python3-Easy-to-understand-and-natural-approach | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
str1 = ''
for i in word1:
str1 += i
str2=''
for j in word2:
str2 += j
return str1==str2 | check-if-two-string-arrays-are-equivalent | [python3] Easy to understand and natural approach | nandanabhishek | 1 | 198 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,005 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/1136597/Python-Simple-and-Easy-To-Understand-Solution | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
s1="".join(word1)
s2="".join(word2)
return s1 == s2: | check-if-two-string-arrays-are-equivalent | Python Simple & Easy To Understand Solution | saurabhkhurpe | 1 | 245 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,006 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2806422/Python-Solution-easiestgreatergreatergreatergreatergreatergreater-easy | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
w="".join(word1)
x="".join(word2)
if w in x and x in w:
return True
else:
return False | check-if-two-string-arrays-are-equivalent | Python Solution easiest>>>>>> easy | shashwatyadav2002 | 0 | 1 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,007 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2781802/Easy-String-Method-Solution | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
s1=''.join(word1)
s2=''.join(word2)
if s1==s2:
return True
else:
return False | check-if-two-string-arrays-are-equivalent | Easy String Method Solution | SnehaGanesh | 0 | 2 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,008 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2777163/OneLiner-Python3-solution | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return True if("".join(word1)=="".join(word2)) else False | check-if-two-string-arrays-are-equivalent | OneLiner Python3 solution | suyog_097 | 0 | 1 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,009 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2772864/Python3-Solution-with-using-concatenation | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return "".join(word1) == "".join(word2) | check-if-two-string-arrays-are-equivalent | [Python3] Solution with using concatenation | maosipov11 | 0 | 2 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,010 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2744167/Implementation-with-generators | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
#return ''.join(word1) == ''.join(word2)
#in efficient code above
for c1, c2 in zip(self.generator(word1), self.generator(word2)):
if c1 != c2:
return False
return True
def generator(self, wordList: List[str]):
for word in wordList:
for char in word:
yield char
yield None | check-if-two-string-arrays-are-equivalent | Implementation with generators | tammadm | 0 | 3 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,011 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2744075/Easy-Python3-solution%3A-1-condition-check-oror-O(1)-complexity-oror-beats-70-of-the-solutions | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
a = b = ""
if a.join(word1) == b.join(word2):
return True
else:
return False | check-if-two-string-arrays-are-equivalent | Easy Python3 solution: 1 condition check || O(1) complexity || beats 70% of the solutions | GNSharma | 0 | 12 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,012 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2744015/Python-1-line-code | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return''.join(word1) ==''.join(word2) | check-if-two-string-arrays-are-equivalent | Python 1 line code | kumar_anand05 | 0 | 4 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,013 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2743937/PYTHON-Python-One-line-solution-oror-EASY | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return "".join(word1) == "".join(word2) | check-if-two-string-arrays-are-equivalent | [PYTHON] Python One line solution || EASY | valera_grishko | 0 | 3 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,014 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2743909/1-line-Solution | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return ''.join(word1) == ''.join(word2) | check-if-two-string-arrays-are-equivalent | 1 line Solution | user6770yv | 0 | 1 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,015 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2743758/Straightforward-Pythonic-Solution | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
str1 = "".join(word1)
str2 = "".join(word2)
return True if str1 == str2 else False | check-if-two-string-arrays-are-equivalent | Straightforward Pythonic Solution | yaha12 | 0 | 3 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,016 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2743640/Python-One-liner | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return ''.join(word1) == ''.join(word2) | check-if-two-string-arrays-are-equivalent | Python One liner | vijay_2022 | 0 | 4 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,017 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2743620/Solution-using-join() | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
str1 = "".join(word1)
str2 = "".join(word2)
return str1 == str2 | check-if-two-string-arrays-are-equivalent | Solution using join() | pratiklilhare | 0 | 4 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,018 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2743515/Python-or-Simple-or-faster-than-95.77-or-4-lines | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
s1 = ''.join(word1)
s2=''.join(word2)
if s1==s2:
return True
return False | check-if-two-string-arrays-are-equivalent | 🔥 Python | Simple | faster than 95.77% | 4 lines ✔ | ShubhamVora1206 | 0 | 4 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,019 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2743487/2-approachs-for-better-understanding-using-python | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
"""a=""
for i in word1:
a+=i
b=""
for i in word2:
b+=i
if a==b:
return True
return False
"""
print("".join(word1))
return "".join(word1)=="".join(word2) | check-if-two-string-arrays-are-equivalent | 2 approachs for better understanding using python | V_Bhavani_Prasad | 0 | 7 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,020 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2743447/Python-or-One-Liner-or-Easy-and-Simple | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return "".join(word1) == "".join(word2) | check-if-two-string-arrays-are-equivalent | ✔ Python | One Liner | Easy and Simple | sarveshmantri200 | 0 | 3 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,021 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2743242/Python-Solution | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
s1 = ""
s2 = ""
for i in range(len(word1)):
s1 = s1+word1[i]
for j in range(len(word2)):
s2 = s2+word2[j]
if(s1 == s2):
return True
else:
return False | check-if-two-string-arrays-are-equivalent | Python Solution | yashkumarjha | 0 | 2 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,022 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2743231/Python-Solution-(short-circuit-map-len-join)-oror-99.82-of-Python3-(23ms) | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return sum(map(len,word1)) == sum(map(len,word1)) and ''.join(word1) == ''.join(word2) | check-if-two-string-arrays-are-equivalent | Python Solution (short circuit, map, len, join) || 99.82% of Python3 (23ms) | MarcosReyes | 0 | 9 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,023 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2743156/Python-easy-one-liner | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return "".join(word1) == "".join(word2) | check-if-two-string-arrays-are-equivalent | Python easy one liner | adangert | 0 | 4 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,024 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2743018/Python-One-liner-easy-sol. | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return ''.join(word1)==''.join(word2) | check-if-two-string-arrays-are-equivalent | Python One liner, easy sol. | jps194 | 0 | 2 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,025 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2742897/Python3!-1-Line-solution. | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return ''.join(word1) == ''.join(word2) | check-if-two-string-arrays-are-equivalent | 😎Python3! 1 Line solution. | aminjun | 0 | 4 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,026 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2742846/Easy-Fast-python3-python | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
s = ''
for x in word1:
s += x
a= ''
for y in word2:
a += y
if a == s:
return True
return False | check-if-two-string-arrays-are-equivalent | Easy Fast python3 - python | genaral-kg | 0 | 5 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,027 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2742785/Simple-one-liner | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return (''.join(word1) == ''.join(word2)) | check-if-two-string-arrays-are-equivalent | Simple one liner | MockingJay37 | 0 | 2 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,028 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2742651/Python-One-Liner | class Solution:
def arrayStringsAreEqual(self, w1: List[str], w2: List[str]) -> bool:
return "".join(w1) == "".join(w2) | check-if-two-string-arrays-are-equivalent | Python One Liner | SouravSingh49 | 0 | 7 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,029 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2742435/Small-improvement-to-a-naive-approach | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
i, j = 0, 0
s_i, s_j = 0, 0
s1, s2 = '', ''
while i < len(word1):
if s2[s_i:s_i + min(s_j - s_i, len(word1[i]))] != word1[i][:min(s_j - s_i, len(word1[i]))]:
return False
s_i += len(word1[i])
s1 += word1[i]
i += 1
while s_i >= s_j and j < len(word2):
if s1[s_j:s_j + min(s_i - s_j, len(word2[j]))] != word2[j][:min(s_i - s_j, len(word2[j]))]:
return False
s_j += len(word2[j])
s2 += word2[j]
j += 1
return s_i == s_j | check-if-two-string-arrays-are-equivalent | Small improvement to a naive approach | nonchalant-enthusiast | 0 | 4 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,030 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2742257/Avoids-worst-case-scenarios!!-i-guess-..-share-any-refined-codes-too | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
str1 = "".join(word1)
str2 = "".join(word2)
if len(str1) != len(str2):
return(False)
print(str1,str2)
for i in range(0,len(str1)):
print(str1[i],str2[i])
if str1[i] != str2[i]:
return(False)
return(True) | check-if-two-string-arrays-are-equivalent | Avoids worst case scenarios!! i guess .. share any refined codes too | tkrishnakumar30 | 0 | 2 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,031 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2742138/PYTHON-oror-ONE-LINE-SOLUTION-oror-93-FASTER-CODE | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return ''.join(word1) == ''.join(word2) | check-if-two-string-arrays-are-equivalent | PYTHON || ONE LINE SOLUTION || 93% FASTER CODE | balbinilya | 0 | 3 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,032 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2742073/Python-oror-three-lines-oror | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
s1,s2= "",""
for i in word1: s1 = s1 + i
for i in word2: s2 = s2 + i
return s1==s2 | check-if-two-string-arrays-are-equivalent | ✅ Python || three lines || | Marie-99 | 0 | 7 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,033 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2741975/Generate-Yield-Simple-Approach-or-Python | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
for c1, c2 in zip(self.charWord(word1),self.charWord(word2)):
if c1 != c2:
return False
return True
def charWord(self,wordList):
for segment in wordList:
for ch in segment:
yield ch
yield None | check-if-two-string-arrays-are-equivalent | Generate - Yield Simple Approach | Python | Abhi_-_- | 0 | 7 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,034 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2741975/Generate-Yield-Simple-Approach-or-Python | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return all(c1 == c2 for c1, c2 in zip(self.charWord(word1), self.charWord(word2)))
def charWord(self,wordList):
for segment in wordList:
for ch in segment:
yield ch
yield None | check-if-two-string-arrays-are-equivalent | Generate - Yield Simple Approach | Python | Abhi_-_- | 0 | 7 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,035 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2741975/Generate-Yield-Simple-Approach-or-Python | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return ''.join(word1) == ''.join(word2) | check-if-two-string-arrays-are-equivalent | Generate - Yield Simple Approach | Python | Abhi_-_- | 0 | 7 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,036 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2741881/1662.-Check-If-Two-String-Arrays-are-Equivalent(easy-python-solution) | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
i,j = 0,0
s1 = ""
s2 = ""
while i < len(word1):
s1 += word1.pop(0)
while j<len(word2):
s2 += word2.pop(0)
if s1 == s2:
return True
else:
return False | check-if-two-string-arrays-are-equivalent | 1662. Check If Two String Arrays are Equivalent(easy python solution) | Aritra_Sen2000 | 0 | 3 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,037 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2741864/One-line-python-Solution | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return "".join(word1)=="".join(word2) | check-if-two-string-arrays-are-equivalent | One line python Solution | VijayGupta09 | 0 | 2 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,038 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2741805/Python3-One-liner | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return ''.join(word1) == ''.join(word2) | check-if-two-string-arrays-are-equivalent | Python3 One liner | karinadelcheva | 0 | 1 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,039 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2741703/Simple-Python-Code | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
str1 = ""
str2 = ""
for i in word1:
str1 = str1+i
for i in word2:
str2 = str2+i
return str1 == str2 | check-if-two-string-arrays-are-equivalent | Simple Python Code | dnvavinash | 0 | 2 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,040 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2741617/One-Line-Python-Solution | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return (''.join(word1) == ''.join(word2)) | check-if-two-string-arrays-are-equivalent | One Line Python Solution | mansoorafzal | 0 | 4 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,041 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2741603/Python-(Faster-than-99.9)-or-One-liner | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return ''.join(word1) == ''.join(word2) | check-if-two-string-arrays-are-equivalent | Python (Faster than 99.9%) | One-liner | KevinJM17 | 0 | 6 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,042 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2741570/My-Python-Simple-Solution-or-Beginner's-Friendly | class Solution(object):
def arrayStringsAreEqual(self, word1, word2):
a, b = '', ''
for ch in word1: a += ch
for ch in word2: b += ch
return a == b | check-if-two-string-arrays-are-equivalent | My Python Simple Solution | Beginner's Friendly | its_krish_here | 0 | 1 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,043 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2741496/Python-O(n)-solution-Accepted | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return "".join(word1) == "".join(word2) | check-if-two-string-arrays-are-equivalent | Python O(n) solution [Accepted] | lllchak | 0 | 1 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 24,044 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1871793/Python3-GREEDY-FILLING-()-Explained | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
res, k, i = ['a'] * n, k - n, n - 1
while k:
k += 1
if k/26 >= 1:
res[i], k, i = 'z', k - 26, i - 1
else:
res[i], k = chr(k + 96), 0
return ''.join(res) | smallest-string-with-a-given-numeric-value | ✔️ [Python3] GREEDY FILLING (🌸¬‿¬), Explained | artod | 56 | 2,900 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,045 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1038948/Python.-3-lines.-faster-than-100.00.-cool-and-clear-solution. | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
z = (k - n) // 25
unique = chr(k - z * 25 - n + 97) if n - z else ""
return "a"*(n-z-1) + unique + "z"*z | smallest-string-with-a-given-numeric-value | Python. 3 lines. faster than 100.00%. cool & clear solution. | m-d-f | 4 | 413 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,046 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1873379/Python-Solution | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
ans = ''
while (n - 1) * 26 >= k:
ans += 'a'
n -= 1; k -= 1
ans += chr(ord('a') + (k % 26 or 26) - 1)
ans += 'z' * (n - 1)
return ans | smallest-string-with-a-given-numeric-value | ✅ Python Solution | dhananjay79 | 3 | 69 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,047 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1871949/Python3-O(n)-time-and-O(1)-space-complexity-solution | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
result =[0]*n
for position in range(n-1,-1,-1):
add = min(k -position,26)
result[position] = chr(ord("a")+add -1)
k-=add
return "".join(result) | smallest-string-with-a-given-numeric-value | [Python3] O(n) time and O(1) space complexity solution | bhushan-dhumal | 3 | 167 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,048 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/944693/Python-3-or-5-line-Greedy-O(N)-or-Explanation | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
ans = ['a'] * n
k, i = k-n, n-1
z, nz = divmod(k, 25) # `z`: number of *z* I need, `nz`: ascii of the letter just to cover the leftover
ans[n-1-z] = chr(nz + ord('a')) # adjust the left over `k` using mod
return ''.join(ans[:n-z]) + 'z' * z # make final string & append `z` to the end | smallest-string-with-a-given-numeric-value | Python 3 | 5-line Greedy O(N) | Explanation | idontknoooo | 3 | 192 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,049 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1872162/Greedy-Python-Solution-with-Explanation | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
offset, arr = 96, []
for i in range(n):
j = 1
# Meaning that if we consider the current char to be 'a' then the remaining
# number of numeric value k cannot be divided into required number of 'z'
if 26*(n-i-1) < k-j:
for j in range(1, 27): # Check which middle character to take
if j > k or 26*(n-i-1) >= k-j: # If remaining number of k cannot be divided into n-i-1 groups of 'z'(26)
break
k -= j
arr.append(chr(j+offset))
# Since latest element is not 'a', we most probably found the middle character('b' to 'y')
if arr[-1] != 'a' and k > 0:
if k < 26: # If k < 26 then we cannot fill in 'z'
arr.append(chr(k+offset))
else: # If k > 26 then we can fill k//2 number of 'z'
arr.append('z' * (k // 26))
break # All k have been expired by this point. Thus break and return
return ''.join(arr) | smallest-string-with-a-given-numeric-value | Greedy Python Solution with Explanation | anCoderr | 2 | 148 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,050 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1871726/python-3-oror-O(n) | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
res = ''
for i in range(n):
q, r = divmod(k, 26)
if r == 0:
q -= 1
r = 26
if n - q - i >= 2:
res += 'a'
k -= 1
else:
res += chr(r + 96) + 'z' * (n - i - 1)
break
return res | smallest-string-with-a-given-numeric-value | python 3 || O(n) | dereky4 | 2 | 157 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,051 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1872251/Easy-Python3-Solution-oror-With-Explanantion | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
dp = ["a"] * n #1
val = n #2
i = n-1 #3
while i >= 0 and val < k :
x = (k-val) #4
if x > 25 : #4
dp[i] = chr(122)
val += 25
else :
dp[i] = chr(x+97) #4
val+=x
i-=1
if val == k : #5
break
return "".join(dp) | smallest-string-with-a-given-numeric-value | 🕸️🕸️✅✅ Easy Python3 Solution || With Explanantion | Into_You | 1 | 65 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,052 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1872129/Python-or-Greedy-Solution | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
# Build a list to store the characters
smallest_string_list = ["a" for _ in range(n)]
# Traverse from right to left using greedy approach
for position in range(n, 0, -1):
# Assign the slot with the maximum value that can be occupied
if k - position >= 26:
smallest_string_list[position - 1] = "z"
k -= 26
else:
smallest_string_list[position - 1] = chr(k - position + ord("a"))
k -= k - position + 1
# Convert the list to a string and return
return "".join(smallest_string_list) | smallest-string-with-a-given-numeric-value | Python | Greedy Solution | aravindsb | 1 | 24 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,053 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1871869/Python-3-Two-liner-O(N)-using-Math-with-Explanation-(No-Loop) | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
l = math.ceil((k - n) / 25)
return (n - l) * 'a' + chr(k - (n - l) - (l - 1) * 26 + ord('a') - 1) + (l - 1) * 'z' | smallest-string-with-a-given-numeric-value | Python 3 Two-liner O(N) using Math with Explanation (No Loop) | xil899 | 1 | 25 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,054 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1327319/Python-solution-in-O(1)-time | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
d = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
x = (k-n)//25
y = (k-n)%25
if y == 0 and x != 0:
return 'a'*(n-x) + 'z' * x
else:
return 'a'*(n-x-1) + d[y+1] + 'z'*(x) | smallest-string-with-a-given-numeric-value | Python solution in O(1) time | EklavyaJoshi | 1 | 67 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,055 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1091951/Python-greedy | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
# Edge case
k -= n
if k == 0:
return "a"*n
# idx -> char
mapping = {i+1 : c for i, c in enumerate("abcdefghijklmnopqrstuvwxyz")}
# Temp variables
result = ["a"]*n
idx = -1
# Greedily subtract
while k != 0:
k += 1 # We add one because we are replace the "a", so we need extra value to compensate
key = min(k, 26)
letter = mapping[key]
result[idx] = letter
idx -= 1
k -= key
# Final result
return "".join(result) | smallest-string-with-a-given-numeric-value | Python greedy | dev-josh | 1 | 127 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,056 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1039840/Python-%2B-explanation | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
res = [] # use list because it is faster to concatenate it
# than create new string each time adding character
# while we still got to add some characters
while n > 0:
diff = n * 26 - k
# if every character should have code 26. That is they all are 'z'
if diff == 0:
# extend same as append but can add multiple values
res.extend('z' * n)
break
# don't really need to care character with which code to use
# so make smallest possible
elif diff > 25:
let = 'a'
k -= 1
else:
code = ord('a') + 26 - diff - 1
let = chr(code)
k -= 26 - diff
res.append(let)
n -= 1
return ''.join(res) | smallest-string-with-a-given-numeric-value | Python + explanation | MariaMozgunova | 1 | 82 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,057 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/944647/Python-Easy-Solution | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
k-=n
l=['a']*n
for i in range(n-1,-1,-1):
v=min(k,25)
l[i]=chr(v+97)
k-=v
return ''.join(l) | smallest-string-with-a-given-numeric-value | Python Easy Solution | lokeshsenthilkumar | 1 | 139 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,058 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/2740421/Python-O(n)-math-solution-explained | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
k -= n
t = k // 25
r = k % 25
if t < n:
ans = (n - t - 1) * 'a' + chr(r + 97) + t * 'z'
else:
ans = n * 'z'
return ans | smallest-string-with-a-given-numeric-value | [Python] O(n) math solution, explained | Valjeanzz | 0 | 2 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,059 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/2676540/Simple-Python-Solution-or-Greedy-or-TC%3A-O(n) | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
s=['a']*n
value=n
i=n-1
while value<k:
if k-value-1>=25:
value+=26-1
s[i]='z'
else:
val=k-value
# print(val)
char=chr(ord('a')+val)
value+=k-1
s[i]=char
i-=1
return ''.join(s) | smallest-string-with-a-given-numeric-value | Simple Python Solution | Greedy | TC: O(n) | Siddharth_singh | 0 | 1 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,060 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/2135418/Python-greedy-from-the-back | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
char = {i + 1: c for i, c in enumerate(string.ascii_lowercase)}
result = ['a'] * n
idx = n - 1
to_assign = k - n
while to_assign > 0:
value = min(25, to_assign)
result[idx] = char[value+1]
to_assign -= value
idx -= 1
return "".join(result) | smallest-string-with-a-given-numeric-value | Python, greedy from the back | blue_sky5 | 0 | 14 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,061 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1876978/Python-Solution-76-faster | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
res = n * ["a"]
k = k - n
i = n - 1
while i >= 0:
if k > 25:
k = k - 25
res[i] = 'z'
i -= 1
else:
res[i] = chr(97+k)
break
return "".join(res) | smallest-string-with-a-given-numeric-value | Python Solution 76% faster | pradeep288 | 0 | 21 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,062 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1873287/pythonor2approachorbrute-forceoroptimaloreasy-to-understand | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
ans=["a"] * n
k-=n #k=27-3=24
i=-1
while k>25:
k-=25
ans[i]="z"
i-=1
ans[i]=chr(k+97)
return "".join(ans) | smallest-string-with-a-given-numeric-value | python|2approach|brute force|optimal|easy to understand | YaBhiThikHai | 0 | 18 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,063 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1873287/pythonor2approachorbrute-forceoroptimaloreasy-to-understand | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
q,r=divmod(k-n,25)
return ("a"*(n-q-1) + chr(97+r) + "z"*q if r!=0 else "a"*(n-q)+ "z"*q) | smallest-string-with-a-given-numeric-value | python|2approach|brute force|optimal|easy to understand | YaBhiThikHai | 0 | 18 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,064 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1873203/Python-or-Simple-solution | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
i = 0
s = ""
res = 26*n - k
while i<n:
num = 26
if res - 25>=0:
num = 1
res-=25
else:
while num>1 and res>0:
num-=1
res-=1
i+=1
s+=chr(96+num)
return s | smallest-string-with-a-given-numeric-value | Python | Simple solution | zouhair11elhadi | 0 | 13 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,065 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1873158/Python3-simple-solution | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
solution = ["a" for k in range((n))]
k-=n
cur_char = n - 1
while k > 0:
val = min(k, 25)
solution[cur_char] = chr(ord(solution[cur_char]) + val)
k-=val
if val == 25:
cur_char-=1
return "".join(solution) | smallest-string-with-a-given-numeric-value | Python3 simple solution | alessiogatto | 0 | 11 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,066 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1872954/Python-beginner-friendly-solution | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
ref = {i+1: c for i, c in enumerate('abcdefghijklmnopqrstuvwxyz')}
res = ''
for i in range(1, n):
if self.checkRemaining(n-i, k-1):
res += ref[1]
k -= 1
else:
val = self.getMinVal(n-i, k)
res += ref[val]
k -= val
res += ref[k]
return res
def checkRemaining(self, n, k):
if k / n <= 26:
return True
return False
def getMinVal(self, n, k):
return k - 26*n | smallest-string-with-a-given-numeric-value | Python beginner-friendly solution | Mowei | 0 | 9 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,067 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1872801/Python3-solution-or-Easy-to-understand!!-or-Using-while-loop | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
ans=['a']*n
k = k-n
i=n-1
while k>0:
if k<26:
ans[i] = chr(97+k)
break
else:
ans[i] = 'z'
k-=25
i-=1
return ''.join(ans) | smallest-string-with-a-given-numeric-value | Python3 solution | Easy to understand!! | Using while loop | mrprashantkumar | 0 | 12 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,068 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1872729/Python-O(n)-solutions | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
res = [0] * n
for i in range(n):
j = min(26, k - (n - i - 1))
k -= j
res[n-i-1] = chr(j + 96)
return ''.join(res) | smallest-string-with-a-given-numeric-value | Python O(n) solutions | atiq1589 | 0 | 13 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,069 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1872108/Python-Solution-or-Greedy-or-With-Explanation-or-Easy | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
result = ""
# build from right to left
for i in range(n-1, -1, -1):
# k-i: we must leave some value from k, that is, 1 numeric value for each remaining position
# 26: the value of 'z'
add = min(k-i, 26)
result += chr(add -1 + ord('a'))
k -= add
return result[::-1] | smallest-string-with-a-given-numeric-value | Python Solution | Greedy | With Explanation | Easy | Mikey98 | 0 | 19 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,070 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1872094/Python-oror-Time-O(n)-oror-Space-O(1)-oror-Explained-oror-Easy-To-understand | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
#In ans list I will store the result in reverse order.
#if result = "aaszz", then ans = ['z', 'z', 's', 'a', 'a']
ans = []
for i in range(n):
# If the value is >= 26, then max we can take 26
# 26 means 'z'
if k - (n - i - 1) >= 26:
ans.append('z')
k -= 26
# If less than 26, then take that value
else:
ans.append(chr(k - (n - i - 1) + 96))
k -= k - (n - i - 1)
# The ans list in reverse order.
# So, I need to reverse this
ans.reverse()
return "".join(ans) | smallest-string-with-a-given-numeric-value | ✅ Python || Time = O(n) || Space = O(1) || Explained || Easy To understand | tusharkanti2001maji | 0 | 11 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,071 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1872035/Easy-and-Faster-Python-Solution-Using-Greedy-Approach | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
d={1:'a',2:'b',3:'c',4:'d',5:'e',6:'f',7:'g',8:'h',9:'i',10:'j',
11:'k',12:'l',13:'m',14:'n',15:'o',16:'p',17:'q',18:'r',19:'s',20:'t',
21:'u',22:'v',23:'w',24:'x',25:'y',26:'z'}
result = ''
current_char = 26
while n > 0 :
while current_char > 0 :
if ( k - current_char ) >= n - 1 :
result = result + d[current_char]
k = k - current_char
break
current_char = current_char - 1
n = n - 1
return result[::-1] | smallest-string-with-a-given-numeric-value | Easy and Faster Python Solution Using Greedy Approach ✔✔👍🔥 | ASHOK_KUMAR_MEGHVANSHI | 0 | 17 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,072 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1871987/Python3-or-Greedy-or-Recursive-and-Iterative-or-Easy | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
result = []
remaining = n
number = k
while remaining > 0:
if remaining == 1:
result.append(chr(number + 96))
else:
maximum_can_use = min(26, number - remaining + 1)
result.append(chr(maximum_can_use + 96))
number -= maximum_can_use
remaining -= 1
return ''.join(result[::-1]) | smallest-string-with-a-given-numeric-value | Python3 | Greedy | Recursive & Iterative | Easy | suhrid | 0 | 21 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,073 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1871974/Python3-98.29-or-Greedy-O(N)-or-Elegant-Implementation | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
arr = [1] * n
i = len(arr) - 1
sum_ = n
while k - sum_ != 0:
if k - sum_ > 25:
arr[i] += 25
sum_ += 25
i -= 1
else:
arr[i] += k - sum_
break
# print(arr)
arr = [chr(i+96) for i in arr[:]]
return ''.join(arr) | smallest-string-with-a-given-numeric-value | Python3 98.29% | Greedy O(N) | Elegant Implementation | doneowth | 0 | 9 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,074 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1871950/6-line-greedy-filling-in-Python | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
ans, a = ['a'] * n, ord('a')
for i in range(n - 1, -1, -1):
v = min(k - i, 26)
ans[i] = chr(a + v - 1)
k -= v
return "".join(ans) | smallest-string-with-a-given-numeric-value | 6-line greedy filling in Python | mousun224 | 0 | 14 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,075 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1871926/2-line-Python-(95-46ms)-with-proof-and-explanation | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
c = min((k - n) // 25, n-1)
return 'a'*(n - c - 1) + chr(k - (n - c - 1) - 26*c - 1 + ord('a')) + 'z'*c | smallest-string-with-a-given-numeric-value | 2-line Python (95% 46ms) with proof and explanation | dwen9 | 0 | 14 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,076 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1871884/Python3-Greedy-O(N)-with-comments | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
rem = k
ret = [None]*n
for i in range(n):
# greedy: push "a" if we still have enough characters for remainder
if (n-i-1)*26 >= rem-1:
ret[i] = "a"
rem -= 1
# the post fix is one transition character and then all "z"
else:
# (26-1) % 26 = 25
idx = (rem-1) % 26
ret[i] = string.ascii_lowercase[idx]
rem -= idx + 1
return "".join(ret) | smallest-string-with-a-given-numeric-value | [Python3] Greedy O(N) with comments | oyqian | 0 | 15 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,077 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1871851/Python-Simple-Beginner-Friendly-oror-O(n)-oror-Greedy | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
ans = [0]*n
for i in range(n-1, -1, -1):
val = min(26, k - i)
ans[i] = chr(97+val-1)
k -= val
return ''.join(ans) | smallest-string-with-a-given-numeric-value | ☑ Python Simple Beginner Friendly || O(n) || Greedy | 57Nights | 0 | 23 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,078 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1871797/PHP-fast-non-iterative-mathy-solution-(still-O(n))-with-explanation-also-python3 | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
# The string will have:
# 0+ a's followed by
# 0 or 1 of a letter between a and z followed by
# 0+ z's
#
# To get the lexicographically smallest string we try to maximize the # of a's.
# To maximize the a's we need to pack as much value as possible into the middle letter and z portion of the string.
#
# The extra (non-a) value of a string we need to fill is the amount k exceeds n.
#
# Math stuff:
# a = # of a's
# z = # of z's
# r = remaining letter value
#
# An all 'a' string has a value of n. To get the value up to k, replace 'a's with 'z's, adding 25 each time until
# the value is up to k. If there is a remainder, that is where r is used.
#
# z = floor((k-n)/25)
# r = k-n-25z
#
# a = n-(z+1) or n-z
z = math.floor((k-n)/25)
remaining = (k-n)-25*z
a = n-z - (1 if remaining > 0 else 0)
ltr = '' if remaining == 0 else (chr(ord('a')+remaining))
str = 'a'*a + ltr + 'z'*z
return str | smallest-string-with-a-given-numeric-value | PHP fast non-iterative mathy solution (still O(n)) with explanation - also python3 | crankyinmv | 0 | 19 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,079 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1871767/Python-two-solutions-one-without-arrays-an-the-other-with-arrays-70-ms-faster-than-83.87 | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
if k/26 ==n
return "z"*n
k =k-n
number_of_z,notz = divmod(k,25)
return "a"*(n-number_of_z-1) + chr(notz+ord('a')) + "z"*number_of_z | smallest-string-with-a-given-numeric-value | Python, two solutions, one without arrays an the other with arrays 70 ms, faster than 83.87% | carlosurteaga | 0 | 16 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,080 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1871767/Python-two-solutions-one-without-arrays-an-the-other-with-arrays-70-ms-faster-than-83.87 | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
# create an array equal to n, I wan n letters
arr_response = [1]*n
# remainder
k =k-n
#idx
i = n-1
# avoid calculation each time
ascci_a = ord('a')-1
# start iterating from the lasted to the first
while k and i>-1:
# if k lower than 26, we will convertd
if k<26:
arr_response[i] = chr(arr_response[i]+k+ascci_a)
break
else:
#substract a y and convert to z the current position
k+=-25
arr_response[i] = chr(arr_response[i]+25+ascci_a) # 'z'
i+=-1
#convert the first integers, to chars
for j in range(0, i):
arr_response[j] = chr(arr_response[j]+ascci_a)
return "".join(arr_response) | smallest-string-with-a-given-numeric-value | Python, two solutions, one without arrays an the other with arrays 70 ms, faster than 83.87% | carlosurteaga | 0 | 16 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,081 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1805754/python-solution | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
if n == k:
return 'a'*k
c = 1 #c will be the number of 'z's required
while True:
if n - c + (26 * c) >= k:
break
else:
c += 1
if n - c + (26 * c) == k:
return 'a'*(n-c) + 'z'*c #(n-c) is the number of 'a's required
# if the condition is not satisfied by only 'a's and 'z's then a different character in between 'a's and 'z's is required.
d = 1
while True:
if (n-c) + ((c - 1)*26) + (26 - d) == k:
break
else:
d += 1
#26-d will give the numeric value of the character required.
ans = 'a'*(n-c) + (chr(96 + 26 -d)) + 'z'*(c-1)
return ans | smallest-string-with-a-given-numeric-value | python solution | MS1301 | 0 | 26 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,082 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1040024/Python-3-or-Straightforward | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
res = []
while n:
# last (n-1) char needs to be all 'z' to represent the k
if k-(n-1)*26 > 1:
val = (k-(n-1)*26)
# last (n-1) char can represent k-1, current value can be minimum
else:
val = 1
res.append(val)
k -= val
n -= 1
# chr(97) = chr(96+1) = 'a', therefore chr(item+96)
return ''.join([chr(item+96) for item in res]) | smallest-string-with-a-given-numeric-value | Python 3 | Straightforward | geoffrey0104 | 0 | 34 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,083 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1039851/Python3-Loop | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
if k < n:
raise Exception('Shit! Wont work!')
res = [1]*n
rest = k-n
i=n-1
while rest and i>=0:
if rest>=25:
res[i]=26
rest-=25
i-=1
else:
res[i]+=rest
rest = 0
print(res)
return ''.join([chr(96+i) for i in res]) | smallest-string-with-a-given-numeric-value | [Python3] Loop | charlie11 | 0 | 28 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,084 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1038763/O(n)-solution-python3 | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
i=0
r=0
res=""
if(k%26):
r=1
while( (n-i) > (k//26+r) ): #try to take as many "a" as possible.
res+="a"
i+=1
k-=1
if(k%26):
r=1
else:
r=0
if(k%26!=0): #try to take a character between "b" to "y" if it is possible.
res+=chr(96+k%26)
res+="z"*(k//26) # try to take as many "z" as possible.
return res | smallest-string-with-a-given-numeric-value | O(n) solution python3 | _Rehan12 | 0 | 22 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,085 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/947423/Simple-Python-Solution-O(n) | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
s=""
for i in range(n):
s+='a'
k-=n
l=['a']*n
i=n-1
while k>0:
y=min(k,25)
l[i]= chr(ord('a')+y)
k-=y
i-=1
return ''.join(l) | smallest-string-with-a-given-numeric-value | Simple Python Solution- O(n) | Ayu-99 | 0 | 40 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,086 |
https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/discuss/1872396/Python3-Solution-with-using-greedy-approach-%2B-comments | class Solution:
def getSmallestString(self, n: int, k: int) -> str:
# set all 'a' char
res = ['a' for _ in range(n)]
k -= n
idx = n - 1
while k > 0:
k += 1 # return num value of current 'a' char
char_num_val = min(k, 26) # 26 - num value of 'z' char
k -= char_num_val
res[idx] = chr(char_num_val + 97 - 1) # '97' ascii code of 'a', but 1-indexed, so we must remove 1
idx -= 1 # move idx
return ''.join(res) | smallest-string-with-a-given-numeric-value | [Python3] Solution with using greedy approach + comments | maosipov11 | -1 | 8 | smallest string with a given numeric value | 1,663 | 0.668 | Medium | 24,087 |
https://leetcode.com/problems/ways-to-make-a-fair-array/discuss/1775588/WEEB-EXPLAINS-PYTHONC%2B%2B-DPPREFIX-SUM-SOLN | class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
if len(nums) == 1:
return 1
if len(nums) == 2:
return 0
prefixEven = sum(nums[2::2])
prefixOdd = sum(nums[1::2])
result = 0
if prefixEven == prefixOdd and len(set(nums)) == 1:
result += 1
for i in range(1,len(nums)):
if i == 1:
prefixOdd, prefixEven = prefixEven, prefixOdd
if i > 1:
if i % 2 == 0:
prefixEven -= nums[i-1]
prefixEven += nums[i-2]
else:
prefixOdd -= nums[i-1]
prefixOdd += nums[i-2]
if prefixOdd == prefixEven:
result += 1
return result | ways-to-make-a-fair-array | WEEB EXPLAINS PYTHON/C++ DP/PREFIX SUM SOLN | Skywalker5423 | 6 | 264 | ways to make a fair array | 1,664 | 0.635 | Medium | 24,088 |
https://leetcode.com/problems/ways-to-make-a-fair-array/discuss/2844663/DP-one-pass-O(n)-python-simple-solution | class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
odd, even = 0, 0
for i in range(len(nums)):
if i % 2:
odd += nums[i]
else:
even += nums[i]
ret = 0
left_even = left_odd = 0
for i in range(len(nums)):
if not i % 2:
even -= nums[i]
if left_even + odd == left_odd + even:
ret += 1
left_even += nums[i]
else:
odd -= nums[i]
if left_odd + even == left_even + odd:
ret += 1
left_odd += nums[i]
return ret | ways-to-make-a-fair-array | DP one-pass O(n) / python simple solution | Lara_Craft | 0 | 1 | ways to make a fair array | 1,664 | 0.635 | Medium | 24,089 |
https://leetcode.com/problems/ways-to-make-a-fair-array/discuss/2814580/Python-(Simple-Maths) | class Solution:
def waysToMakeFair(self, nums):
ans1, ans2 = sum(nums[0::2]), sum(nums[1::2])
total = toggle = 0
for i in range(len(nums)):
if i%2 == 0:
ans1 -= nums[i]
if ans1 == ans2:
total += 1
ans2 += nums[i]
else:
ans2 -= nums[i]
if ans1 == ans2:
total += 1
ans1 += nums[i]
return total | ways-to-make-a-fair-array | Python (Simple Maths) | rnotappl | 0 | 1 | ways to make a fair array | 1,664 | 0.635 | Medium | 24,090 |
https://leetcode.com/problems/ways-to-make-a-fair-array/discuss/2796320/Python3-or-Prefix-and-Suffix-Sum | class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
n = len(nums)
left = [[] for i in range(n)]
right = [[] for i in range(n)]
even,odd,ans= [0]*3
for i in range(n):
left[i] = [even,odd]
if i % 2 == 0:
even+=nums[i]
else:
odd+=nums[i]
even,odd = 0,0
for i in range(n-1,-1,-1):
right[i] = [even,odd]
if i % 2 == 0:
even+=nums[i]
else:
odd+=nums[i]
for i in range(n):
left_even_sum = left[i][0]
left_odd_sum = left[i][1]
right_even_sum = right[i][0]
right_odd_sum = right[i][1]
if left_even_sum + right_odd_sum == left_odd_sum + right_even_sum:
ans+=1
return ans | ways-to-make-a-fair-array | [Python3] | Prefix and Suffix Sum | swapnilsingh421 | 0 | 8 | ways to make a fair array | 1,664 | 0.635 | Medium | 24,091 |
https://leetcode.com/problems/ways-to-make-a-fair-array/discuss/2741884/Python3-Commented-Prefix-Sum-Solution | class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
# we can make an array where we save the prefix sum
# for every even and odd number
even = 0
odd = 0
# save the prefix sums
for idx, num in enumerate(nums):
# check whether we are odd or even
if idx % 2: # odd
nums[idx] = odd + num
odd = nums[idx]
else:
nums[idx] = even + num
even = nums[idx]
# initialize the result
result = 0
# now delete every single element
for idx, num in enumerate(nums):
if idx % 2: # odd index
# in order to construct the even sum
# we need the following:
#
# The even sum from before this element
# The odd sum from after this element till the end
# the second sum corresponds to current prefix sum
# minus the last odd
even_sum = nums[idx-1] + odd - nums[idx]
# for the odd summ we need the following:
#
# The odd sum from before this element
# and the even sum after this element
# which corresponds to the last even sum
# minus the sum from the element before this
#
# here we need to deal with the special case in
# the beginning, where idx-2 might not be defined
if idx == 1:
odd_sum = even - nums[idx-1]
else:
odd_sum = nums[idx-2] + even - nums[idx-1]
else: # even index
# in order to construct the even sum we need the
# following:
#
# The even sum from before this element
# The pdd sum after this element which can be computed
# using the last odd sum minus the sum right before
#
# we also need to deal with special cases. For the
# zeros element we don't have anything right before
if idx == 0:
even_sum = odd
else:
even_sum = nums[idx-2] + odd - nums[idx-1]
# in order to construct the odd sum we need the
# following:
#
# The odd sum before this element
# The even sum after this element, which can be
# computet with the final even minus the current
# sum
if idx == 0:
odd_sum = even-nums[idx]
else:
odd_sum = nums[idx-1] + even - nums[idx]
# now check whether the sums are equal
if odd_sum == even_sum:
result += 1
return result | ways-to-make-a-fair-array | [Python3] - Commented Prefix-Sum Solution | Lucew | 0 | 1 | ways to make a fair array | 1,664 | 0.635 | Medium | 24,092 |
https://leetcode.com/problems/ways-to-make-a-fair-array/discuss/1884589/Python-easy-to-read-and-understand-or-prefix-sum | class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
n = len(nums)
even, odd = [], []
evensum, oddsum = 0, 0
for i, num in enumerate(nums):
if i % 2 == 0:
evensum += num
else:
oddsum += num
even.append(evensum)
odd.append(oddsum)
# print(even,odd)
ans = 0
for i in range(n):
if i == 0:
evensum = odd[n - 1] - odd[i]
oddsum = even[n - 1] - even[i]
ans += 1 if evensum == oddsum else 0
else:
evensum = even[i - 1] + (odd[n - 1] - odd[i])
oddsum = odd[i - 1] + (even[n - 1] - even[i])
ans += 1 if evensum == oddsum else 0
return ans | ways-to-make-a-fair-array | Python easy to read and understand | prefix-sum | sanial2001 | 0 | 101 | ways to make a fair array | 1,664 | 0.635 | Medium | 24,093 |
https://leetcode.com/problems/ways-to-make-a-fair-array/discuss/1367564/Python3-simple-solution | class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
oddsum = evensum = 0
for i in range(len(nums)):
if i%2 == 0:
evensum += nums[i]
else:
oddsum += nums[i]
even = odd = count = 0
for i in range(len(nums)):
if i % 2 == 0:
a = oddsum - odd + even
b = evensum - even - nums[i] + odd
even += nums[i]
else:
a = oddsum - odd - nums[i] + even
b = evensum - even + odd
odd += nums[i]
if a == b:
count += 1
return count | ways-to-make-a-fair-array | Python3 simple solution | EklavyaJoshi | 0 | 90 | ways to make a fair array | 1,664 | 0.635 | Medium | 24,094 |
https://leetcode.com/problems/ways-to-make-a-fair-array/discuss/1100405/Python3-prefix-sum | class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
odd, even = [0], [0]
for i, x in enumerate(nums):
if i&1: odd.append(odd[-1] + x)
else: even.append(even[-1] + x)
ans = 0
for i in range(len(nums)):
if odd[i//2] + even[-1] - even[i//2+1] == even[(i+1)//2] + odd[-1] - odd[(i+1)//2]: ans += 1
return ans | ways-to-make-a-fair-array | [Python3] prefix sum | ye15 | 0 | 74 | ways to make a fair array | 1,664 | 0.635 | Medium | 24,095 |
https://leetcode.com/problems/ways-to-make-a-fair-array/discuss/1100405/Python3-prefix-sum | class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
prefix = [0]*2
suffix = [sum(nums[::2]), sum(nums[1::2])]
ans = 0
for i, x in enumerate(nums):
suffix[i%2] -= x
if prefix[0] + suffix[1] == prefix[1] + suffix[0]: ans += 1
prefix[i%2] += x
return ans | ways-to-make-a-fair-array | [Python3] prefix sum | ye15 | 0 | 74 | ways to make a fair array | 1,664 | 0.635 | Medium | 24,096 |
https://leetcode.com/problems/ways-to-make-a-fair-array/discuss/1080987/Python-Beats-97-speed-and-99-memory | class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
s1, s2 = sum(nums[::2]), sum(nums[1::2])
ans = 0
# t1 is current odd sum, t2 — even
t1 = t2 = 0
for i in range(len(nums)):
if i & 1:
t2 += nums[i]
# If we remove i-th elem from nums and this elem is even
# new odd sum becomes equal to odd sum to the left from i-th
# plus initial even sum to the right from i-th elem.
# Same approach for new even sum.
if t1 + s2 - t2 == t2 - nums[i] + s1 - t1:
ans += 1
else:
t1 += nums[i]
if t2 + s1 - t1 == t1 - nums[i] + s2 - t2:
ans += 1
return ans | ways-to-make-a-fair-array | [Python] Beats 97% speed and 99% memory | UladzislauB | 0 | 137 | ways to make a fair array | 1,664 | 0.635 | Medium | 24,097 |
https://leetcode.com/problems/ways-to-make-a-fair-array/discuss/970526/Intuitive-approach-with-sliding-window | class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
even_sum = sum([nums[i] for i in range(0, len(nums), 2)])
odd_sum = sum([nums[i] for i in range(1, len(nums), 2)])
'''
nums = [2,1,6,4]
even_sum = 8, odd_sum = 5
front_even_sum = front_odd_sum = 0
i=0, n=2: (even)
current_even_sum = front_even_sum + odd_sum - front_odd_sum
= 0 + 5 - 0 = 5
current_odd_sum = front_odd_sum + even_sum - n - front_even_sum
= 0 + 8 - 2 - 0 = 6
=> not fair (5!=6)
i=1, n=1: (odd)
front_even_sum = 2, front_odd_sum = 0
current_even_sum = front_even_sum + odd_sum - front_odd_sum - n
= 2 + 5 - 0 - 1 = 6
current_odd_sum = front_odd_sum + even_sum - front_even_sum
= 0 + 8 - 2 = 6
=> fair (6=6)
i=2, n=6: (even)
front_even_sum = 2, front_odd_sum = 1
current_even_sum = front_even_sum + odd_sum - front_odd_sum
= 2 + 5 - 1 = 6
current_odd_sum = front_odd_sum + even_sum - n - front_even_sum
= 1 + 8 - 2 - 6 = 1
=> not fair (6!=1)
'''
ans = front_even_sum = front_odd_sum = 0
for i, n in enumerate(nums):
if i % 2 == 0:
# even
current_even_sum = front_even_sum + odd_sum - front_odd_sum
current_odd_sum = front_odd_sum + even_sum - n - front_even_sum
if current_even_sum == current_odd_sum:
ans += 1
front_even_sum += n
else:
# odd
current_even_sum = front_even_sum + odd_sum - front_odd_sum - n
current_odd_sum = front_odd_sum + even_sum - front_even_sum
if current_even_sum == current_odd_sum:
ans += 1
front_odd_sum += n
return ans | ways-to-make-a-fair-array | Intuitive approach with sliding window | puremonkey2001 | 0 | 58 | ways to make a fair array | 1,664 | 0.635 | Medium | 24,098 |
https://leetcode.com/problems/ways-to-make-a-fair-array/discuss/953224/Simple-Python-Prefix-Solution | class Solution:
def waysToMakeFair(self, nums: List[int]) -> int:
even=[]
odd=[]
e=0
o=0
for i in range(len(nums)):
if i%2==0:
e+=nums[i]
else:
o+=nums[i]
even.append(e)
odd.append(o)
ans=0
for i in range(len(nums)):
if i%2==0:
e1=even[i]
e1-=nums[i]
e1+= odd[len(nums)-1]
e1-=odd[i]
o1=odd[i]
o1+=even[len(nums)-1]
o1-=even[i]
if e1==o1:
ans+=1
else:
o1=odd[i]
o1-=nums[i]
o1+=even[len(nums)-1]
o1-=even[i]
e1=even[i]
e1+=odd[len(nums)-1]
e1-=odd[i]
if o1==e1:
ans+=1
return ans | ways-to-make-a-fair-array | Simple Python Prefix Solution | Ayu-99 | 0 | 70 | ways to make a fair array | 1,664 | 0.635 | Medium | 24,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.