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/find-first-palindromic-string-in-the-array/discuss/2714488/Python-Solution-oror-Easy-to-understand
class Solution: def firstPalindrome(self, words: List[str]) -> str: for st in words: rev = st[::-1] if st==rev: return st return ""
find-first-palindromic-string-in-the-array
Python Solution || Easy to understand
Ram06
0
3
find first palindromic string in the array
2,108
0.786
Easy
29,100
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2677359/PYTHON-or-4-Lines-or-Beginner-or-Easy-or-O(n)
class Solution: def firstPalindrome(self, words: List[str]) -> str: for i in words: x= i[::-1] if i == x: return i return ""
find-first-palindromic-string-in-the-array
PYTHON | 4 Lines | Beginner | Easy | O(n)
envyTheClouds
0
12
find first palindromic string in the array
2,108
0.786
Easy
29,101
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2676498/Python-or-Easyor-Faster-than-93.04
class Solution: def firstPalindrome(self, words: List[str]) -> str: for i in words: if self.palindromChecker(i): return i return "" def palindromChecker(self,word): lPtr = 0 rPtr = len(word)-1 while lPtr<rPtr: if word[lPtr]==word[rPtr]: lPtr += 1 rPtr -= 1 else: return False return True
find-first-palindromic-string-in-the-array
Python | Easy| Faster than 93.04%
tanaydwivedi095
0
2
find first palindromic string in the array
2,108
0.786
Easy
29,102
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2657746/Python-oror-Easily-Understood-oror-Faster-than-96-oror
class Solution: def firstPalindrome(self, words: List[str]) -> str: ans = '' for i in words: if i==i[::-1]: ans+=i break return ans
find-first-palindromic-string-in-the-array
🔥 Python || Easily Understood ✅ || Faster than 96% ||
rajukommula
0
10
find first palindromic string in the array
2,108
0.786
Easy
29,103
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2656888/first-palindromic-string
class Solution: def firstPalindrome(self, words: List[str]) -> str: d={} first=1 for i in words: reverseword=''.join(reversed(i)) if i==reverseword: d[i]=first first+=1 else: pass for key,val in d.items(): if val==1: return key return ''
find-first-palindromic-string-in-the-array
first palindromic string
shivansh2001sri
0
2
find first palindromic string in the array
2,108
0.786
Easy
29,104
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2651061/Simple-and-Easy-to-Understand-or-Python
class Solution(object): def firstPalindrome(self, words): def pali(s, n): l, r = 0, n - 1 while r > l: if s[r] != s[l]: return False l += 1 r -= 1 return True for word in words: if pali(word, len(word)): return word return ''
find-first-palindromic-string-in-the-array
Simple and Easy to Understand | Python
its_krish_here
0
11
find first palindromic string in the array
2,108
0.786
Easy
29,105
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2631685/Basic-Python-Solution
class Solution: def firstPalindrome(self, words: List[str]) -> str: def isPal(s): return s == s[::-1] ans="" for i in words: if isPal(i): ans=i break return ans
find-first-palindromic-string-in-the-array
Basic Python Solution
beingab329
0
6
find first palindromic string in the array
2,108
0.786
Easy
29,106
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2604623/Python-Understandable-and-Readable-Solutions
class Solution: def firstPalindrome(self, words: List[str]) -> str: for word in words: if word == word[::-1]: return word return ""
find-first-palindromic-string-in-the-array
[Python] - Understandable and Readable Solutions
Lucew
0
14
find first palindromic string in the array
2,108
0.786
Easy
29,107
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2604623/Python-Understandable-and-Readable-Solutions
class Solution: def firstPalindrome(self, words: List[str]) -> str: for word in words: # return word if the suffix matches the prefix if all(word[idx] == word[-idx-1] for idx in range(len(word)//2)) or len(word)==1: return word return ""
find-first-palindromic-string-in-the-array
[Python] - Understandable and Readable Solutions
Lucew
0
14
find first palindromic string in the array
2,108
0.786
Easy
29,108
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2604623/Python-Understandable-and-Readable-Solutions
class Solution: def firstPalindrome(self, words: List[str]) -> str: for word in words: # get half the word length word_length = len(word)//2 # check whether the word has a lenght of one if not word_length: return word # make a flag that tells us whether the word is a palindrome palindrome = True # check each letter from the beginning and the end for idx in range(word_length): if word[idx] != word[-idx-1]: palindrome = False break # if flag is still up we return the word if palindrome: return word return ""
find-first-palindromic-string-in-the-array
[Python] - Understandable and Readable Solutions
Lucew
0
14
find first palindromic string in the array
2,108
0.786
Easy
29,109
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2489208/Simple-beginner-friendly-solution
class Solution: def isPalindrome(self,s): l = 0 r = len(s)-1 while l < r: if s[l] != s[r]: return False break l += 1 r -= 1 return True def firstPalindrome(self, words: List[str]) -> str: for word in words: if self.isPalindrome(word) == True: return word break return ""
find-first-palindromic-string-in-the-array
Simple beginner friendly solution
aruj900
0
21
find first palindromic string in the array
2,108
0.786
Easy
29,110
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2424306/Solution-(97-Faster)
class Solution: def firstPalindrome(self, words: List[str]) -> str: for i in words: if i == i[::-1]: words = i return words return ""
find-first-palindromic-string-in-the-array
Solution (97 % Faster)
fiqbal997
0
41
find first palindromic string in the array
2,108
0.786
Easy
29,111
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2421677/Python-solution-Faster-than-86.25
class Solution: def firstPalindrome(self, words: List[str]) -> str: first_pal_word = "" for word in words: if word[::-1] == word[:]: first_pal_word = word break if len(first_pal_word) == 0: return "" return first_pal_word
find-first-palindromic-string-in-the-array
Python solution Faster than 86.25 %
samanehghafouri
0
8
find first palindromic string in the array
2,108
0.786
Easy
29,112
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2411106/Simple-python-code-with-explanation
class Solution: def firstPalindrome(self, words: List[str]) -> str: #create a empty string res = "" #iterate over the elements in the words-->list for i in range(len(words)): #if the element is same when it is reversed if words[i] == words[i][::-1]: #then return that element return words[i] #if not return empty string return res
find-first-palindromic-string-in-the-array
Simple python code with explanation
thomanani
0
7
find first palindromic string in the array
2,108
0.786
Easy
29,113
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2360870/Find-First-Palindromic-String-in-the-Array
class Solution: def palindrome(self, word) -> bool: l = 0 r = len(word)-1 while l<r : if word[l]==word[r]: l+=1 r-=1 else : return False return True def firstPalindrome(self, words: List[str]) -> str: for i in words : x=self.palindrome(i) if x : return i return ""
find-first-palindromic-string-in-the-array
Find First Palindromic String in the Array
dhananjayaduttmishra
0
6
find first palindromic string in the array
2,108
0.786
Easy
29,114
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2122972/Pyhton3-Solution-easily-explained.
class Solution: def firstPalindrome(self, words: List[str]) -> str: # we'd go through every word, check if it's palindrome and return that word # and we don't have to go through other words once we find the first palindromic word # and if we don't find any palindromic word, we'd return an empty string for word in words: if word == word[::-1]: return word return ""
find-first-palindromic-string-in-the-array
Pyhton3 Solution, easily explained.
shubhamdraj
0
55
find first palindromic string in the array
2,108
0.786
Easy
29,115
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2110263/Easy-Python-Solution-or-Faster-than-78
class Solution: def firstPalindrome(self, words: List[str]) -> str: res ="" for word in words: if word == word[::-1]: res = word break return res
find-first-palindromic-string-in-the-array
Easy Python Solution | Faster than 78%
nikhitamore
0
56
find first palindromic string in the array
2,108
0.786
Easy
29,116
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2099814/Python-simple-solution
class Solution: def firstPalindrome(self, words: List[str]) -> str: for i in words: if i == i[::-1]: return i return ""
find-first-palindromic-string-in-the-array
Python simple solution
StikS32
0
42
find first palindromic string in the array
2,108
0.786
Easy
29,117
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/2010466/python3-Simple-Solution
class Solution: def firstPalindrome(self, words: List[str]) -> str: for w in words: if w == w[::-1]: return w return ''
find-first-palindromic-string-in-the-array
[python3] Simple Solution
terrencetang
0
26
find first palindromic string in the array
2,108
0.786
Easy
29,118
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1940850/Python-dollarolution
class Solution: def firstPalindrome(self, words: List[str]) -> str: for i in words: if i == i[::-1]: return i return ''
find-first-palindromic-string-in-the-array
Python $olution
AakRay
0
28
find first palindromic string in the array
2,108
0.786
Easy
29,119
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1908682/Python3-or-Simple
class Solution: def firstPalindrome(self, words: List[str]) -> str: def isPalindrome(word): word = list(word) while len(word) > 1: if word.pop() != word.pop(0): return False return True for word in words: if isPalindrome(word): return word return ""
find-first-palindromic-string-in-the-array
Python3 | Simple
user0270as
0
28
find first palindromic string in the array
2,108
0.786
Easy
29,120
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1908584/WEEB-DOES-PYTHONC%2B%2B-2-POINTER-SOLUTION
class Solution: def firstPalindrome(self, words: List[str]) -> str: for word in words: low = 0 high = len(word)-1 flag = False while low < high: if word[low] != word[high]: flag = True break high-=1 low+=1 if not flag: return word return ""
find-first-palindromic-string-in-the-array
WEEB DOES PYTHON/C++ 2 POINTER SOLUTION
Skywalker5423
0
63
find first palindromic string in the array
2,108
0.786
Easy
29,121
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1862058/Python-(Simple-Approach-and-Beginner-Friendly)
class Solution: def firstPalindrome(self, words: List[str]) -> str: ans = "" for i in words: stringlength=len(i) slicedString=i[stringlength::-1] if i == slicedString: return i return ""
find-first-palindromic-string-in-the-array
Python (Simple Approach and Beginner-Friendly)
vishvavariya
0
50
find first palindromic string in the array
2,108
0.786
Easy
29,122
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1829941/Py-2-ways-stack-and-extra-function
class Solution: def isItPalindrome(self, element): """this will return true/false if element is palindromic""" return_bool = True n = int(len(element)/2) for i in range(0,n): if element[i] == element[-1-i]: pass else: return_bool = False break return return_bool def firstPalindrome(self, words: List[str]) -> str: for each in words: if self.isItPalindrome(each): return each break return ''
find-first-palindromic-string-in-the-array
[Py] 2 ways - stack and extra function
ankit61d
0
28
find first palindromic string in the array
2,108
0.786
Easy
29,123
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1744469/Python3-or-faster-than-93.30-of-Python3
class Solution: def firstPalindrome(self, words: List[str]) -> str: for word in words: if word == word[::-1]: return word return ""
find-first-palindromic-string-in-the-array
Python3 | faster than 93.30% of Python3
khalidhassan3011
0
89
find first palindromic string in the array
2,108
0.786
Easy
29,124
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1697490/Python3-accepted-solution
class Solution: def firstPalindrome(self, words: List[str]) -> str: for i in words: if(i==i[::-1]): return i return ""
find-first-palindromic-string-in-the-array
Python3 accepted solution
sreeleetcode19
0
55
find first palindromic string in the array
2,108
0.786
Easy
29,125
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1671825/Python3-Memory-Less-Than-99.38
class Solution: def firstPalindrome(self, words: List[str]) -> str: def check(s): i, j = 0, len(s) - 1 while i < j: if s[i] == s[j]: i, j = i + 1, j - 1 else: return False return True for i in words: if check(i): return i return ""
find-first-palindromic-string-in-the-array
Python3 Memory Less Than 99.38%
Hejita
0
47
find first palindromic string in the array
2,108
0.786
Easy
29,126
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1637957/Python-simplest-solution
class Solution: def firstPalindrome(self, words: List[str]) -> str: for word in words: if word == word[::-1]: return word return ""
find-first-palindromic-string-in-the-array
Python simplest solution
byuns9334
0
70
find first palindromic string in the array
2,108
0.786
Easy
29,127
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1635488/python-easy-solution
class Solution: def firstPalindrome(self, words: List[str]) -> str: def isPalindrome(word): l, r = 0, len(word)-1 while l < r: if word[l] != word[r]: return False l += 1; r -= 1 return True res = "" for word in words: if isPalindrome(word): return word return res
find-first-palindromic-string-in-the-array
python easy solution
abkc1221
0
60
find first palindromic string in the array
2,108
0.786
Easy
29,128
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1635201/Python-straightforward
class Solution: def firstPalindrome(self, words: List[str]) -> str: for word in words: if word == word[::-1]: return word return ""
find-first-palindromic-string-in-the-array
Python, straightforward
blue_sky5
0
89
find first palindromic string in the array
2,108
0.786
Easy
29,129
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1635087/Python3-Solution-Easy
class Solution: def firstPalindrome(self, words: List[str]) -> str: flag=0 for i in words: rev="".join(reversed(i)) if rev==i: flag=1 break if flag==1: return rev else: return ""
find-first-palindromic-string-in-the-array
Python3 Solution Easy
anshikajaiswal2000
0
55
find first palindromic string in the array
2,108
0.786
Easy
29,130
https://leetcode.com/problems/find-first-palindromic-string-in-the-array/discuss/1635012/Python-Solution
class Solution: def firstPalindrome(self, words: List[str]) -> str: for word in words: if word == word[::-1]: return word return ""
find-first-palindromic-string-in-the-array
Python Solution
pratushah
0
83
find first palindromic string in the array
2,108
0.786
Easy
29,131
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635075/Python-Split-and-Join-to-the-rescue.-From-TLE-to-Accepted-!-Straightforward
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: arr = [] prev = 0 for space in spaces: arr.append(s[prev:space]) prev = space arr.append(s[space:]) return " ".join(arr)
adding-spaces-to-a-string
[Python] Split and Join to the rescue. From TLE to Accepted ! Straightforward
mostlyAditya
8
388
adding spaces to a string
2,109
0.563
Medium
29,132
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1695213/**-Python-code%3A-Adding-Spaces-to-a-String
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: arr=[] prev=0 for i in spaces: arr.append(s[prev:i]) prev=i arr.append(s[i:]) return " ".join(arr)
adding-spaces-to-a-string
** Python code: Adding Spaces to a String
Anilchouhan181
6
214
adding spaces to a string
2,109
0.563
Medium
29,133
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635095/Python3-forward-and-backward
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: ans = [] j = 0 for i, ch in enumerate(s): if j < len(spaces) and i == spaces[j]: ans.append(' ') j += 1 ans.append(ch) return ''.join(ans)
adding-spaces-to-a-string
[Python3] forward & backward
ye15
5
480
adding spaces to a string
2,109
0.563
Medium
29,134
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635095/Python3-forward-and-backward
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: ans = [] for i in reversed(range(len(s))): ans.append(s[i]) if spaces and spaces[-1] == i: ans.append(' ') spaces.pop() return "".join(reversed(ans))
adding-spaces-to-a-string
[Python3] forward & backward
ye15
5
480
adding spaces to a string
2,109
0.563
Medium
29,135
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635392/Fastest-Python-Solution-with-comments-or-O(n)
class Solution: def addSpaces(self, st: str, sp: List[int]) -> str: ret = [st[:sp[0]]] # writing the initial part of the string (from index 0 to the 1st space) to the variable for i in range(1, len(sp)): # going throught each space ret.append(' ') # adding space at the position ret.append(st[sp[i-1]:sp[i]]) # writing the next part of the string, which does not have spaces in between ret.append(' ') ret.append(st[sp[-1]:]) # writing the last part of string to the variable return ''.join(ret) # converting the contents of list to string
adding-spaces-to-a-string
Fastest Python Solution with comments | O(n)
the_sky_high
2
186
adding spaces to a string
2,109
0.563
Medium
29,136
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635392/Fastest-Python-Solution-with-comments-or-O(n)
class Solution: def addSpaces(self, st: str, sp: List[int]) -> str: ret = st[:sp[0]] # writing the initial part of the string (from index 0 to the 1st space) to the variable for i in range(1, len(sp)): # going through each space ret += ' ' # adding space at the position ret += st[sp[i - 1]:sp[i]] # writing the next part of the string, which does not have spaces in between ret += ' ' ret += st[sp[-1]:] # writing the last part of string to the variable return ret
adding-spaces-to-a-string
Fastest Python Solution with comments | O(n)
the_sky_high
2
186
adding spaces to a string
2,109
0.563
Medium
29,137
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635392/Fastest-Python-Solution-with-comments-or-O(n)
class Solution: def addSpaces(self, st: str, sp: List[int]) -> str: ret = st[:sp[0]] for i in range(1, len(sp)): ret = ret + ' ' + st[sp[i-1]:sp[i]] ret = ret + ' ' + st[sp[-1]:] return ret
adding-spaces-to-a-string
Fastest Python Solution with comments | O(n)
the_sky_high
2
186
adding spaces to a string
2,109
0.563
Medium
29,138
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1746778/Python-RunTime%3A-843ms-53.97-Memory%3A-52.2mb-27.97
class Solution: def addSpaces(self, string: str, spaces: List[int]) -> str: if not string and not spaces:return string res = str() spaces = set(spaces) for i in range(len(string)): if i in spaces: res += " " res += string[i] return res
adding-spaces-to-a-string
Python RunTime: 843ms 53.97% Memory: 52.2mb 27.97%
arshergon
1
75
adding spaces to a string
2,109
0.563
Medium
29,139
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635224/Python-oror-Easy-Solution
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: a="" j=0 for i in range(len(s)): if(j<=len(spaces)-1 and i==spaces[j]): a+=" " j+=1 a+=s[i] return a
adding-spaces-to-a-string
Python || Easy Solution
HimanshuGupta_p1
1
54
adding spaces to a string
2,109
0.563
Medium
29,140
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/2836222/Python3-easy-to-understand
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: res = '' DQ = deque(spaces) next = DQ.popleft() for i in range(len(s)): if next == i: res += ' ' + s[i] if len(DQ) != 0: next = DQ.popleft() else: res = res + s[i] return res
adding-spaces-to-a-string
Python3 - easy to understand
mediocre-coder
0
2
adding spaces to a string
2,109
0.563
Medium
29,141
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/2832614/Python-Easy-80-Runtime-Solution-oror-O(n)-Solution
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: res = '' j = 0 x = len(spaces) for i in range(len(s)): if j > x - 1: return res + s[i:] elif i == spaces[j]: res += ' ' j += 1 res += s[i] return res
adding-spaces-to-a-string
Python Easy 80% Runtime Solution || O(n) Solution
darsigangothri0698
0
1
adding spaces to a string
2,109
0.563
Medium
29,142
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/2728418/1-pass-solution-without-extra-set
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: strs = "" j = 0 for i in range(len(s)): if j < len(spaces) and i == spaces[j]: strs += " " j+=1 strs += s[i] return strs
adding-spaces-to-a-string
1 pass solution without extra set
stanleyyuen_pang
0
1
adding spaces to a string
2,109
0.563
Medium
29,143
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/2690920/Python3-Concise-and-readable-Solution
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: # make a set of spaces for fast look up spaces = set(spaces) # go over every character result = [] for idx, char in enumerate(s): if idx in spaces: result.append(" ") result.append(char) return "".join(result)
adding-spaces-to-a-string
[Python3] - Concise and readable Solution
Lucew
0
3
adding spaces to a string
2,109
0.563
Medium
29,144
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/2462777/Python-simple-and-clean-solution-O(n)-or-2-liner
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: sp=[0] + spaces + [len(s)] return " ".join([s[start:end] for start,end in zip(sp,sp[1:])])
adding-spaces-to-a-string
Python-simple-and-clean-solution O(n) | 2 liner
YaBhiThikHai
0
28
adding spaces to a string
2,109
0.563
Medium
29,145
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/2455878/TLE-solved-!!-Easy-Python-solution
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: lst=list(s) for i in range(0,len(spaces)): lst.insert(i+spaces[i]," ") l="".join(lst) return l
adding-spaces-to-a-string
TLE solved !! Easy Python solution
keertika27
0
19
adding spaces to a string
2,109
0.563
Medium
29,146
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/2455878/TLE-solved-!!-Easy-Python-solution
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: lst = [] start = 0 for i in spaces: lst.append(s[start:i]) start = i lst.append(s[start:]) return " ".join(lst)
adding-spaces-to-a-string
TLE solved !! Easy Python solution
keertika27
0
19
adding spaces to a string
2,109
0.563
Medium
29,147
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1984823/Multiple-logic-or-TLE-reported-logics-or-94-Faster-solution-or-Multiple-accepted-solution
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: ```
adding-spaces-to-a-string
Multiple logic | TLE reported logics | 94% Faster solution | Multiple accepted solution
abuzarmd
0
31
adding spaces to a string
2,109
0.563
Medium
29,148
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1699443/Python3-Solution-oror-Use-enumerate-oror-Two-ways
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: res_str= "" set1 = set(spaces) for x, char in enumerate(s): if x in set1: res_str += " " res_str += char return res_str
adding-spaces-to-a-string
[Python3] Solution || Use enumerate || Two ways
Cheems_Coder
0
27
adding spaces to a string
2,109
0.563
Medium
29,149
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1699443/Python3-Solution-oror-Use-enumerate-oror-Two-ways
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: res_str= "" set1 = set(spaces) for i in range(len(s)): if i in set1: res_str += " " res_str += s[i] return res_str
adding-spaces-to-a-string
[Python3] Solution || Use enumerate || Two ways
Cheems_Coder
0
27
adding spaces to a string
2,109
0.563
Medium
29,150
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1697479/Python3-accepted-solution
class Solution: def addSpaces(self, s: str, li: List[int]) -> str: ans = "" counter = 0 for i in li: ans += (s[counter:i])+" " counter = i ans += (s[i:]) return ans
adding-spaces-to-a-string
Python3 accepted solution
sreeleetcode19
0
34
adding spaces to a string
2,109
0.563
Medium
29,151
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1694526/Python3-or-four-liners-or-99.6-faster-or-simple-no-substrings
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: arr = [*s] for i in spaces: arr[i] = " "+arr[i] return ''.join(arr)
adding-spaces-to-a-string
Python3 | four liners | 99.6% faster | simple, no substrings
Xascoria2
0
67
adding spaces to a string
2,109
0.563
Medium
29,152
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1640539/Python-3-very-easy-solution
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: n_spaces = len(spaces) space = 0 res = '' for i, c in enumerate(s): if space < n_spaces and spaces[space] == i: res += ' ' space += 1 res += c return res
adding-spaces-to-a-string
Python 3 very easy solution
dereky4
0
113
adding spaces to a string
2,109
0.563
Medium
29,153
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1637277/100-faster-python-solution
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: new_string = "" a = set(spaces) for i in range(len(s)): if i in a: new_string += " " new_string += s[i] return new_string
adding-spaces-to-a-string
100% faster python solution
loltk
0
66
adding spaces to a string
2,109
0.563
Medium
29,154
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1636163/Python-3-(Adding-Spaces-to-a-String)%3A-faster-than-100
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: if len(s) == 1: return " " + s if len(spaces) == 1: return s[:spaces[0]] + " " + s[spaces[0]:] answer = s[: spaces[0]] + " " for i in range(1, len(spaces)): left, right = spaces[i - 1], spaces[i] answer += s[left:right] + " " answer += s[right:] return answer
adding-spaces-to-a-string
Python 3 (Adding Spaces to a String): faster than 100%
artur_temievich
0
47
adding spaces to a string
2,109
0.563
Medium
29,155
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635805/python3-or-faster-than-83.33-or-time-O(n)-or-space-O(n)
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: string="" count=0 for i in range(len(s)): if count<len(spaces) and i==spaces[count]: string+=" " count+=1 string+=s[i] return string
adding-spaces-to-a-string
python3 | faster than 83.33% | time-O(n) | space-O(n)
Rohit_Patil
0
29
adding spaces to a string
2,109
0.563
Medium
29,156
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635640/Join-substrings-100-speed
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: if len(spaces) == 1: return " ".join([s[:spaces[0]], s[spaces[0]:]]) return f"{s[:spaces[0]]} {' '.join(s[start: end] for start, end in zip(spaces, spaces[1:]))} {s[spaces[-1]:]}"
adding-spaces-to-a-string
Join substrings, 100% speed
EvgenySH
0
28
adding spaces to a string
2,109
0.563
Medium
29,157
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635491/python-solution
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: s = list(s) n = len(spaces) res = [] start, end = 0, spaces[0] for i in range(n): res.append(''.join(s[start: end])) start = end if i < n-1: end = spaces[i+1] return ' '.join(res) + " " + ''.join(s[end:]) # concat the end part of the string
adding-spaces-to-a-string
python solution
abkc1221
0
40
adding spaces to a string
2,109
0.563
Medium
29,158
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635371/python-straight-forward
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: ans = [] word = '' space, curr = 0, 0 while curr < len(s): if curr != spaces[space]: word += s[curr] else: ans.append(word) word = s[curr] if space < len(spaces)-1: space += 1 else: word += s[curr+1:] break curr += 1 ans.append(word) return ' '.join(ans)
adding-spaces-to-a-string
python straight forward
Alvinyoo
0
29
adding spaces to a string
2,109
0.563
Medium
29,159
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635251/Simple-Python-Code-or-Set
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: spaces = set(spaces) res = "" for i, c in enumerate(s): if i in spaces: res += " " res += c return res
adding-spaces-to-a-string
Simple Python Code | Set
GigaMoksh
0
31
adding spaces to a string
2,109
0.563
Medium
29,160
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635213/Python-Easy-join-solution
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: ans = [] prev = 0 for sp in spaces: ans.append(s[prev:sp]) prev = sp ans.append(s[prev:]) return " ".join(ans)
adding-spaces-to-a-string
[Python] Easy join solution
nightybear
0
31
adding spaces to a string
2,109
0.563
Medium
29,161
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635066/Python-Solution
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: result,hset = "",set(spaces) for i,char in enumerate(s): if i in hset: result += " " result += char return result
adding-spaces-to-a-string
Python Solution
pratushah
0
61
adding spaces to a string
2,109
0.563
Medium
29,162
https://leetcode.com/problems/adding-spaces-to-a-string/discuss/1635194/Python-Easiest-Solution-All-test-cases-passed
class Solution: def addSpaces(self, s: str, spaces: List[int]) -> str: outpur="" rest=0 for k in range(len(s)): if rest<len(spaces) and k == spaces[rest]: outpur=outpur+" " rest=rest+1 outpur=outpur+s[k] return(outpur)
adding-spaces-to-a-string
Python Easiest Solution All test cases passed
grraghav
-1
44
adding spaces to a string
2,109
0.563
Medium
29,163
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1635103/Python3-counting
class Solution: def getDescentPeriods(self, prices: List[int]) -> int: ans = 0 for i, x in enumerate(prices): if i == 0 or prices[i-1] != x + 1: cnt = 0 cnt += 1 ans += cnt return ans
number-of-smooth-descent-periods-of-a-stock
[Python3] counting
ye15
6
279
number of smooth descent periods of a stock
2,110
0.576
Medium
29,164
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1635091/Python-Solution
class Solution: def getDescentPeriods(self, prices: List[int]) -> int: result,count = 0,1 for i in range(1,len(prices)): if prices[i] + 1 == prices[i-1]: count += 1 else: result += (count * (count+1))//2 count = 1 result += (count * (count+1))//2 return result
number-of-smooth-descent-periods-of-a-stock
Python Solution
pratushah
1
53
number of smooth descent periods of a stock
2,110
0.576
Medium
29,165
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/2270341/Easy-Python-with-Math
class Solution: def getDescentPeriods(self, prices: List[int]) -> int: seq, tot = 1, 0 for i in range(1, len(prices)): if prices[i] - prices[i-1] == -1: seq += 1 else: tot += seq*(seq+1)/2 seq = 1 return int(tot + seq*(seq+1)/2) ```
number-of-smooth-descent-periods-of-a-stock
Easy Python with Math
TheKivs
0
13
number of smooth descent periods of a stock
2,110
0.576
Medium
29,166
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1851267/WEEB-DOES-PYTHONC%2B%2B-DP-SOLUTION
class Solution: def getDescentPeriods(self, nums: List[int]) -> int: prev, result = 1, 1 for i in range(1,len(nums)): if nums[i] == nums[i-1]-1: result += prev + 1 prev += 1 else: result += 1 prev = 1 return result
number-of-smooth-descent-periods-of-a-stock
WEEB DOES PYTHON/C++ DP SOLUTION
Skywalker5423
0
52
number of smooth descent periods of a stock
2,110
0.576
Medium
29,167
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1752730/Python-Solution-using-hints
class Solution: def getDescentPeriods(self, prices: List[int]) -> int: #initiate res which will store the subsequent sum, # and count which will keep a count of adjacent values who have a difference on '1' # (Basically keeping in mind that we need to find the longest possible period) res,count = 0,1 for i in range(1,len(prices)): if prices[i] + 1 == prices[i-1]: count += 1 #Hint 2 being used else: res += (count * (count+1))//2 #Finding that formula, a bit of brainstorming helps reach this count = 1 #resetting count as the next longest subbarrays with each subsequent elements that differ by '1' will be counted again from the beginning res += (count * (count+1))//2 return res
number-of-smooth-descent-periods-of-a-stock
Python Solution using hints
kindahodor
0
22
number of smooth descent periods of a stock
2,110
0.576
Medium
29,168
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1734959/python3-sliding-window-solution
class Solution: def getDescentPeriods(self, prices: List[int]) -> int: left=0 ans=1 for right in range(1,len(prices)): if prices[right]!=prices[right-1]-1: left=right ans+=(right-left+1) return ans
number-of-smooth-descent-periods-of-a-stock
python3 sliding window solution
Karna61814
0
25
number of smooth descent periods of a stock
2,110
0.576
Medium
29,169
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1648384/One-pass-with-zip-92-speed
class Solution: def getDescentPeriods(self, prices: List[int]) -> int: ans, count = 0, 1 for a, b in zip(prices, prices[1:]): if a == b + 1: count += 1 else: ans += count * (1 + count) // 2 count = 1 return ans + count * (1 + count) // 2
number-of-smooth-descent-periods-of-a-stock
One pass with zip, 92% speed
EvgenySH
0
52
number of smooth descent periods of a stock
2,110
0.576
Medium
29,170
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1635935/pyhthon3-or-Recursive-%2B-DP-approach
class Solution: def __init__(self): self.count=0 def getDescentPeriods(self, prices: List[int]) -> int: self.rec(prices,0,[]) return self.count def rec(self,prices,i,op): if i==len(prices): return if len(op)==0: op+=[prices[i]] self.count+=1 self.rec(prices,i+1,op) self.rec(prices,i+1,[]) else: if op[-1]-prices[i]==1: self.count+=1 op+=[prices[i]] self.rec(prices,i+1,op)
number-of-smooth-descent-periods-of-a-stock
pyhthon3 | Recursive + DP approach
Rohit_Patil
0
55
number of smooth descent periods of a stock
2,110
0.576
Medium
29,171
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1635935/pyhthon3-or-Recursive-%2B-DP-approach
class Solution: def getDescentPeriods(self, prices: List[int]) -> int: dp=[0 for i in range(len(prices))] dp[0]=1 for i in range(1,len(prices)): if prices[i]+1==prices[i-1]: # check if last element is greater than 1 by current element dp[i]+=dp[i-1] # add the answer from prev element dp[i]+=1 #always add 1 bcz a single element is counted return sum(dp)
number-of-smooth-descent-periods-of-a-stock
pyhthon3 | Recursive + DP approach
Rohit_Patil
0
55
number of smooth descent periods of a stock
2,110
0.576
Medium
29,172
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1635585/JavaPython3-DP-oror-O(n)-oror-Simple-Solution
class Solution: def getDescentPeriods(self, prices: List[int]) -> int: n = len(prices) dp = [0] * n dp[0] = 1 for i in range(1,n): if prices[i-1] - prices[i] == 1: dp[i] = dp[i-1] + 1 else: dp[i] = 1 return sum(dp)
number-of-smooth-descent-periods-of-a-stock
[Java/Python3] DP || O(n) || Simple Solution
abhijeetmallick29
0
21
number of smooth descent periods of a stock
2,110
0.576
Medium
29,173
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1635555/python-greedy-solution
class Solution: def getDescentPeriods(self, prices: List[int]) -> int: n = len(prices) res = n # for each element in array i = 0 while i < n-1: k = 1 # length of contiguous chunk of smooth descent while i < n-1 and prices[i] - prices[i+1] == 1: k += 1 i += 1 i += 1 res += (k*(k-1)//2) # in every contiguous chunk count smaller chunks return res
number-of-smooth-descent-periods-of-a-stock
python greedy solution
abkc1221
0
29
number of smooth descent periods of a stock
2,110
0.576
Medium
29,174
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1635552/Python3-one-liner
class Solution: def getDescentPeriods(self, prices: List[int]) -> int: return sum((lambda sum_: sum_*(sum_+1)//2)(sum(1 for item in items)) for i,items in itertools.groupby(i+p for i,p in enumerate(prices)))
number-of-smooth-descent-periods-of-a-stock
Python3 one-liner
pknoe3lh
0
17
number of smooth descent periods of a stock
2,110
0.576
Medium
29,175
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1635210/Python-Easy-DP-solution
class Solution: def getDescentPeriods(self, prices: List[int]) -> int: n = len(prices) dp = [0]*(n) dp[0] = 1 # dp[i] += dp[i-1] for i in range(1, n): dp[i] = 1 if prices[i] == prices[i-1] -1: dp[i] += dp[i-1] return sum(dp)
number-of-smooth-descent-periods-of-a-stock
[Python] Easy DP solution
nightybear
0
23
number of smooth descent periods of a stock
2,110
0.576
Medium
29,176
https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/discuss/1635128/Very-Simple-Python3-Solution
class Solution: def getDescentPeriods(self, prices: List[int]) -> int: lastPrice = None counter = 0 seen = 0 for price in prices: if lastPrice and lastPrice - price == 1: counter += 1 + seen seen += 1 else: seen = 0 counter += 1 lastPrice = price return counter
number-of-smooth-descent-periods-of-a-stock
Very Simple Python3 Solution
tyrocoder
0
46
number of smooth descent periods of a stock
2,110
0.576
Medium
29,177
https://leetcode.com/problems/minimum-operations-to-make-the-array-k-increasing/discuss/1635109/Python3-almost-LIS
class Solution: def kIncreasing(self, arr: List[int], k: int) -> int: def fn(sub): """Return ops to make sub non-decreasing.""" vals = [] for x in sub: k = bisect_right(vals, x) if k == len(vals): vals.append(x) else: vals[k] = x return len(sub) - len(vals) return sum(fn(arr[i:len(arr):k]) for i in range(k))
minimum-operations-to-make-the-array-k-increasing
[Python3] almost LIS
ye15
2
177
minimum operations to make the array k increasing
2,111
0.378
Hard
29,178
https://leetcode.com/problems/minimum-operations-to-make-the-array-k-increasing/discuss/1635109/Python3-almost-LIS
class Solution: def kIncreasing(self, arr: List[int], k: int) -> int: ans = 0 for _ in range(k): vals = [] for i in range(_, len(arr), k): if not vals or vals[-1] <= arr[i]: vals.append(arr[i]) else: vals[bisect_right(vals, arr[i])] = arr[i] ans += len(vals) return len(arr) - ans
minimum-operations-to-make-the-array-k-increasing
[Python3] almost LIS
ye15
2
177
minimum operations to make the array k increasing
2,111
0.378
Hard
29,179
https://leetcode.com/problems/minimum-operations-to-make-the-array-k-increasing/discuss/2621662/Short-and-easy-LIS-solution-in-Python-with-explanation
class Solution: def lis(self, start): results = [] total = 0 for i in range(start, len(self.arr), self.k): total += 1 if not results or results[-1] <= self.arr[i]: results.append(self.arr[i]) else: results[bisect_right(results, self.arr[i])] = self.arr[i] return total - len(results) def kIncreasing(self, arr: List[int], k: int) -> int: self.arr, self.k = arr, k return sum(self.lis(i) for i in range(k))
minimum-operations-to-make-the-array-k-increasing
Short and easy LIS solution in Python with explanation
metaphysicalist
0
7
minimum operations to make the array k increasing
2,111
0.378
Hard
29,180
https://leetcode.com/problems/minimum-operations-to-make-the-array-k-increasing/discuss/2396115/Python-from-scratch
class Solution: def kIncreasing(self, arr: List[int], k: int) -> int: def LIS(nums): res = [] for i in nums: low,high = 0, len(res) while low < high: mid = (low+high)//2 if res[mid] <= i: low = mid + 1 else: high = mid if low == len(res): res.append(i) else: res[low] = i return len(res) ans = 0 for i in range(k): bucket = [] startingIdx = i while startingIdx < len(arr): bucket.append(arr[startingIdx]) startingIdx += k ans += len(bucket) - LIS(bucket) return ans ```
minimum-operations-to-make-the-array-k-increasing
Python from scratch
mrPython
0
60
minimum operations to make the array k increasing
2,111
0.378
Hard
29,181
https://leetcode.com/problems/minimum-operations-to-make-the-array-k-increasing/discuss/2280832/binary-search-easy
class Solution: def LIS(self, arr): n = len(arr) dp = ['None']*n dp[0] = arr[0] j= 0 for i in range(1,n): if arr[i]>=dp[j]: dp[j+1] = arr[i] j+=1 else: idx = bisect.bisect(dp,arr[i],0,j) # log(n) dp[idx] = arr[i] count = 0 for i in dp: if i=='None': break count+=1 return count def kIncreasing(self, arr: List[int], k: int) -> int: ans = 0 n = len(arr) for i in range(k): nums = [] for j in range(i,n,k): nums.append(arr[j]) ans+=(len(nums)-self.LIS(nums)) return ans
minimum-operations-to-make-the-array-k-increasing
binary search easy
Abhi_009
0
74
minimum operations to make the array k increasing
2,111
0.378
Hard
29,182
https://leetcode.com/problems/minimum-operations-to-make-the-array-k-increasing/discuss/1635216/Python-Easy-LIS-solution
class Solution: def kIncreasing(self, arr: List[int], k: int) -> int: n = len(arr) def lis(nums): ''' Patience sort Time: O(NlogN) Space: O(N) ''' n = len(nums) if n <= 1: return n # num, pointer c = [] size = 0 for x in nums: # bisect as it is non-decreasing l = bisect.bisect_right(c, x) if l < size: c[l] = x else: c.append(x) size = max(size, l+1) return size ans = 0 for i in range(k): sub = [] for j in range(i, n, k): sub.append(arr[j]) ans += len(sub) - lis(sub) return ans
minimum-operations-to-make-the-array-k-increasing
[Python] Easy LIS solution
nightybear
0
94
minimum operations to make the array k increasing
2,111
0.378
Hard
29,183
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/1646786/Count-Spaces
class Solution: def mostWordsFound(self, ss: List[str]) -> int: return max(s.count(" ") for s in ss) + 1
maximum-number-of-words-found-in-sentences
Count Spaces
votrubac
64
8,100
maximum number of words found in sentences
2,114
0.88
Easy
29,184
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/1646692/Python3-One-Liner-and-Simple-and-Easy-2-solutions
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: mx=0 for i in sentences: c=i.split() if len(c)>mx: mx=len(c) return mx
maximum-number-of-words-found-in-sentences
[Python3] One-Liner and Simple and Easy - 2 solutions
ro_hit2013
6
618
maximum number of words found in sentences
2,114
0.88
Easy
29,185
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/1646692/Python3-One-Liner-and-Simple-and-Easy-2-solutions
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: return max([len(i.split()) for i in sentences])
maximum-number-of-words-found-in-sentences
[Python3] One-Liner and Simple and Easy - 2 solutions
ro_hit2013
6
618
maximum number of words found in sentences
2,114
0.88
Easy
29,186
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/2716894/python-code
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: ans=0 for i in sentences: ans=max(ans,i.count(' ')) return ans+1
maximum-number-of-words-found-in-sentences
python code
ayushigupta2409
5
428
maximum number of words found in sentences
2,114
0.88
Easy
29,187
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/2060780/Python-simple-solution
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: a=[] for i in sentences: count=0 for j in i: if(j==" "): count+=1 count+=1 a.append(count) return (max(a))
maximum-number-of-words-found-in-sentences
Python {simple} solution
tusharkhanna575
5
165
maximum number of words found in sentences
2,114
0.88
Easy
29,188
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/2802005/Python3-or-99-Faster-O(1)-Space
class Solution: def mostWordsFound(self, s: List[str]) -> int: # TC: O(N) || SC: O(1) ans = 0 for i in range(len(s)): s[i] = s[i].count(' ') + 1 ans = max(ans, s[i]) return ans
maximum-number-of-words-found-in-sentences
Python3 | 99% Faster, O(1) Space
ZetaRising
3
291
maximum number of words found in sentences
2,114
0.88
Easy
29,189
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/2659030/Python3-solution.-Clean-code-with-comments.-98.69-run-time-82.79-space.
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: # Description state that the words are separated only by 'space'. SPACE = ' ' max_words = 0 list_of_words = [] for string in sentences: # We will take each sentence and split it into words. word_counter = string.split(SPACE) # Then we place the words into a list. list_of_words += word_counter # Now we will check the length of the list against the current max_words. max_words = max(max_words, len(list_of_words)) # After that, we clear the list for the next iteration. list_of_words.clear() return max_words # Runtime: 37 ms, faster than 98.69% of Python3 online submissions # for Maximum Number of Words Found in Sentences. # Memory Usage: 13.9 MB, less than 82.79% of Python3 online submissions # for Maximum Number of Words Found in Sentences. # If you like my solution, then I'll appreciate # a like. It's helping me make more solutions.
maximum-number-of-words-found-in-sentences
Python3 solution. Clean code with comments. 98.69% run time, 82.79% space.
375d
3
193
maximum number of words found in sentences
2,114
0.88
Easy
29,190
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/2060686/Python-easy-solution-~-Beats-93.21
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: arr = [] for i in sentences: a = i.split(" ") arr.append(len(a)) return max(arr)
maximum-number-of-words-found-in-sentences
Python easy solution ~ Beats 93.21%
Shivam_Raj_Sharma
3
181
maximum number of words found in sentences
2,114
0.88
Easy
29,191
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/2119659/5-Lines-of-Python-code-with-for-Loop
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: ma = 0 for i in sentences: s = i.split() ma = max(len(s), ma) return ma
maximum-number-of-words-found-in-sentences
5 Lines of Python code with for Loop
prernaarora221
2
122
maximum number of words found in sentences
2,114
0.88
Easy
29,192
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/2398665/Simple-python-code-with-explanation
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: #let the ans-> variable be 0 ans = 0 #iterate through the strings in list (sentences) for i in sentences: #count the no. of spaces k = i.count(" ") #update the ans val ans = max(ans,k) #return the ans + 1 because (no.of words in sentences = no.of spaces + 1) return ans + 1
maximum-number-of-words-found-in-sentences
Simple python code with explanation
thomanani
1
49
maximum number of words found in sentences
2,114
0.88
Easy
29,193
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/1933806/Python-Easy-Solution-or-Faster-or-Optimal
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: maxNum = 0 for i in sentences: words = i.split(' ') if len(words) > maxNum: maxNum = len(words) return maxNum
maximum-number-of-words-found-in-sentences
[Python] Easy Solution | Faster | Optimal
jamil117
1
95
maximum number of words found in sentences
2,114
0.88
Easy
29,194
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/1739089/Python3-simple-solution
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: count = 0 for i in range(len(sentences)): count = max(count, len(sentences[i].split())) return count
maximum-number-of-words-found-in-sentences
Python3 simple solution
EklavyaJoshi
1
82
maximum number of words found in sentences
2,114
0.88
Easy
29,195
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/1732157/python-solution-with-Time-complexity-O(n)-and-Space-Complexity-O(n)
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: len_word = [] for i in range(len(sentences)): d = sentences[i].split(' ') len_word.append(len(d)) return max(len_word)
maximum-number-of-words-found-in-sentences
python solution with Time complexity - O(n) and Space Complexity - O(n)
vikashjyoti
1
66
maximum number of words found in sentences
2,114
0.88
Easy
29,196
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/1705913/2114-max-words-in-sentences
class Solution(object): def mostWordsFound(self, sentences): """ :type sentences: List[str] :rtype: int """ max=0 for lines in sentences: if max < len(lines.split(' ')): max = len(lines.split(' ')) return max
maximum-number-of-words-found-in-sentences
2114 - max words in sentences
ankit61d
1
143
maximum number of words found in sentences
2,114
0.88
Easy
29,197
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/1697589/Python3-accepted-one-liner-solution
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: return max([len(i.split()) for i in sentences])
maximum-number-of-words-found-in-sentences
Python3 accepted one-liner solution
sreeleetcode19
1
58
maximum number of words found in sentences
2,114
0.88
Easy
29,198
https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/discuss/1646836/Python3-1-line
class Solution: def mostWordsFound(self, sentences: List[str]) -> int: return max(len(x.split()) for x in sentences)
maximum-number-of-words-found-in-sentences
[Python3] 1-line
ye15
1
53
maximum number of words found in sentences
2,114
0.88
Easy
29,199