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/reconstruct-original-digits-from-english/discuss/2079750/Simple-intuitive-Python-Using-Dict-less-Operator | class Solution:
def originalDigits(self, s: str) -> str:
# (z)ero, one, t(w)o, three, fo(u)r, five, si(x), seven, ei(g)ht, nine
from collections import Counter
sc = Counter(s)
digits = {0: Counter("zero"), 1: Counter("one"), 2:Counter("two"), 3:Counter("three"), 4:Counter("four"), 5:... | reconstruct-original-digits-from-english | Simple, intuitive Python, Using Dict <= Operator | boris17 | 1 | 70 | reconstruct original digits from english | 423 | 0.513 | Medium | 7,500 |
https://leetcode.com/problems/reconstruct-original-digits-from-english/discuss/1131036/Simple-solution-in-Python3-by-taking-the-count-of-the-unique-characters | class Solution:
def originalDigits(self, s: str) -> str:
c = dict()
c[0] = s.count("z")
c[2] = s.count("w")
c[4] = s.count("u")
c[6] = s.count("x")
c[8] = s.count("g")
c[3] = s.count("h") - c[8]
c[5] = s.count("f") - c[4]
c[7]... | reconstruct-original-digits-from-english | Simple solution in Python3 by taking the count of the unique characters | amoghrajesh1999 | 1 | 67 | reconstruct original digits from english | 423 | 0.513 | Medium | 7,501 |
https://leetcode.com/problems/reconstruct-original-digits-from-english/discuss/2009700/Python-easy-solution-using-dictionaries | class Solution:
def originalDigits(self, s: str) -> str:
freq = Counter(s)
res = {}
res["0"] = freq["z"]
res["2"] = freq["w"]
res["4"] = freq["u"]
res["6"] = freq["x"]
res["8"] = freq["g"]
res["3"] = freq["h"] - res["8"]
res["5"] = freq["f"] - ... | reconstruct-original-digits-from-english | Python easy solution using dictionaries | alishak1999 | 0 | 97 | reconstruct original digits from english | 423 | 0.513 | Medium | 7,502 |
https://leetcode.com/problems/reconstruct-original-digits-from-english/discuss/1696076/Python3-accepted-solution | class Solution:
def originalDigits(self, s: str) -> str:
ans = ""
# words with unique letters
ans += "0"*(s.count("z"))
s = s.replace("z","",s.count("z")).replace("e","",s.count("z")).replace("r","",s.count("z")).replace("o","",s.count("z"))
ans += "2"*(s.count("w"))
... | reconstruct-original-digits-from-english | Python3 accepted solution | sreeleetcode19 | 0 | 192 | reconstruct original digits from english | 423 | 0.513 | Medium | 7,503 |
https://leetcode.com/problems/reconstruct-original-digits-from-english/discuss/839022/Python3-via-Counter | class Solution:
def originalDigits(self, s: str) -> str:
freq = Counter(s)
nums = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
ans = [0]*10
for c, i in ("g", 8), ("u", 4), ("w", 2), ("x", 6), ("z", 0), ("s", 7), ("v", 5), ("h", 3), ("i", 9... | reconstruct-original-digits-from-english | [Python3] via Counter | ye15 | 0 | 161 | reconstruct original digits from english | 423 | 0.513 | Medium | 7,504 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/900524/Simple-Python-solution-moving-window-O(n)-time-O(1)-space | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
# Maintain a dictionary that keeps track of last 'window' characters
# See if 'window' size minus occurrences of the most common char is <= k, if so it's valid
# Run time is O(length of string * size of alphabet)... | longest-repeating-character-replacement | Simple Python solution, moving window O(n) time, O(1) space | wesleyliao3 | 16 | 2,100 | longest repeating character replacement | 424 | 0.515 | Medium | 7,505 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/1965712/Python-Easiest-solution-With-Explanation-or-96.68-Faster-or-Beg-to-Adv-or-Sliding-Window | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
maxf = l = 0
count = collections.Counter() # counting the occurance of the character in the string.
# instead of using "count = collections.Counter()", we can do the following:-
"""
for r, n in enumerate(s):
if... | longest-repeating-character-replacement | Python Easiest solution With Explanation | 96.68% Faster | Beg to Adv | Sliding Window | rlakshay14 | 13 | 871 | longest repeating character replacement | 424 | 0.515 | Medium | 7,506 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/1705860/Python-Intuitive-and-Clean-O(n)-time-approach-and-Big-O-explained | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
leftPointer = 0
currentCharDict = {}
longestLength = 0
for rightPointer,rightValue in enumerate(s):
leftValue = s[leftPointer]
if rightValue not in currentCharDict:
... | longest-repeating-character-replacement | Python Intuitive and Clean O(n) time, approach and Big O explained | kenanR | 5 | 597 | longest repeating character replacement | 424 | 0.515 | Medium | 7,507 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2497159/O(n)-Hashmap-and-sliding-window-or-Python | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
res = 0
left = 0
count = {}
for right in range(len(s)):
count[s[right]] = 1 + count.get(s[right], 0)
# Check this is a valid window
while (right - left + 1) - max(coun... | longest-repeating-character-replacement | O(n) Hashmap & sliding window | Python | Monicaaaaaa | 3 | 270 | longest repeating character replacement | 424 | 0.515 | Medium | 7,508 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2327129/Sliding-window-Python-code-with-proper-explanation | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
"""
s = ABAB
k = 2
If we have no limit on k then we can say that
(no of replacements to be done =
length of string - count of character with maximum occurence)
... | longest-repeating-character-replacement | Sliding window - Python code with proper explanation | Pratyush_Priyam_Kuanr | 3 | 206 | longest repeating character replacement | 424 | 0.515 | Medium | 7,509 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/1585789/Python-Sliding-Window-Approach | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
windowStart = 0
maxRepeatLetterCount = 0
maxLength = 0
char_freq = {}
for windowEnd in range(len(s)):
rightChar = s[windowEnd]
if rightChar not in char_freq:
... | longest-repeating-character-replacement | Python - Sliding Window Approach | lennywgonzalez | 3 | 668 | longest repeating character replacement | 424 | 0.515 | Medium | 7,510 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2750877/Python-Sliding-Window | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
d = {}
slow = 0
ans = 0
for fast in range(len(s)):
if s[fast] in d:
d[s[fast]] += 1
else:
d[s[fast]] = 1
# get max of the window and... | longest-repeating-character-replacement | Python Sliding Window | DietCoke777 | 2 | 157 | longest repeating character replacement | 424 | 0.515 | Medium | 7,511 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/1779450/Python-sliding-window-simple-explanation-with-comments | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
seen = {} # These are the elements we have seen
left = 0 # This is the left pointer for our window
res = 0 # The result we will return in the end
# We will iterate through the entire array
... | longest-repeating-character-replacement | Python - sliding window - simple explanation with comments | iamricks | 2 | 166 | longest repeating character replacement | 424 | 0.515 | Medium | 7,512 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/1529076/Python-code-%2B-explanation-of-logic | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
left = 0
right = 0
ht = {}
longest = 0
while right < len(s):
letter = s[right]
if letter not in ht:
ht[letter] = 0
ht[l... | longest-repeating-character-replacement | Python code + explanation of logic | SleeplessChallenger | 2 | 252 | longest repeating character replacement | 424 | 0.515 | Medium | 7,513 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/1264545/Readable-Code-With-Explanation | class Solution:
def characterReplacement(self, string, k):
left, right = 0, 0
frequencyOfChars, mostCommonElementCount = {}, 0
for right in range(len(string)):
frequencyOfChars[string[right]] = frequencyOfChars.get(string[right], 0) + 1
mostCommonElementCount = max(mo... | longest-repeating-character-replacement | Readable Code With Explanation | ramit_kumar | 2 | 387 | longest repeating character replacement | 424 | 0.515 | Medium | 7,514 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2635275/Python-3-Sliding-window-super-easy-to-understand | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
window = []
max_len = 0
max_freq = 0
char_dict = defaultdict(lambda: 0)
for char in s:
char_dict[char] += 1
window.append(char)
max_freq = max(max_freq, char_dict[char])... | longest-repeating-character-replacement | Python 3 Sliding window - super easy to understand | zakmatt | 1 | 163 | longest repeating character replacement | 424 | 0.515 | Medium | 7,515 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2611348/Sliding-Window-Python-Solution | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
ws=0
d={}
freq=0
maxlen=0
for we in range(len(s)):
c=s[we]
d[c]=d.get(c,0)+1
freq=max(freq,d[c])
if we-ws+1-freq>k:
leftchar=s[ws]
... | longest-repeating-character-replacement | Sliding Window Python Solution | shagun_pandey | 1 | 119 | longest repeating character replacement | 424 | 0.515 | Medium | 7,516 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2258780/Python-O(N)-Faster-than-99-of-submissions-with-explanation | class Solution:
def characterReplacement(self, s: str, k: int) -> int: # output = 10, k = 5, used 4 we need at least 5
counts = {}
l,r = 0,0
most_frequent = s[0]
while r < len(s):
letter = s[r]
# increment this letter's count
counts[letter] = count... | longest-repeating-character-replacement | Python O(N) Faster than 99% of submissions with explanation | joshnewburn42 | 1 | 147 | longest repeating character replacement | 424 | 0.515 | Medium | 7,517 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2228279/Python-Sliding-Window-Beats-98-with-full-working-explanation | class Solution:
def characterReplacement(self, s: str, k: int) -> int: # Time: O(n) and Space:O(n)
count = {} # hashmap to count the occurrences of the characters in string
res = 0
l = 0
# maxfreq will store the frequency of most occurring word in the entire string,
# this way... | longest-repeating-character-replacement | Python [Sliding Window / Beats 98%] with full working explanation | DanishKhanbx | 1 | 209 | longest repeating character replacement | 424 | 0.515 | Medium | 7,518 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2228279/Python-Sliding-Window-Beats-98-with-full-working-explanation | class Solution: # same as above but takes O(26) each time to serach max count value
def characterReplacement(self, s: str, k: int) -> int: # Time: O(26*n) and Space:O(n)
count = {}
res = 0
l = 0
for r in range(len(s)):
count[s[r]] = 1 + count.get(s[r], 0)
... | longest-repeating-character-replacement | Python [Sliding Window / Beats 98%] with full working explanation | DanishKhanbx | 1 | 209 | longest repeating character replacement | 424 | 0.515 | Medium | 7,519 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/1987332/Python-O(N) | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
result = 0
counts = defaultdict(int)
start = 0
for end in range(len(s)):
counts[s[end]] += 1
while end - start + 1 - max(counts.values()) > k:
counts[s[start]] -= 1
... | longest-repeating-character-replacement | Python, O(N) | blue_sky5 | 1 | 207 | longest repeating character replacement | 424 | 0.515 | Medium | 7,520 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2833389/Simple-Python-Windows-using-Counter-O(N) | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
windows = collections.Counter()
size = len(s)
left, right, max_times = 0, 0, 0
while right < size:
windows[ord(s[right])] += 1
max_times = max(max_times, windows[ord(s[right])])
... | longest-repeating-character-replacement | Simple Python Windows using Counter O(N) | qunfei | 0 | 3 | longest repeating character replacement | 424 | 0.515 | Medium | 7,521 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2763066/Easy-Python-Solution-oror-Fully-Explained-oror-Sliding-Window | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
longest = 0
i, j = 0, 0
count = {}
while j < len(s):
# insert the frequency of current window
count.setdefault(s[j], 0)
count.update({ s[j]: count[s[j]]+1 ... | longest-repeating-character-replacement | Easy Python Solution || Fully Explained || Sliding Window | rishisoni6071 | 0 | 12 | longest repeating character replacement | 424 | 0.515 | Medium | 7,522 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2757491/Python3-not-so-effiecient-approach | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
l,r =0,0
res = ''
d = collections.defaultdict(int)
maxlen = 0
while r < len(s):
d[s[r]] += 1
res = res+s[r]
if len(res) - max(d.values()) > k:
d[s[l]] -=... | longest-repeating-character-replacement | Python3 not so effiecient approach | leetcodesquad | 0 | 5 | longest repeating character replacement | 424 | 0.515 | Medium | 7,523 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2750335/Python-two-pointer-sliding-window | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
# O(26 * n), O(n)
res = 0
l = 0
count = {}
for r in range(len(s)):
count[s[r]] = 1 + count.get(s[r], 0)
while ((r - l + 1) - max(count.values())) > k:
count[s[l]] ... | longest-repeating-character-replacement | Python two pointer sliding window | sahilkumar158 | 0 | 10 | longest repeating character replacement | 424 | 0.515 | Medium | 7,524 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2729276/Optimised-window-operator-with-hashmap | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
res = 0
count = {}
l = 0
max_f = 0
for r in range(len(s)):
count[s[r]] = count.get(s[r], 0) + 1
max_f = max(count[s[r]], max_f)
while r - l + 1 - max_f > k:
... | longest-repeating-character-replacement | Optimised window operator with hashmap | meechos | 0 | 10 | longest repeating character replacement | 424 | 0.515 | Medium | 7,525 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2648185/Easy-and-well-explained | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
sub=[0]*26
pointer=0
res=0
for index,value in enumerate(s):
sub[ord(value)-ord('A')]+=1
if sum(sub)-max(sub)>k:
char=s[pointer]
sub[ord(char)-ord('... | longest-repeating-character-replacement | Easy and well explained | chuantianlin | 0 | 12 | longest repeating character replacement | 424 | 0.515 | Medium | 7,526 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2626388/Python-%3A-Sliding-Window | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
hm = {}
ans = 0
l = 0
for r in range(len(s)):
hm[s[r]] = 1 + hm.get(s[r],0)
while (r-l+1) - max(hm.values()) > k:
hm[s[l]] -= 1
l += 1
... | longest-repeating-character-replacement | Python : Sliding Window ✅ | Khacker | 0 | 95 | longest repeating character replacement | 424 | 0.515 | Medium | 7,527 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2565941/Python-solution-using-dict-and-sliding-windows | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
start , end = 0 , 0
ump = {}
maxFreq = 0
mini = 0
for end in range(len(s)):
ump[s[end]] = ump.get(s[end] , 0) + 1
maxFreq = max(maxFreq , ump[s[end]])
if((end - start +... | longest-repeating-character-replacement | Python solution using dict and sliding windows | rajitkumarchauhan99 | 0 | 64 | longest repeating character replacement | 424 | 0.515 | Medium | 7,528 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2546421/Simple-O(N)-python-solution-or-98.82-Faster | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
res = 0
count = defaultdict(int) #Initialize count with defaultdict to increment count for new letters
maxFreq = 0 #keep track of a count for a letter with maximum occurence rate
l = 0 #left pointer of a ... | longest-repeating-character-replacement | Simple O(N) python solution | 98.82% Faster | KohsukeIde | 0 | 210 | longest repeating character replacement | 424 | 0.515 | Medium | 7,529 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2526009/Longest-Repeating-Character-Replacement | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
size = len(s)
if size == 1:
return size
subStringHash = {
'A':0,'B':0,'C':0,'D':0,'E':0,
'F':0,'G':0,'H':0,'I':0,'J':0,
'K':0,'L':0,'M':0,'N':0,'O':0,
'... | longest-repeating-character-replacement | Longest Repeating Character Replacement | ashfaque8198 | 0 | 63 | longest repeating character replacement | 424 | 0.515 | Medium | 7,530 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2514218/Python-or-3-Different-Solutions-or-Optimal-Complexity | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
# Method 1 (Naive Approach): T.C: O(26*n) => O(n) S.C: O(26) => O(1)
windowsize = 0
count = {}
l = r = 0
ans = 0
for i in range(len(s)):
windowsize += 1
count[s[r]] = 1 + c... | longest-repeating-character-replacement | Python | 3 Different Solutions | Optimal Complexity | chawlashivansh | 0 | 94 | longest repeating character replacement | 424 | 0.515 | Medium | 7,531 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2447422/Python3-oror-Sliding-Window-oror-O(n)-solution | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
count = {}
res = 0
l = 0
maxf = 0
for r in range(len(s)):
count[s[r]] = 1 + count.get(s[r], 0)
maxf = max(maxf, count[s[r]])
while (r - l + 1) - maxf > k:
... | longest-repeating-character-replacement | Python3 || Sliding Window || O(n) solution | WhiteBeardPirate | 0 | 117 | longest repeating character replacement | 424 | 0.515 | Medium | 7,532 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2430556/Longest-repeating-character-replacement-oror-Python3-oror-Sliding-Window | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
map = {}
max_freq = 1
ans = 1
i = 0
j = 0
# Acquire and release appproach
while(j < len(s)):
char_val = ord(s[j]) - ord('A')
if(char_val in map):
map[char_... | longest-repeating-character-replacement | Longest repeating character replacement || Python3 || Sliding-Window | vanshika_2507 | 0 | 44 | longest repeating character replacement | 424 | 0.515 | Medium | 7,533 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2324218/Python3-Detailed-Explanation-with-Example | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
start = 0
count = {}
max_ = 0
for end in range(len(s)):
# Update the most-common character including the current one
count[s[end]] = count.get(s[end], 0) + 1
max_ = max(max_,... | longest-repeating-character-replacement | Python3 Detailed Explanation with Example | geom1try | 0 | 95 | longest repeating character replacement | 424 | 0.515 | Medium | 7,534 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2188279/Python-Solving-8-Substring-problem-with-same-template | class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
hmap = {}
begin, end, duplicate = 0, 0, False
max_val = 0
while end < len(s):
hmap[s[end]] = hmap.get(s[end], 0) + 1
if hmap[s[end]] > 1:
duplicate = True
end += 1
... | longest-repeating-character-replacement | [Python] Solving 8 Substring problem with same template | Gp05 | 0 | 76 | longest repeating character replacement | 424 | 0.515 | Medium | 7,535 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2188279/Python-Solving-8-Substring-problem-with-same-template | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
start, end, duplicate= 0, 0, False
length = 0
hashmap = {}
while end < len(s):
hashmap[s[end]] = hashmap.get(s[end], 0) + 1
if ((end - start + 1) - max(hashmap.values())) > k:
... | longest-repeating-character-replacement | [Python] Solving 8 Substring problem with same template | Gp05 | 0 | 76 | longest repeating character replacement | 424 | 0.515 | Medium | 7,536 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2188279/Python-Solving-8-Substring-problem-with-same-template | class Solution:
def length_of_longest_substring_two_distinct(self, s: str) -> int:
start, end, duplicate= 0, 0, False
length = 0
listmap = []
val = 0
while end < len(s):
if s[end] not in listmap:
val += 1
listmap.append(s[end])
... | longest-repeating-character-replacement | [Python] Solving 8 Substring problem with same template | Gp05 | 0 | 76 | longest repeating character replacement | 424 | 0.515 | Medium | 7,537 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2188279/Python-Solving-8-Substring-problem-with-same-template | class Solution:
def length_of_longest_substring_k_distinct(self, s: str, k: int) -> int:
start, end, duplicate= 0, 0, False
length = 0
listmap = []
val = 0
while end < len(s):
if s[end] not in listmap:
val += 1
listmap.append(s[end])
... | longest-repeating-character-replacement | [Python] Solving 8 Substring problem with same template | Gp05 | 0 | 76 | longest repeating character replacement | 424 | 0.515 | Medium | 7,538 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2188279/Python-Solving-8-Substring-problem-with-same-template | class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
hmap = Counter(p)
start, end, count = 0, 0, len(hmap)
res = []
while end < len(s):
if s[end] in hmap:
hmap[s[end]] -= 1
if hmap[s[end]] == 0:
... | longest-repeating-character-replacement | [Python] Solving 8 Substring problem with same template | Gp05 | 0 | 76 | longest repeating character replacement | 424 | 0.515 | Medium | 7,539 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2188279/Python-Solving-8-Substring-problem-with-same-template | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
hashmap = Counter(s1)
start, end, counter = 0, 0, len(hashmap)
while end < len(s2):
if s2[end] in hashmap:
hashmap[s2[end]] -= 1
if hashmap[s2[end]] == 0:
... | longest-repeating-character-replacement | [Python] Solving 8 Substring problem with same template | Gp05 | 0 | 76 | longest repeating character replacement | 424 | 0.515 | Medium | 7,540 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2188279/Python-Solving-8-Substring-problem-with-same-template | class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
lenWords = len(words[0])
res = []
for i in range(lenWords):
begin, end = (i + 0), (i + 0)
hashmap = Counter(words)
counter = len(hashmap)
while end < len(s):
... | longest-repeating-character-replacement | [Python] Solving 8 Substring problem with same template | Gp05 | 0 | 76 | longest repeating character replacement | 424 | 0.515 | Medium | 7,541 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2188279/Python-Solving-8-Substring-problem-with-same-template | class Solution:
def minWindow(self, s: str, t: str) -> str:
minStr = ""
minStrLen = len(s) + 1
hashmap = Counter(t)
start, end, counter = 0, 0, len(hashmap)
while end < len(s):
if s[end] in hashmap:
hashmap[s[end]] -= 1
if ... | longest-repeating-character-replacement | [Python] Solving 8 Substring problem with same template | Gp05 | 0 | 76 | longest repeating character replacement | 424 | 0.515 | Medium | 7,542 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2178054/Sliding-Window-Approach-(Optimized) | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
length = len(s)
l = 0
frequency = {}
longestLength = 0
maxFrequency = 0
for r in range(length):
frequency[s[r]] = 1 + frequency.get(s[r], 0)
maxFrequency = max(maxFrequency... | longest-repeating-character-replacement | Sliding Window Approach (Optimized) | Vaibhav7860 | 0 | 148 | longest repeating character replacement | 424 | 0.515 | Medium | 7,543 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2045109/Python-Two-pointers-and-HashMap | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
d = {}
result = 0
left, right = 0, 0
while right < len(s):
d[s[right]] = 1 + d.get(s[right], 0)
while (right - left + 1) - max(d.values()) > k:
... | longest-repeating-character-replacement | Python - Two pointers and HashMap | TrueJacobG | 0 | 164 | longest repeating character replacement | 424 | 0.515 | Medium | 7,544 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/2017423/Python-Sliding-Window%2BHashmap-O(n) | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
n=len(s)
dic=defaultdict(int)
left=right=0
maxcnt=1
while right<n:
dic[s[right]]+=1
maxcnt=max(maxcnt,dic[s[right]])
right+=1
if maxcnt+k<right-left:
... | longest-repeating-character-replacement | Python Sliding Window+Hashmap O(n) | appleface | 0 | 84 | longest repeating character replacement | 424 | 0.515 | Medium | 7,545 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/1905621/Python-Optimal-solution | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
# using a hash map to keep count of alphabets
count={}
# left counter
l=0
res=0
# iterating over right counter
for r in range(len(s)):
# assigning... | longest-repeating-character-replacement | Python Optimal solution | saty035 | 0 | 111 | longest repeating character replacement | 424 | 0.515 | Medium | 7,546 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/1826423/Python-easy-to-read-and-understand-or-sliding-window | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
d = {}
n = len(s)
i, ans = 0, 0
for j in range(n):
d[s[j]] = d.get(s[j], 0) + 1
cnt = (j-i+1) - max(d.values())
while cnt > k:
d[s[i]] -= 1
i = i... | longest-repeating-character-replacement | Python easy to read and understand | sliding window | sanial2001 | 0 | 241 | longest repeating character replacement | 424 | 0.515 | Medium | 7,547 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/1616334/Most-intuitive-python-sliding-window-solution | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
from collections import defaultdict
count=defaultdict(int)
n=len(s)
left=0
res=0
for right in range(n):
count[s[right]]+=1
while right-left+1-max(count... | longest-repeating-character-replacement | Most intuitive python sliding window solution | Karna61814 | 0 | 134 | longest repeating character replacement | 424 | 0.515 | Medium | 7,548 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/1569554/Easy-Python-Sliding-Window-Solution-or-80-Faster | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
winS=0
maxL=-(1<<31)
maxchar=0
freq={}
for winE in range(len(s)):
freq[s[winE]]=freq.get(s[winE],0)+1
maxchar=max(maxchar,freq[s[winE]])
if ((winE-winS+1)-maxchar)>k:
if freq[s[winS]]==1:
del freq[s[winS]]
else:
... | longest-repeating-character-replacement | Easy Python Sliding Window Solution | 80% Faster | AdityaTrivedi88 | 0 | 175 | longest repeating character replacement | 424 | 0.515 | Medium | 7,549 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/1350922/GolangPython3-Solution-with-using-sliding-window | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
start_window_idx = 0
repeat_len = 0
max_len = 0
ch2cnt = collections.defaultdict(int)
for end_window_idx in range(len(s)):
ch2cnt[s[end_window_idx]] += 1
repeat_le... | longest-repeating-character-replacement | [Golang/Python3] Solution with using sliding window | maosipov11 | 0 | 88 | longest repeating character replacement | 424 | 0.515 | Medium | 7,550 |
https://leetcode.com/problems/longest-repeating-character-replacement/discuss/1327422/python3-solution-sliding-window-and-hash-map-O(n)-time-complexity. | class Solution:
def characterReplacement(self, s: str, k: int) -> int:
res = 0
left = 0
count = {}
maxf = 0
for right in range(len(s)):
count[s[right]] = 1 + count.get(s[right],0)
maxf = max(maxf, count[s[right]])
while (right-left+1) ... | longest-repeating-character-replacement | python3 solution sliding window and hash map O(n) time complexity. | pravishbajpai06 | 0 | 485 | longest repeating character replacement | 424 | 0.515 | Medium | 7,551 |
https://leetcode.com/problems/construct-quad-tree/discuss/874250/Python3-building-tree-recursively | class Solution:
def construct(self, grid: List[List[int]]) -> 'Node':
def fn(x0, x1, y0, y1):
"""Return QuadTree subtree."""
val = {grid[i][j] for i, j in product(range(x0, x1), range(y0, y1))}
if len(val) == 1: return Node(val.pop(), True, None, None, None, Non... | construct-quad-tree | [Python3] building tree recursively | ye15 | 2 | 167 | construct quad tree | 427 | 0.663 | Medium | 7,552 |
https://leetcode.com/problems/construct-quad-tree/discuss/2709862/Python-easy-to-understand | class Solution:
def construct(self, grid: List[List[int]]) -> 'Node':
bits = {1:0,0:0}
for row in grid:
for i in row:
bits[i] += 1
total_bits = len(grid) ** 2
if bits[1] == total_bits: return Node(1, True, None, None, None, None)
if bits[0] == tota... | construct-quad-tree | [Python] easy to understand | scrptgeek | 0 | 16 | construct quad tree | 427 | 0.663 | Medium | 7,553 |
https://leetcode.com/problems/construct-quad-tree/discuss/2642818/Python-O(N2)-O(1) | class Solution:
def construct(self, grid: List[List[int]]) -> 'Node':
def helper(left, right, down, up):
if left == right and up == down:
return Node(grid[up][left], 1, None, None, None, None)
midlr = (left + right) // 2
midud = (down + up) //... | construct-quad-tree | Python - O(N^2), O(1) | Teecha13 | 0 | 11 | construct quad tree | 427 | 0.663 | Medium | 7,554 |
https://leetcode.com/problems/construct-quad-tree/discuss/2276162/Python-Iterative-Tree-Construction-(Simpler-than-Recursion) | class Solution:
def construct(self, grid: List[List[int]]) -> 'Node':
level = [[Node(val, True, None, None, None, None) for val in row] for row in grid]
while len(level) * len(level[0]) != 1:
print([[node.val for node in row] for row in level])
height = len(level)
... | construct-quad-tree | Python, Iterative Tree Construction (Simpler than Recursion?) | boris17 | 0 | 57 | construct quad tree | 427 | 0.663 | Medium | 7,555 |
https://leetcode.com/problems/construct-quad-tree/discuss/1365149/Recursive-short-Python-less-10-Lines | class Solution:
def construct(self, grid: List[List[int]]) -> 'Node':
def shouldSplit(i,j,n) -> bool:
return len(set(x for row in grid[i:i+n] for x in row[j:j+n])) > 1
def f(i,j,n):
if shouldSplit(i,j,n):
s = n//2
return Node(... | construct-quad-tree | Recursive short Python < 10 Lines | worker-bee | 0 | 166 | construct quad tree | 427 | 0.663 | Medium | 7,556 |
https://leetcode.com/problems/construct-quad-tree/discuss/681140/Simple-Python3-Recursion | class Solution:
def construct(self, grid: List[List[int]]) -> 'Node':
if not grid:
return
node = Node()
grid_sum, n = sum(map(sum, grid)), len(grid)
if grid_sum == (n * n) or grid_sum == 0:
node.val, node.isLeaf = grid_sum // (n * n), True
return n... | construct-quad-tree | Simple Python3 Recursion | whissely | 0 | 175 | construct quad tree | 427 | 0.663 | Medium | 7,557 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2532005/Python-BFS | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
result = []
q = deque([root] if root else [])
while q:
result.append([])
for _ in range(len(q)):
node = q.popleft()
result[-1].append(node.val)
... | n-ary-tree-level-order-traversal | Python, BFS | blue_sky5 | 14 | 1,300 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,558 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2533492/Python-Elegant-and-Short-or-BFS-%2B-DFS-or-Generators | class Solution:
"""
Time: O(n)
Memory: O(n)
"""
def levelOrder(self, root: Optional['Node']) -> List[List[int]]:
if root is None:
return []
queue = deque([root])
levels = []
while queue:
levels.append([])
for _ in range(len(queue)):
node = queue.popleft()
levels[-1].append(node.val)
... | n-ary-tree-level-order-traversal | Python Elegant & Short | BFS + DFS | Generators | Kyrylo-Ktl | 2 | 83 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,559 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2533492/Python-Elegant-and-Short-or-BFS-%2B-DFS-or-Generators | class Solution:
"""
Time: O(n)
Memory: O(n)
"""
def levelOrder(self, root: Optional['Node']) -> List[List[int]]:
levels = defaultdict(list)
for node, depth in self._walk(root):
levels[depth].append(node.val)
return [levels[d] for d in sorted(levels)]
@classmethod
def _walk(cls, root: Optional['Nod... | n-ary-tree-level-order-traversal | Python Elegant & Short | BFS + DFS | Generators | Kyrylo-Ktl | 2 | 83 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,560 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/1095683/PythonPython3-N-ary-Tree-Level-Order-Traversal | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root: return []
ans = []
level = [root]
while level:
ans.append([node.val for node in level])
level = [kid for node in level for kid in node.children if ki... | n-ary-tree-level-order-traversal | [Python/Python3] N-ary Tree Level Order Traversal | newborncoder | 2 | 171 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,561 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2533959/Python-Recursive-DFS-Solution | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root: return []
res = []
levels = set()
def dfs(node, level):
if level not in levels:
levels.add(level)
res.append([])
res[level].append(node.val)
... | n-ary-tree-level-order-traversal | [Python] Recursive DFS Solution | sreenithishb | 1 | 34 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,562 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2533561/100-or-Classic-BFS-.-2-ways-in-each-language-(C%2B%2B-Go-Python-TsJs-Java) | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
r = []
if not root:
return r
dq = deque()
dq.append(root)
while dq:
temp = []
size = len(dq)
for _ in range(size):
node = dq.popleft()
... | n-ary-tree-level-order-traversal | 🌻 100% | Classic BFS . 2 ways in each language (C++, Go, Python, Ts/Js, Java) | nuoxoxo | 1 | 35 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,563 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2532377/Easy-python-solution-18-lines-only | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if root is None:
return []
#print(root.children)
valList=[[root.val]]
flst=[]
xlst=[]
lst=[root]
while lst:
x=lst.pop(0)
if x.children:
f... | n-ary-tree-level-order-traversal | Easy python solution 18 lines only | shubham_1307 | 1 | 11 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,564 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2084329/Python3-BFS-Iterative-Solution | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
#if root is null directly return empty list
if not root:
return []
#create queue data structure
queue=collections.deque()
#add the root node to the queue
queue.append(root)
res=[]
while(q... | n-ary-tree-level-order-traversal | Python3 BFS Iterative Solution | rohith4pr | 1 | 95 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,565 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/1725735/python-Faster-than-78 | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
groups = []
queue = deque()
if root is not None:
queue.append((root,0))
while len(queue)>0:
current,dist = queue.popleft()
if len(groups)-1>=dist:
... | n-ary-tree-level-order-traversal | python Faster than 78% | nisal_sasmitha | 1 | 51 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,566 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/1642817/While-loop-97-speed | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root:
return []
ans = []
level = [root]
while level:
new_level = []
level_values = []
for node in level:
level_values.append(node.val)
... | n-ary-tree-level-order-traversal | While loop, 97% speed | EvgenySH | 1 | 90 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,567 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/1387969/Super-simple-Python-solution | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root: return []
output=[]
level=[root]
while level:
currLevel=[]
nextLevel=[]
for node in level:
currLevel.append(... | n-ary-tree-level-order-traversal | Super simple Python 🐍 solution | InjySarhan | 1 | 90 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,568 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/840366/Python3-BFS | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
ans = []
if root:
queue = [root]
while queue:
newq, vals = [], []
for x in queue:
vals.append(x.val)
newq.extend(x.children)
... | n-ary-tree-level-order-traversal | [Python3] BFS | ye15 | 1 | 41 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,569 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/302450/Python-beats-98-recursive | class Solution:
def levelOrder(self, root) -> List[List[int]]:
output = []
if root is not None:
output.append([root.val])
self.trav(root.children, 0, output)
output.pop()
return output
def trav(self, node, deep, output):
deep += 1
if (len(output) - 1 < deep): output.append([])
for x in node:
... | n-ary-tree-level-order-traversal | Python beats 98% recursive | ht921005 | 1 | 444 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,570 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2765535/Python-solution-or-BFS | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
levels = []
def bfs(node, level):
if node:
if level >= len(levels):
levels.append([])
levels[level].append(node.val)
for child in no... | n-ary-tree-level-order-traversal | Python solution | BFS | maomao1010 | 0 | 2 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,571 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2718603/N-ary-Tree-Level-Order-Traversal-in-Python | class Solution(object):
def levelOrder(self, root):
if not root:
return []
output=[]
queue=[root]
while queue:
b=[]
for i in range(len(queue)):
a=queue.pop(0)
b.append(a.val)
for j in a.children:
... | n-ary-tree-level-order-traversal | N-ary Tree Level Order Traversal in Python | siddharth2205 | 0 | 3 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,572 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2543182/C%2B%2BPython-or-Easy-solution-or-BFS | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root :
return []
ans, queue = [], [root]
while queue :
row = []
N = len(queue)
for _ in range(N) :
t = queue.pop(0)
... | n-ary-tree-level-order-traversal | [C++/Python] | Easy solution | BFS | prakharrai1609 | 0 | 7 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,573 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2536155/python-oror-easy-oror-default-dict-oror-beginner-friendly | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
d=defaultdict(list)
ans=[]
def solve(root,level):
if root==None:
return
else:
d[level].append(root.val)
for c in root.children:
... | n-ary-tree-level-order-traversal | python || easy || default dict || beginner friendly | minato_namikaze | 0 | 4 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,574 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2535711/Python-or-Queue | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
def levelOrderTrav(root):
if not root:
return []
q = collections.deque()
q.append(root)
res = []
while q:
curLevel = []
for _ in range(len(q)):
curNode = q.popleft()
for node in curNode.children:
q.a... | n-ary-tree-level-order-traversal | Python | Queue | Mark5013 | 0 | 9 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,575 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2535446/Python-Solution-with-Dynamic-Programming | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
children = [root]
result = []
while not all(i is None for i in children):
level_values = []
new_children = []
for node in children:
level_values.append(node.val)
... | n-ary-tree-level-order-traversal | Python Solution with Dynamic Programming | DyHorowitz | 0 | 3 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,576 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2535446/Python-Solution-with-Dynamic-Programming | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
children = [root]
result = []
while not all(i is None for i in children):
new_children = []
for node in children:
for child in node.children:
new_children.append... | n-ary-tree-level-order-traversal | Python Solution with Dynamic Programming | DyHorowitz | 0 | 3 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,577 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2534420/Python3-DFS-48ms-faster-than-98 | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
def helper(root, level, ans):
if not root:
return []
if len(ans)<level:
ans.append([])
ans[level-1].append(root.val)
f... | n-ary-tree-level-order-traversal | Python3 DFS 48ms faster than 98% | mrprashantkumar | 0 | 6 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,578 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2534374/Simple-Python-Solution-oror-BFS | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
ans = []
cur_level, cur_level_nodes = 0, []
# initialise and add root in queue with level as 0
q = deque()
q.append((root, 0))
while q:
# get first element from the queue and its resp. level
... | n-ary-tree-level-order-traversal | Simple Python Solution || BFS | wilspi | 0 | 7 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,579 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2534089/Python-easy-to-understand-BFS-iterative-stack | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root:
return []
result = []
stack = [(root,)]
while stack:
new_level = []
local_result = []
nodes = stack.pop()
if not nodes:
cont... | n-ary-tree-level-order-traversal | [Python] easy to understand BFS iterative stack | dlog | 0 | 3 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,580 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2533626/Python-BFS-using-queue | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root:
return root
que = deque()
que.append(root)
result = []
while que:
res = []
for i in range(len(que)):
node = q... | n-ary-tree-level-order-traversal | Python - BFS using queue | supersid1695 | 0 | 9 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,581 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2533115/Python-or-bfs-or-O(n)-Time-or-O(n)-Space | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root:
return []
stack = []
stack.append(root)
ans = []
while len(stack):
size = len(stack)
l = []
while size:
node = stack[0]... | n-ary-tree-level-order-traversal | Python | bfs | O(n) Time | O(n) Space | coolakash10 | 0 | 3 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,582 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2533056/Python-solution-easy-to-understand-bfs | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root:
return []
node = [root]
res = []
while(node):
currLvl = []
nxtNode = []
for x in range(len(node)):
currLvl.append(... | n-ary-tree-level-order-traversal | Python solution - easy to understand - bfs | MacPatil23 | 0 | 3 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,583 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2533012/EASY-PYTHON3-SOLUTION | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
result = []
q = deque([root]) if root else None
while q:
level = []
len_ = len(q)
for _ in range(len_):
node = q.popleft()
level.append(node.val... | n-ary-tree-level-order-traversal | 🔥 EASY PYTHON3 SOLUTION 🔥 | rajukommula | 0 | 1 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,584 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2532490/Python3-90.32-or-O(N)-BFS-Solution-or-Beginner-Friendly-Pythonic | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
# edge:
if not root:
return []
bfs_q = [root]
ans = []
while bfs_q:
lvl_leng = len(bfs_q)
lvl = []
for _ in range(lvl_leng):
... | n-ary-tree-level-order-traversal | Python3 90.32% | O(N) BFS Solution | Beginner Friendly, Pythonic | doneowth | 0 | 3 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,585 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2532341/Python-bfs | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root: return []
ans = []
queue = deque()
queue.append(root)
while queue:
nq = deque()
temp = []
while queue:
node = queue.popleft()
... | n-ary-tree-level-order-traversal | Python bfs | li87o | 0 | 5 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,586 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2532219/Python-BFS-better-than-90.40 | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root:
return []
out=[]
queue=deque([root])
while queue:
size = len(queue)
temp=[]
while size>0:
node = queue.popleft()
temp.ap... | n-ary-tree-level-order-traversal | Python, BFS better than 90.40% | omarihab99 | 0 | 10 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,587 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2532162/Python3-or-Easy-to-Understand-or-BFS-or-Efficient | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root: return []
q = deque([root])
traversal = []
while q:
level = []
for _ in range(len(q)):
node = q.popleft()
... | n-ary-tree-level-order-traversal | ✅Python3 | Easy to Understand | BFS | Efficient | thesauravs | 0 | 8 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,588 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2532124/Python-or-BFS | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
ans = []
def bfs(node, lvl):
nonlocal ans
if not node:
return
## If this is a new level, we make a blank array inside ans
if len(ans) < lvl + 1:
ans.append([]... | n-ary-tree-level-order-traversal | Python | BFS | prithuls | 0 | 4 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,589 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2531986/Python-or-FIFO-Queue | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if root is None:
return []
nodeQueue = deque([(root, 0)]); LOT = []
while nodeQueue:
currentNode, level = nodeQueue.popleft()
if len(LOT) == level:
LOT.append(... | n-ary-tree-level-order-traversal | Python | FIFO Queue | sr_vrd | 0 | 6 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,590 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2355456/Python3-O(n)-Solution-or-O(nlgn)-Space-though | class Solution:
#Time-Complexity: LevelOrder function always traverse each and every node in
#n-ary tree input! Also, helper function traverses each and every non-null node
#once in dfs manner! -> O(n), where n is number of nodes in n-ary tree input!
#Space-Complexity: O(n + (lgn - 1)* n), since answe... | n-ary-tree-level-order-traversal | Python3 O(n) Solution | O(nlgn) Space though | JOON1234 | 0 | 15 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,591 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2349746/Python-3-oror-98-oror-Recursive | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
lst = []
def traversal(root,height):
if root == None:
return
if height > len(lst)-1:
lst.append([])
lst[height].append(root.val)
for chi... | n-ary-tree-level-order-traversal | Python 3 || 98% || Recursive | tq326 | 0 | 32 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,592 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2210233/Python-Simple-solution | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
res = []
if not root:
return res
q = deque([root])
while q:
temp = []
for _ in range(len(q)):
node = q.popleft()
if node:
... | n-ary-tree-level-order-traversal | Python Simple solution | Gp05 | 0 | 13 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,593 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/2053316/Python-%22DFS%22-94-Memory | class Solution:
def __init__(self):
self.res = []
def levelOrder(self, root: 'Node') -> List[List[int]]:
def dfs(root, stage):
if not root:
return
if len(self.res)-1 < stage:
self.res.append([root.val])
else:
sel... | n-ary-tree-level-order-traversal | Python "DFS" 94% Memory | codeee5141 | 0 | 31 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,594 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/1964100/Python3-simple-solution | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root:
return []
q = [root]
res = [[root.val]]
while q:
x = []
c = len(q)
for i in range(c):
z = q.pop(0)
if z.children:
... | n-ary-tree-level-order-traversal | Python3 simple solution | EklavyaJoshi | 0 | 26 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,595 |
https://leetcode.com/problems/n-ary-tree-level-order-traversal/discuss/1898063/Python-easy-understanding-solution-with-comment | class Solution:
def levelOrder(self, root: 'Node') -> List[List[int]]:
if not root:
return None
queue = collections.deque([root])
res = []
while queue:
i, l = 0, len(queue) # l is the number of nodes in current level
... | n-ary-tree-level-order-traversal | Python easy - understanding solution with comment | byroncharly3 | 0 | 33 | n ary tree level order traversal | 429 | 0.706 | Medium | 7,596 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/1550377/Python-Recursion%3A-Easy-to-understand-with-Explanation | class Solution:
def flatten(self, head: 'Node') -> 'Node':
def getTail(node):
prev = None
while node:
_next = node.next
if node.child:
# ... <-> node <-> node.child <-> ...
node.next = node.child
node.child ... | flatten-a-multilevel-doubly-linked-list | Python Recursion: Easy-to-understand with Explanation | zayne-siew | 6 | 501 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,597 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/645406/Python-sol-by-recursion.-80%2B-w-Visualization | class Solution:
def flatten(self, head: 'Node') -> 'Node':
def helper(head) -> 'None':
prev, current_node = None, head
while current_node:
if current_node.child:
# flatten chi... | flatten-a-multilevel-doubly-linked-list | Python sol by recursion. 80%+ [w/ Visualization ] | brianchiang_tw | 5 | 756 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,598 |
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/discuss/652275/Python3-recursive-and-iterative-solution | class Solution:
def flatten(self, head: 'Node') -> 'Node':
def fn(node):
"""Recursively flatten doubly-linked list."""
if not node: return
tail = node
if node.child:
tail = fn(node.child)
tail.next = node.next
... | flatten-a-multilevel-doubly-linked-list | [Python3] recursive & iterative solution | ye15 | 3 | 118 | flatten a multilevel doubly linked list | 430 | 0.595 | Medium | 7,599 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.