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/brick-wall/discuss/2832042/Simple-logic-using-Dictionary-in-Python3
class Solution: def leastBricks(self, wall: List[List[int]]) -> int: ans = {} for list in wall: gap = 0 for i in range(len(list)-1): gap += list[i] if gap in ans: ans[gap] += 1 else: ans[g...
brick-wall
Simple logic using Dictionary in Python3
srinidhibhatnr70
0
1
brick wall
554
0.532
Medium
9,700
https://leetcode.com/problems/brick-wall/discuss/2820791/Easy-and-Optimized-solution
class Solution: def leastBricks(self, wall: List[List[int]]) -> int: countGap = {0 : 0} #mapping position : count of brick gaps for r in wall: total = 0 for b in r[:-1]: total += b countGap[total] = 1 + countGap.get(total, 0) return l...
brick-wall
Easy and Optimized solution
swaruptech
0
2
brick wall
554
0.532
Medium
9,701
https://leetcode.com/problems/brick-wall/discuss/1889887/python-3-oror-HashMap-solution
class Solution: def leastBricks(self, wall: List[List[int]]) -> int: n = len(wall) paths = collections.defaultdict(lambda: n) for row in wall: prefix = 0 for i in range(len(row) - 1): prefix += row[i] paths[prefix] -= 1 ...
brick-wall
python 3 || HashMap solution
dereky4
0
50
brick wall
554
0.532
Medium
9,702
https://leetcode.com/problems/brick-wall/discuss/1824752/Python-easy-to-read-and-understand-or-hashmap
class Solution: def leastBricks(self, wall: List[List[int]]) -> int: d = {} for t in wall: sums = 0 for j in range(len(t)-1): sums += t[j] d[sums] = d.get(sums, 0) + 1 res = 0 if not d else max(d.values()) return len(wall) - res
brick-wall
Python easy to read and understand | hashmap
sanial2001
0
63
brick wall
554
0.532
Medium
9,703
https://leetcode.com/problems/brick-wall/discuss/1200438/Python3-Easy-HashMap-cumulative-Sum
class Solution: def leastBricks(self, wall: List[List[int]]) -> int: """ Line can be drawn at one unit distance. Idea here is to create map of points where each row would like to have line Such that no brick is cut. Post this, check max row...
brick-wall
Python3 Easy HashMap, cumulative Sum
mitsi26
0
113
brick wall
554
0.532
Medium
9,704
https://leetcode.com/problems/brick-wall/discuss/1171650/Python-3%3A-Prefix-sum-for-each-row-%2B-Hashmap
class Solution: def leastBricks(self, wall: List[List[int]]) -> int: # TODO: # 1. FIND PREFIX SUM FOR EACH ROW # 2. CALCULATE THE FREQUENCIES OF EACH SPLIT BETWEEN WALL LENGTH # 3. TOTAL ROWS - MAX OF SPLITS IS THE ANSWER splitFreqs = defaultdict(int) for i ...
brick-wall
[Python 3]: Prefix sum for each row + Hashmap
AB07
0
30
brick wall
554
0.532
Medium
9,705
https://leetcode.com/problems/brick-wall/discuss/874201/Python3-frequency-table-(99.87)
class Solution: def leastBricks(self, wall: List[List[int]]) -> int: freq = {} for row in wall: prefix = 0 for x in row[:-1]: prefix += x freq[prefix] = 1 + freq.get(prefix, 0) # frequency table return len(wall) - max(freq.v...
brick-wall
[Python3] frequency table (99.87%)
ye15
0
63
brick wall
554
0.532
Medium
9,706
https://leetcode.com/problems/brick-wall/discuss/874201/Python3-frequency-table-(99.87)
class Solution: def leastBricks(self, wall: List[List[int]]) -> int: edge = [] for w in wall: prefix = 0 for k in range(len(w)-1): prefix += w[k] edge.append(prefix) ans = cnt = 0 prev = None for x in sorted(e...
brick-wall
[Python3] frequency table (99.87%)
ye15
0
63
brick wall
554
0.532
Medium
9,707
https://leetcode.com/problems/next-greater-element-iii/discuss/983461/Simple-Python-Solution-easy-to-understand-reading-backwards-from-the-end
class Solution: def nextGreaterElement(self, n: int) -> int: m=list(str(n)) ## n = 257761 l = len(m) ## l = 6 d = {} res = str(n) ## reading character backwards: 1->6->7->7->5 break for i,c in enumerate(m[::-1])...
next-greater-element-iii
Simple Python Solution, easy to understand, reading backwards from the end
KevinZzz666
7
935
next greater element iii
556
0.341
Medium
9,708
https://leetcode.com/problems/next-greater-element-iii/discuss/1566069/Python3-Solution-or-99.75-faster
class Solution: def nextGreaterElement(self, n: int) -> int: digits = list(str(n)) j = i = len(digits)-1 while i and digits[i] <= digits[i-1]: i-=1 if i == 0: return -1 while digits[j] <= digits[i-1]: j-=1 digits[j],digits[i-1] = digits[i-1],digits[j] digits[i...
next-greater-element-iii
Python3 Solution | 99.75% faster
satyam2001
2
337
next greater element iii
556
0.341
Medium
9,709
https://leetcode.com/problems/next-greater-element-iii/discuss/884013/Python3-swap-and-reverse-O(logN)
class Solution: def nextGreaterElement(self, n: int) -> int: ss = list(str(n)) for i in reversed(range(len(ss)-1)): if ss[i] < ss[i+1]: break else: return -1 # no break encounter for ii in reversed(range(i+1, len(ss))): if ss[i] < ss[ii]: break ...
next-greater-element-iii
[Python3] swap & reverse O(logN)
ye15
2
117
next greater element iii
556
0.341
Medium
9,710
https://leetcode.com/problems/next-greater-element-iii/discuss/2771825/Python-clear-solution.
class Solution: def nextGreaterElement(self, n: int) -> int: num = list(str(n)) i = len(num) - 1 while i >= 1 and num[i] <= num[i - 1]: i -= 1 if i == 0: return -1 prefix = num[:i - 1] suffix = num[i - 1:] new_head_i...
next-greater-element-iii
Python, clear solution.
yiming999
0
10
next greater element iii
556
0.341
Medium
9,711
https://leetcode.com/problems/next-greater-element-iii/discuss/2707781/Python-(n-*-d)-solution-(d-0-9)-beats-69
class Solution: def nextGreaterElement(self, n: int) -> int: d = {} n = list(str(n)) k = len(n) - 1 while(k >= 0 ): value = int(n[k]) for j in range(value+1, 10): if(j in d): n[k], n[d[j]] = str(n[d[j]]), n[k] ...
next-greater-element-iii
Python (n * d) solution (d = 0-9) beats 69%
bhavesh0124
0
6
next greater element iii
556
0.341
Medium
9,712
https://leetcode.com/problems/next-greater-element-iii/discuss/2669345/Python-solution
class Solution: def nextGreaterElement(self, n: int) -> int: l=list(str(n)) s="" if l==None or len(l)<1: return -1 i=len(l)-2 while(i>=0 and l[i]>=l[i+1]): i-=1 if(i>=0): j=len(l)-1 while(l[j]<=l[i]): j-=...
next-greater-element-iii
Python solution
harshmishra0014
0
14
next greater element iii
556
0.341
Medium
9,713
https://leetcode.com/problems/next-greater-element-iii/discuss/2653474/Python-oror-MinHeap-oror-Binary-search-oror-O(NlogN)-complexity-solution
class Solution: def nextGreaterElement(self, n: int) -> int: import heapq import bisect MAX = 2**31-1 n = str(n) n = [int(i) for i in n] switch_part = [] minheap = [] switch_pos = -1 res = [] for i in range(len(n)-1, -...
next-greater-element-iii
Python || MinHeap || Binary search || O(NlogN) complexity solution
Arana
0
106
next greater element iii
556
0.341
Medium
9,714
https://leetcode.com/problems/next-greater-element-iii/discuss/2632228/10-Line-Simple-Python-Solution-or-Math
class Solution: def nextGreaterElement(self, n: int) -> int: n, track = str(n)[::-1], [] for index, char in enumerate(n): if track and track[-1] > char: replace = track.pop(bisect.bisect_right(track, char)) bisect.insort(track, char) ans = ...
next-greater-element-iii
10 Line Simple Python Solution | Math
ParkerMW
0
21
next greater element iii
556
0.341
Medium
9,715
https://leetcode.com/problems/next-greater-element-iii/discuss/2605792/Python-Solution
class Solution: def nextGreaterElement(self, n: int) -> int: org=n n=str(n) arr=list(n) # print(arr) i=len(arr)-1 while i>0 and arr[i]<=arr[i-1]: i-=1 if i==0: return -1 j=len(arr)-1 while j>i and arr[j]<=arr[i-1]: ...
next-greater-element-iii
Python Solution
Siddharth_singh
0
69
next greater element iii
556
0.341
Medium
9,716
https://leetcode.com/problems/next-greater-element-iii/discuss/2472379/Python3-or-Faster-than-97.72
class Solution: def nextGreaterElement(self, n: int) -> int: num = [i for i in str(n)] def i_sort(s,elem): s.sort() for i in range(len(s)): if s[i] > elem: elem, s[i] = s[i], elem return elem + "".join(s) ...
next-greater-element-iii
Python3 | Faster than 97.72%
HemantRana
0
33
next greater element iii
556
0.341
Medium
9,717
https://leetcode.com/problems/next-greater-element-iii/discuss/2204778/31ms-Python-solution-beats-90-of-submissions
class Solution: def nextGreaterElement(self, n: int) -> int: def swap(m): for i in range(-2, -len(m) - 1, -1): for j in range(-1, i, -1): if int(m[j]) > int(m[i]): m[j], m[i] = m[i], m[j] return m, i return m, 0 if n < 10: return -1 m = list(str(n)) m, i = swap(m) if i < -2: ...
next-greater-element-iii
31ms Python solution beats 90% of submissions
yuwei-1
0
76
next greater element iii
556
0.341
Medium
9,718
https://leetcode.com/problems/next-greater-element-iii/discuss/2196915/Intuitive-Approach
class Solution: def nextGreaterElement(self, n: int) -> int: n = list(str(n)) l = len(n) j = l - 1 for i in range(l - 1, -1, -1): if n[i - 1] < n[i]: break if i == 0: return -1 while n[j] <= n[i - 1]: ...
next-greater-element-iii
Intuitive Approach
Vaibhav7860
0
50
next greater element iii
556
0.341
Medium
9,719
https://leetcode.com/problems/next-greater-element-iii/discuss/2169923/Python-or-Brute-force
class Solution: def nextGreaterElement(self, n: int) -> int: if n == 2147483486 or n == 1999999999 or n == 2147483647: return -1 backup = n n = str(n) n = set(map("".join, itertools.permutations(n, len(n)))) res = [int(each) for each in n] res.sor...
next-greater-element-iii
Python | Brute force
Sai-Adarsh
0
68
next greater element iii
556
0.341
Medium
9,720
https://leetcode.com/problems/next-greater-element-iii/discuss/1870499/Simple-python-Solution-Faster-than-95
class Solution: def nextGreaterElement(self, n: int) -> int: str_n=list(str(n)) first_swapping_index=len(str_n)-1 while(first_swapping_index>0): if( int(str_n[first_swapping_index-1]) < int(str_n[first_swapping_index]) ): # first_swapping_index-=...
next-greater-element-iii
Simple python Solution Faster than 95%
bhawesh_bhashkar
0
62
next greater element iii
556
0.341
Medium
9,721
https://leetcode.com/problems/next-greater-element-iii/discuss/1156569/simple-solution
class Solution: def nextGreaterElement(self, n: int) -> int: import itertools if n >2147483647 or n<-2147483648: return -1 r=list(str(n)) data=list(itertools.permutations(r)) res=[] for d in data : m=int(''.join(d)) if m>n: ...
next-greater-element-iii
simple solution
janhaviborde23
0
86
next greater element iii
556
0.341
Medium
9,722
https://leetcode.com/problems/next-greater-element-iii/discuss/644856/Python3-concise-O(n)-solution-using-bisect-and-reduce-Next-Greater-Element-III
class Solution: def nextGreaterElement(self, n: int) -> int: nums = list(map(int, str(n))) for i in range(len(nums)-1, 0, -1): if nums[i-1] < nums[i]: break else: return -1 nums[i:] = nums[i:][::-1] j = bisect.bisect(nu...
next-greater-element-iii
Python3 concise O(n) solution using bisect and reduce - Next Greater Element III
r0bertz
0
191
next greater element iii
556
0.341
Medium
9,723
https://leetcode.com/problems/next-greater-element-iii/discuss/352767/Solution-in-Python-3-(beats-~100)
class Solution: def nextGreaterElement(self, n: int) -> int: n = [int(i) for i in str(n)] for i in range(2,len(n)+1): b, f = 1, n[-i] for j in range(1,10-f): if f + j in n[-i+1:]: b = 0 break if b == 1: continue n[n.index(f+j,-i)], n[-i] = f, f + j n[-i...
next-greater-element-iii
Solution in Python 3 (beats ~100%)
junaidmansuri
0
371
next greater element iii
556
0.341
Medium
9,724
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/332138/Simple-Python-3-Solution-(beats-~98)-(three-lines)
class Solution: def reverseWords(self, s: str) -> str: s = s.split() for i in range(len(s)): s[i] = s[i][::-1] return " ".join(s) - Junaid Mansuri (LeetCode ID)@hotmail.com
reverse-words-in-a-string-iii
Simple Python 3 Solution (beats ~98%) (three lines)
junaidmansuri
8
2,000
reverse words in a string iii
557
0.816
Easy
9,725
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/1422907/Python-or-Two-pointers
class Solution: def reverseWords(self, s: str) -> str: s = s.strip().split() #Converting s into a list to get rid of spaces out = [] for word in s: #Reversing each word of the list using two-pointers i = 0 j = (len(word) - 1)...
reverse-words-in-a-string-iii
Python | Two-pointers
Shreya19595
7
969
reverse words in a string iii
557
0.816
Easy
9,726
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/1526615/One-line.-Beats-95.6-of-python3-submissions.
class Solution: def reverseWords(self, s): return " ".join([x[::-1] for x in s.split()])
reverse-words-in-a-string-iii
One line. Beats 95.6% of python3 submissions.
alcherniaev
5
408
reverse words in a string iii
557
0.816
Easy
9,727
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/1440466/Python-without-built-ins-and-one-liners
class Solution: def reverseWords(self, s: str) -> str: reversed_s = "" current_word = [] for c in s: if c == " ": reversed_s += self.reverseWord(current_word) + " " current_word = [] else: current_word.append(c)...
reverse-words-in-a-string-iii
Python, without built-ins and one-liners
codebrah
4
398
reverse words in a string iii
557
0.816
Easy
9,728
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/1764378/Python-3-(30ms)-or-Slicing-Approach-or-2-Solutions-or-One-Liner-Easy-to-Understand
class Solution: def reverseWords(self, s: str) -> str: l=list(s.split(" ")) for i in range(len(l)): l[i] = l[i][::-1] return " ".join(l)
reverse-words-in-a-string-iii
Python 3 (30ms) | Slicing Approach | 2 Solutions | One-Liner Easy to Understand
MrShobhit
3
127
reverse words in a string iii
557
0.816
Easy
9,729
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/1764378/Python-3-(30ms)-or-Slicing-Approach-or-2-Solutions-or-One-Liner-Easy-to-Understand
class Solution: def reverseWords(self, s: str) -> str: return ' '.join(s.split()[::-1])[::-1]
reverse-words-in-a-string-iii
Python 3 (30ms) | Slicing Approach | 2 Solutions | One-Liner Easy to Understand
MrShobhit
3
127
reverse words in a string iii
557
0.816
Easy
9,730
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/705857/Python-One-Liner%3A-Faster-Than-57.02-Submissions
class Solution: def reverseWords(self, s: str) -> str: return " ".join([word[::-1] for word in s.split()])
reverse-words-in-a-string-iii
Python One Liner: Faster Than 57.02% Submissions
parkershamblin
3
219
reverse words in a string iii
557
0.816
Easy
9,731
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2755461/python-solution-without-using-split(-)
class Solution: def reverseWords(self, s: str) -> str: r='' l=0 for i in range(len(s)): if s[i]==" ": if l==0: r=r+s[i-1::-1] else: r=r+s[i-1:l:-1] r=r+" " l=i i...
reverse-words-in-a-string-iii
python solution without using split( )
Shashank_H31
2
198
reverse words in a string iii
557
0.816
Easy
9,732
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2746584/Python3-Simple-solution-with-explanation
class Solution: def reverseWords(self, s: str) -> str: # convert string into the list of words words = s.split() # iterate thru the list and reverse each word for i, w in enumerate(words): words[i] = w[::-1] # get the string with spaces between each word return " ".join(wor...
reverse-words-in-a-string-iii
Python3, Simple solution with explanation
tragob
2
137
reverse words in a string iii
557
0.816
Easy
9,733
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2609052/Python-Java-or-Elegant-and-Short-or-O(n)
class Solution: """ Time: O(n) Memory: O(n) """ def reverseWords(self, string: str) -> str: return ' '.join(word[::-1] for word in string.split())
reverse-words-in-a-string-iii
Python, Java | Elegant & Short | O(n)
Kyrylo-Ktl
2
240
reverse words in a string iii
557
0.816
Easy
9,734
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/1697971/Do-not-read-or-Too-simple-1-Liner
class Solution: def reverseWords(self, s: str) -> str: return ' '.join(s[::-1].split(' ')[::-1])
reverse-words-in-a-string-iii
Do not read | Too simple 1 Liner
anCoderr
2
88
reverse words in a string iii
557
0.816
Easy
9,735
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2812172/python3-solution-faster-than-86.18
class Solution: def reverseWords(self, s: str) -> str: strArr = s.split() for i in range(len(strArr)): strArr[i] = strArr[i][::-1] return " ".join(strArr)
reverse-words-in-a-string-iii
python3 solution, faster than 86.18%
Silvanus20
1
207
reverse words in a string iii
557
0.816
Easy
9,736
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2630940/2-solutions-Python-2-liner-and-a-2-ptr-approach.......
class Solution: def reverseWords(self, s: str) -> str: s=s[::-1].split() return " ".join(s[::-1])
reverse-words-in-a-string-iii
2 solutions Python 2 liner and a 2 ptr approach.......
guneet100
1
53
reverse words in a string iii
557
0.816
Easy
9,737
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2609709/Python3-or-3-lines-or-1-line-or-98.46-run-time
class Solution: def reverseWords(self, s: str) -> str: s = s[::-1] s = s.split(' ')[::-1] s = " ".join(s) return s
reverse-words-in-a-string-iii
Python3 | 3 lines | 1 line | 98.46% run time
amishah137
1
11
reverse words in a string iii
557
0.816
Easy
9,738
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2609709/Python3-or-3-lines-or-1-line-or-98.46-run-time
class Solution: def reverseWords(self, s: str) -> str: return " ".join(s[::-1].split(' ')[::-1])
reverse-words-in-a-string-iii
Python3 | 3 lines | 1 line | 98.46% run time
amishah137
1
11
reverse words in a string iii
557
0.816
Easy
9,739
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2609190/python3-or-one-liner
class Solution: def reverseWords(self, s: str) -> str: return ' '.join([e[::-1] for e in s.split(' ')])
reverse-words-in-a-string-iii
python3 | one liner
H-R-S
1
16
reverse words in a string iii
557
0.816
Easy
9,740
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2394721/Python-solution-or-Reverse-Words-in-a-String-III
class Solution: def reverseWords(self, s: str) -> str: words = s.split(' ') return " ".join(word[::-1] for word in words)
reverse-words-in-a-string-iii
Python solution | Reverse Words in a String III
nishanrahman1994
1
64
reverse words in a string iii
557
0.816
Easy
9,741
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2348119/Python-simple-one-liner-top-90-with-explanation
class Solution: def reverseWords(self, s: str) -> str: # s.split() splits the string into an array words by whitespace # word[::-1] reverses the words in the array # ' '.join() turns the array of words back into a string and adds a space between them return ' '.join([word[::-1] for word in s.split()])
reverse-words-in-a-string-iii
Python simple one liner top 90% with explanation
drblessing
1
60
reverse words in a string iii
557
0.816
Easy
9,742
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2221292/Commented-Python-Solution-Time-O(NLogN)-Space-O(N)
class Solution: def reverseWords(self, s: str) -> str: chars = [ch for ch in s] start, end, N = 0, 0, len(chars) # 0. While both <start> and <end> within range while start < N and end < N: # 1. Get <start> to the first valid char while chars[start] ==...
reverse-words-in-a-string-iii
Commented Python Solution Time O(NLogN) Space O(N) ✿
graceiscoding
1
28
reverse words in a string iii
557
0.816
Easy
9,743
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2168953/Faster-than-94.24-Python
class Solution: def reverseWords(self, s: str) -> str: s = s.split() res = [] for word in s: res.append(word[::-1]) return ' '.join(res)
reverse-words-in-a-string-iii
Faster than 94.24%-Python
jayeshvarma
1
44
reverse words in a string iii
557
0.816
Easy
9,744
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2136436/Easy-and-Simple-Python-Solutions-Two-approaches
class Solution: def reverseWords(self, s: str) -> str: #Approach 1 arr=s.split() for i in range(len(arr)): arr[i]=arr[i][::-1] return " ".join(arr)
reverse-words-in-a-string-iii
Easy and Simple Python Solutions - Two approaches
pruthashouche
1
60
reverse words in a string iii
557
0.816
Easy
9,745
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2136436/Easy-and-Simple-Python-Solutions-Two-approaches
class Solution: def reverseWords(self, s: str) -> str: #Approach 2 - Onle liner return " ".join([word[::-1] for word in s.split(" ")])
reverse-words-in-a-string-iii
Easy and Simple Python Solutions - Two approaches
pruthashouche
1
60
reverse words in a string iii
557
0.816
Easy
9,746
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/1884188/Python-one-line-solution
class Solution: def reverseWords(self, s: str) -> str: return ' '.join(x[::-1] for x in s.split())
reverse-words-in-a-string-iii
Python one line solution
alishak1999
1
70
reverse words in a string iii
557
0.816
Easy
9,747
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/1751823/Python-Simple-Python-Solution-Using-Two-Approach
class Solution: def reverseWords(self, s: str) -> str: return ' '.join(s[::-1].split()[::-1])
reverse-words-in-a-string-iii
[ Python ] ✅✅ Simple Python Solution Using Two Approach 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
1
96
reverse words in a string iii
557
0.816
Easy
9,748
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/1751823/Python-Simple-Python-Solution-Using-Two-Approach
class Solution: def reverseWords(self, s: str) -> str: result = '' for word in s.split(): result = result + word[::-1] + ' ' result = result[:-1] return result
reverse-words-in-a-string-iii
[ Python ] ✅✅ Simple Python Solution Using Two Approach 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
1
96
reverse words in a string iii
557
0.816
Easy
9,749
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/1313437/Simple-Non-Developer-answer-in-Python3
class Solution: def reverseWords(self, s: str) -> str: words = s.split(" ") result = [] for word in words: result.append(word[::-1]) return ' '.join(result)
reverse-words-in-a-string-iii
Simple Non Developer answer in Python3
mankuuuuu
1
31
reverse words in a string iii
557
0.816
Easy
9,750
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/1262656/python-easy-to-understand
class Solution: def reverseWords(self, s: str) -> str: s = s.split(" ") for i in range(len(s)): s[i] = s[i][::-1] return " ".join(s)
reverse-words-in-a-string-iii
python easy to understand
user2227R
1
70
reverse words in a string iii
557
0.816
Easy
9,751
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/1127965/Simple-Python-Solution-Faster-than-95
class Solution: def reverseWords(self, s: str) -> str: l = s.split() for i in range(len(l)): l[i] = l[i][::-1] return ' '.join(l)
reverse-words-in-a-string-iii
Simple Python Solution; Faster than 95%;
Annushams
1
232
reverse words in a string iii
557
0.816
Easy
9,752
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2840087/Reverse-Words-in-a-String-III-or-Python-3.x-or-Python's-methods
class Solution: def reverseWords(self, s: str) -> str: reflection = ' '.join([word[::-1] for word in s.split(' ')]) return reflection
reverse-words-in-a-string-iii
Reverse Words in a String III | Python 3.x | Python's methods
SnLn
0
1
reverse words in a string iii
557
0.816
Easy
9,753
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2839049/Python-Easy-n-Fast-Solution
class Solution: def reverseWords(self, s: str) -> str: s_new = '' s_temp = [] for i, c in enumerate(s): if(c == ' '): s_new += ''.join(s_temp[::-1]) + ' ' s_temp = [] else: s_temp.append(c) return s_new + ''.join...
reverse-words-in-a-string-iii
Python - Easy n Fast Solution
hbashanae
0
2
reverse words in a string iii
557
0.816
Easy
9,754
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2822103/One-Liner-Python-Solution
class Solution: def reverseWords(self, s: str) -> str: return ' '.join([word[::-1] for word in s.split()])
reverse-words-in-a-string-iii
One-Liner Python Solution
PranavBhatt
0
4
reverse words in a string iii
557
0.816
Easy
9,755
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2814321/Easy-understanding-Python-Code-for-beginners
class Solution: def reverseWords(self, s: str) -> str: lst = [] s1 = s.split(" ") for i in s1: word = "" for j in range(len(i)-1,-1,-1): word+=i[j] lst.append(word) return " ".join(lst)
reverse-words-in-a-string-iii
Easy understanding Python Code for beginners
Aayush3014
0
4
reverse words in a string iii
557
0.816
Easy
9,756
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2814019/TC-%3A-80.26-Python-simple-solution
class Solution: def reverseWords(self, s: str) -> str: l = s.split() res = "" for i in l: res += (i[::-1] + " ") return res[:-1]
reverse-words-in-a-string-iii
😎 TC : 80.26% Python simple solution
Pragadeeshwaran_Pasupathi
0
2
reverse words in a string iii
557
0.816
Easy
9,757
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2811374/Python
class Solution: def reverseWords(self, s: str) -> str: return " ".join([i[::-1] for i in s.split()])
reverse-words-in-a-string-iii
Python
Teyllayka
0
1
reverse words in a string iii
557
0.816
Easy
9,758
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2803933/Simple-approach-fully-commented.-O(n)-time-O(n)-space
class Solution: def reverseWords(self, s: str) -> str: strArr = list(s) left = 0 right = 0 for i in range(len(strArr)): #increment i until we htio a single space or end of array while i < len(strArr) and strArr[i] != ' ': i += 1 #s...
reverse-words-in-a-string-iii
Simple approach, fully commented. O(n) time, O(n) space
gugliamo
0
2
reverse words in a string iii
557
0.816
Easy
9,759
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2797519/Python-One-Liner
class Solution: def reverseWords(self, s: str) -> str: return " ".join([e[::-1] for e in s.split()])
reverse-words-in-a-string-iii
Python One Liner
sbhupender68
0
1
reverse words in a string iii
557
0.816
Easy
9,760
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2786024/Easy-Python-SolutionororSlicing
class Solution: def reverseWords(self, s: str) -> str: str1=s.split() ab=' '.join(str1[::-1]) return ab[::-1]
reverse-words-in-a-string-iii
Easy Python Solution||Slicing
ankitr8055
0
3
reverse words in a string iii
557
0.816
Easy
9,761
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2782908/Two-pointers-python3
class Solution: def reverseWords(self, s: str) -> str: words = s.split() for i in range(len(words)): word = list(words[i]) l, r = 0, len(word)-1 while l<r: word[l], word[r] = word[r], word[l] l += 1 r -= 1 ...
reverse-words-in-a-string-iii
Two pointers, python3
haniyeka
0
3
reverse words in a string iii
557
0.816
Easy
9,762
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2781789/Python3-Solution
class Solution: def reverseWords(self, s: str) -> str: s = s.split(' ') for i in range(len(s)): s[i] = s[i][::-1] return ' '.join(s)
reverse-words-in-a-string-iii
Python3 Solution
chapmanchris
0
1
reverse words in a string iii
557
0.816
Easy
9,763
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2779444/Reverse-words-III
class Solution: def reverseWords(self, s: str) -> str: lst=s.split(' ') result=[] for i in range(len(lst)): result.append(lst[i][::-1]) return " ".join(result)
reverse-words-in-a-string-iii
Reverse words III
RaviShanker__Thadishetti
0
2
reverse words in a string iii
557
0.816
Easy
9,764
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2769073/Reverse-words-in-a-string-python
class Solution: def reverseWords(self, s: str) -> str: k = s.split(' ') l = '' for idx in range(0,len(k)): k[idx] = k[idx][::-1] if idx == 0: l = l + k[idx] else: l = l + " " + k[idx] return l
reverse-words-in-a-string-iii
Reverse words in a string python
keerthikrishnakumar93
0
3
reverse words in a string iii
557
0.816
Easy
9,765
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2763676/Python-One-Line-Solution
class Solution: def reverseWords(self, s: str) -> str: return " ".join([i[::-1] for i in s.split()])
reverse-words-in-a-string-iii
Python One Line Solution
AbrorAli
0
1
reverse words in a string iii
557
0.816
Easy
9,766
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2755654/Python-!!-For-Beginners-without-using-split()
class Solution: def reverseWords(self, s: str) -> str: c=len(s)-1 newStr="" newStr2="" v=0 while c>=v: if s[v]!=" ": newStr= s[v]+newStr else: newStr2=newStr2 +newStr newStr2=newStr2+" " n...
reverse-words-in-a-string-iii
Python !! For Beginners, without using split()
Yadunandan_1
0
7
reverse words in a string iii
557
0.816
Easy
9,767
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2754759/runtime-36ms-easiest-way-to-solve
class Solution: def reverseWords(self, s: str) -> str: l=s.split(' ') r=[] for i in range(len(l)): r.append(l[i][::-1]) return ' '.join(r)
reverse-words-in-a-string-iii
runtime 36ms easiest way to solve
sindhu_300
0
3
reverse words in a string iii
557
0.816
Easy
9,768
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2744579/Reverse-Word-in-a-String-3-line-solution-in-python
class Solution: def reverseWords(self, s: str) -> str: s = s.strip().split(' ') s = [i[::-1] for i in s] return " ".join(s)
reverse-words-in-a-string-iii
Reverse Word in a String 3 line solution in python
jashii96
0
2
reverse words in a string iii
557
0.816
Easy
9,769
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2742576/Simple-Python-Solution
class Solution: def reverseWords(self, s: str) -> str: str1 = "" lst = s.split() for i in lst: str1 = str1 + i[::-1]+" " str2 = str1[:-1] return str2
reverse-words-in-a-string-iii
Simple Python Solution
dnvavinash
0
2
reverse words in a string iii
557
0.816
Easy
9,770
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2733613/Python3-Solution-one-liner-beat-91.5
class Solution: def reverseWords(self, s: str) -> str: return(" ".join([w[::-1] for w in s.split(" ")]))
reverse-words-in-a-string-iii
Python3 Solution - one-liner, beat 91.5%
sipi09
0
2
reverse words in a string iii
557
0.816
Easy
9,771
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2730066/Python-One-Liner
class Solution: def reverseWords(self, s: str) -> str: return ' '.join([''.join(reversed(x)) for x in s.split()])
reverse-words-in-a-string-iii
Python One-Liner
vbrunell
0
1
reverse words in a string iii
557
0.816
Easy
9,772
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2724821/Python-Solution
class Solution: def reverseWords(self, s: str) -> str: split_string = s.split() #split the given string to words #"'Let's', 'take' , 'LeetCode' ,'contest'" reverse_split_string = [ele[::-1] for ele in split_string ] #re...
reverse-words-in-a-string-iii
Python Solution
Daybyday16
0
1
reverse words in a string iii
557
0.816
Easy
9,773
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2723451/My-Python-solution-easy
class Solution: def reverseWords(self, s: str) -> str: new_words='' for word in s.split(' '): new_words+=word[::-1]+' ' return new_words[:-1]
reverse-words-in-a-string-iii
My Python solution - easy
irouis
0
2
reverse words in a string iii
557
0.816
Easy
9,774
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2701928/Python3-using-split-and-join-beats-95
class Solution: def reverseWords(self, s: str) -> str: new_s = s.split(" ") for i,v in enumerate(new_s): new_s[i] = v[::-1] return(" ".join(new_s))
reverse-words-in-a-string-iii
Python3 using split and join beats 95%
pruth_shiv
0
6
reverse words in a string iii
557
0.816
Easy
9,775
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2698730/Simple-Python-Solution
class Solution: def reverseWords(self, s: str) -> str: ans = "" l = s.split() for word in l: if len(ans)>=1: ans += " " ans += word[::-1] return ans
reverse-words-in-a-string-iii
Simple Python Solution
imkprakash
0
4
reverse words in a string iii
557
0.816
Easy
9,776
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2652583/Simple-and-fast-Python-solution-with-list-comprehension
class Solution: def reverseWords(self, s: str) -> str: return ' '.join([''.join(x[::-1]) for x in s.split(' ')])
reverse-words-in-a-string-iii
Simple and fast Python solution with list comprehension
gbiagini
0
4
reverse words in a string iii
557
0.816
Easy
9,777
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2652344/Simplist-python-solution
class Solution: def reverseWords(self, s: str) -> str: l = s.split() for i in range(len(l)): l[i] = l[i][::-1] return " ".join(l)
reverse-words-in-a-string-iii
Simplist python solution
pro6igy
0
24
reverse words in a string iii
557
0.816
Easy
9,778
https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/2615855/python-solution-using-splitjoin-TC-O(n)
class Solution: def reverseWords(self, s: str) -> str: l1=s.split() l=[] for i in l1: c=i[::-1] l.append(c) for i in l: c=' '.join(l) return c ``
reverse-words-in-a-string-iii
python solution-using split/join [TC-O(n)]
Shreya_sg_283
0
11
reverse words in a string iii
557
0.816
Easy
9,779
https://leetcode.com/problems/logical-or-of-two-binary-grids-represented-as-quad-trees/discuss/884082/Python3-9-line-recursive-(64ms-93.33)
class Solution: def intersect(self, quadTree1: 'Node', quadTree2: 'Node') -> 'Node': if quadTree1.isLeaf: return quadTree1 if quadTree1.val else quadTree2 # boundary condition if quadTree2.isLeaf: return quadTree2 if quadTree2.val else quadTree1 # boundary condition tl = self.intersect(qua...
logical-or-of-two-binary-grids-represented-as-quad-trees
[Python3] 9-line recursive (64ms 93.33%)
ye15
2
151
logical or of two binary grids represented as quad trees
558
0.483
Medium
9,780
https://leetcode.com/problems/logical-or-of-two-binary-grids-represented-as-quad-trees/discuss/1466156/Python-3-or-DFS-Simulation-or-Explanation
class Solution: def intersect(self, quadTree1: 'Node', quadTree2: 'Node') -> 'Node': if quadTree1.isLeaf and quadTree2.isLeaf: # case 1: both are leaf nodes node = Node(quadTree1.val | quadTree2.val, 1, None, None, None, None) return node el...
logical-or-of-two-binary-grids-represented-as-quad-trees
Python 3 | DFS, Simulation | Explanation
idontknoooo
0
119
logical or of two binary grids represented as quad trees
558
0.483
Medium
9,781
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/629671/Python-O(n)-by-DFS-90%2B-w-Comment
class Solution: def maxDepth(self, root: 'Node') -> int: if root is None: # empty node or empty tree return 0 else: # DFS to choose the longest path if root.children: # current node has subtrees ...
maximum-depth-of-n-ary-tree
Python O(n) by DFS 90%+ [w/ Comment]
brianchiang_tw
4
438
maximum depth of n ary tree
559
0.716
Easy
9,782
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/1985642/Python-Easy-Solution-or-Faster-than-94-submits
class Solution: def maxDepth(self, root: 'Node') -> int: if not root : return 0 if root.children : return 1 + max([self.maxDepth(x) for x in root.children]) else : return 1
maximum-depth-of-n-ary-tree
[ Python ] Easy Solution | Faster than 94% submits
crazypuppy
3
111
maximum depth of n ary tree
559
0.716
Easy
9,783
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/548337/Python-simple-solution-36-ms-15MB
class Solution(object): def maxDepth(self, root): """ :type root: Node :rtype: int """ self.maxdepth = 0 def helper(node, depth): if node: depth += 1 for child in node.children: helper(child, depth) ...
maximum-depth-of-n-ary-tree
Python simple solution, 36 ms, 15MB
hemina
3
312
maximum depth of n ary tree
559
0.716
Easy
9,784
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/1323720/Straightforward-BFS-in-python-beating-98-submissions
class Solution: def maxDepth(self, root: 'Node') -> int: depth = 0 if not root: return depth level = [root] while level: depth += 1 level = [c for n in level for c in n.children if c] return depth
maximum-depth-of-n-ary-tree
Straightforward BFS in python, beating 98% submissions
mousun224
2
102
maximum depth of n ary tree
559
0.716
Easy
9,785
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/1200514/PythonPython3-Maximum-Depth-of-N-ary-Tree
class Solution: def maxDepth(self, root: 'Node') -> int: if not root: return 0 def calc_depth(node): if not node: return 0 try: return 1 + max(map(calc_depth, node.children)) except: return 1 ...
maximum-depth-of-n-ary-tree
[Python/Python3] Maximum Depth of N-ary Tree
newborncoder
1
275
maximum depth of n ary tree
559
0.716
Easy
9,786
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/2820426/Faster-than-95-python-solution
class Solution: def maxDepth(self, root: 'Node') -> int: queue = [] if root: queue.append((root, 1)) depth = 0 for (node, level) in queue: depth = level for child in node.children: queue += [(child, level+1) ] return depth
maximum-depth-of-n-ary-tree
Faster than 95% python solution
sushants007
0
1
maximum depth of n ary tree
559
0.716
Easy
9,787
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/2772866/Python-solution-or-BFS
class Solution: def maxDepth(self, root: 'Node') -> int: q = deque() q.append(((1, root))) length = 0 if not root: return 0 while q: d, n = q.popleft() length = max(length, d) for i in range(len(n....
maximum-depth-of-n-ary-tree
Python solution | BFS
maomao1010
0
5
maximum depth of n ary tree
559
0.716
Easy
9,788
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/2760929/Python-recursion
class Solution: def maxDepth(self, root: 'Node') -> int: if not root: return 0 if not root.children: return 1 return 1 + max([self.maxDepth(child) for child in root.children])```
maximum-depth-of-n-ary-tree
Python recursion
vincentoldfriend
0
3
maximum depth of n ary tree
559
0.716
Easy
9,789
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/2694415/Python-O(n)-BFS-solution-6-lines
class Solution: def maxDepth(self, root: 'Node') -> int: if not root: return 0 count, level = 0, [root] while level: count += 1 level = [leaf for node in level for leaf in node.children if leaf] return count
maximum-depth-of-n-ary-tree
[Python] O(n) BFS solution 6 lines
smokfyz
0
9
maximum depth of n ary tree
559
0.716
Easy
9,790
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/2672651/Clean-Fast-Python3-w-Stack
class Solution: def maxDepth(self, root: 'Node') -> int: if not root: return 0 stack = [(root, 1)] max_depth = 1 # dfs while stack: node, depth = stack.pop() # if leaf, stop and record depth if not node.children: ...
maximum-depth-of-n-ary-tree
Clean, Fast Python3 w/ Stack
ryangrayson
0
5
maximum depth of n ary tree
559
0.716
Easy
9,791
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/2655398/Python-Solution-Using-BFS-and-queue
class Solution: def maxDepth(self, root: 'Node') -> int: q = deque() if root: q.append(root) level = 0 while q: for i in range(len(q)): node = q.popleft() if node.children: for chil...
maximum-depth-of-n-ary-tree
Python Solution Using BFS and queue
Furat
0
13
maximum depth of n ary tree
559
0.716
Easy
9,792
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/2333142/ShortPython-recursive-solution-easy-to-understand
class Solution: def maxDepth(self, root: 'Node') -> int: if root == None: return 0 if len(root.children) == 0: return 1 return max([self.maxDepth(child) for child in root.children])+1
maximum-depth-of-n-ary-tree
ShortPython recursive solution easy to understand
tq326
0
46
maximum depth of n ary tree
559
0.716
Easy
9,793
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/2156572/Easy-Python-Solution-using-BFS
class Solution: def maxDepth(self, root: 'Node') -> int: if(root is None): return 0 depth = 0 nodeQueue = [(root, 1)] while(nodeQueue): curr, level = nodeQueue.pop(0) depth = max(depth, level) for node...
maximum-depth-of-n-ary-tree
Easy Python Solution using BFS
kaus_rai
0
30
maximum depth of n ary tree
559
0.716
Easy
9,794
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/1963641/Python1-liner
class Solution: def maxDepth(self, root: 'Node') -> int: return 1 + (max(self.maxDepth(child) for child in root.children) if root.children else 0) if root else 0
maximum-depth-of-n-ary-tree
Python1 liner
constantine786
0
21
maximum depth of n ary tree
559
0.716
Easy
9,795
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/1595786/Topdown-approach
class Solution: def maxDepth(self, root: 'Node') -> int: def dfs(root, depth): if root is None: return 0 maxDepth = depth for child in root.children: maxDepth = max(maxDepth, dfs(child, depth+1)) return maxDepth ...
maximum-depth-of-n-ary-tree
Topdown approach
kirtinikam
0
40
maximum depth of n ary tree
559
0.716
Easy
9,796
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/1534033/Python-one-liner
class Solution: def maxDepth(self, root: 'Node') -> int: return 1 + max([self.maxDepth(c) for c in root.children], default=0) if root else 0
maximum-depth-of-n-ary-tree
Python, one-liner
blue_sky5
0
53
maximum depth of n ary tree
559
0.716
Easy
9,797
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/1501336/Python-recursive-with-explanation
class Solution: def maxDepth(self, root: 'Node') -> int: depth = 0 if not root: return 0 elif root.children == []: return 1 for child in root.children: result = 1 + self.maxDepth(child) depth = max(depth, result) ...
maximum-depth-of-n-ary-tree
Python recursive with explanation
SleeplessChallenger
0
141
maximum depth of n ary tree
559
0.716
Easy
9,798
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/discuss/859298/python-dfs-max-from-list-of-paths
class Solution: def maxDepth(self, root: 'Node') -> int: def dfs(root: 'Node', length: int): if root is None: return length += 1 if not root.children: result.append(length) else: ...
maximum-depth-of-n-ary-tree
python dfs max from list of paths
anthcor
0
52
maximum depth of n ary tree
559
0.716
Easy
9,799