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/jump-game-iii/discuss/1062049/Weeb-does-Python(bfs)
class Solution: def canReach(self, arr, start): queue = deque([start]) visited = set([]) limit = len(arr) while queue: for _ in range(len(queue)): i = queue.popleft() if i in visited: continue if arr[i] == 0: return True visited.add(i) for new_i in [i + arr[i], i - arr[i]]: if 0<=new_i<limit and new_i not in visited: queue.append(new_i) return False
jump-game-iii
Weeb does Python(bfs)
Skywalker5423
0
75
jump game iii
1,306
0.631
Medium
19,500
https://leetcode.com/problems/jump-game-iii/discuss/953186/Simple-DFS-Solution
class Solution: def canReach(self, arr: List[int], start: int) -> bool: visited=[0]*len(arr) def helper(arr, start,i): if i>=len(arr) or i<0: return False if arr[i]==0: return True if visited[i]: return False visited[i]=1 x=helper(arr, start, i+arr[i]) y=helper(arr, start, i-arr[i]) return x or y return helper(arr, start, start)
jump-game-iii
Simple DFS Solution
Ayu-99
0
51
jump game iii
1,306
0.631
Medium
19,501
https://leetcode.com/problems/jump-game-iii/discuss/953046/Intuitive-approach-by-DFS
class Solution: def canReach(self, arr: List[int], start: int) -> bool: visisted_pos = set() dest_pos = set() # 0) Search destination(s) for i in range(len(arr)): if arr[i] == 0: dest_pos.add(i) # 1) Short cut for no answer if len(dest_pos) == 0: return False # 2) Start DFS visisted_pos.add(start) def search_ans(p, visited_pos=visisted_pos, dest_pos=dest_pos): if p in dest_pos: return True visited_pos.add(p) jump = arr[p] jump_back, jump_head = p - jump, p + jump next_pos_list = [] if jump_back >=0 and jump_back not in visited_pos: next_pos_list.append(jump_back) if jump_head < len(arr) and jump_head not in visited_pos: next_pos_list.append(jump_head) for pos in next_pos_list: if search_ans(pos): return True return False return search_ans(start)
jump-game-iii
Intuitive approach by DFS
puremonkey2001
0
30
jump game iii
1,306
0.631
Medium
19,502
https://leetcode.com/problems/jump-game-iii/discuss/948583/Python-Simple-Recursion
class Solution: def canReach(self, arr: List[int], start: int) -> bool: visit = [0 for _ in range(len(arr))] def recursion(arr,start,visit): if start>=len(arr) or start<0 or visit[start]==True: return False if arr[start]==0: return True start1 = start+arr[start] start2 = start-arr[start] visit[start]=True return recursion(arr,start1,visit) or recursion(arr,start2,visit) return recursion(arr,start,visit)
jump-game-iii
Python Simple Recursion
bharatgg
0
72
jump game iii
1,306
0.631
Medium
19,503
https://leetcode.com/problems/jump-game-iii/discuss/873755/Python3-very-simple-recursion-solution
class Solution: def canReach(self, arr: List[int], start: int) -> bool: graph=collections.defaultdict(list) # create a graph for i in range(len(arr)): if i-arr[i]>=0: graph[i].append(i-arr[i]) if i+arr[i]<len(arr): graph[i].append(i+arr[i]) def dfs(node): nonlocal visited if visited[node]:return if arr[node]==0: return True visited[node]=True for neigh in graph[node]: if dfs(neigh): return True return False visited=[False]*len(arr) return dfs(start)
jump-game-iii
Python3 very simple recursion solution
harshitCode13
0
43
jump game iii
1,306
0.631
Medium
19,504
https://leetcode.com/problems/jump-game-iii/discuss/714389/Python3-BFS-Solution
class Solution: def canReach(self, arr: List[int], start: int) -> bool: seen = set() q = deque() q.append(start) while q: curr = q.popleft() if curr in seen: continue seen.add(curr) if 0 <= curr - arr[curr] < len(arr): if arr[curr-arr[curr]] == 0: return True else: q.append(curr-arr[curr]) if 0 <= curr + arr[curr] < len(arr): if arr[curr+arr[curr]] == 0: return True else: q.append(curr+arr[curr]) return False
jump-game-iii
Python3 BFS Solution
fuzzywuzzy21
0
68
jump game iii
1,306
0.631
Medium
19,505
https://leetcode.com/problems/jump-game-iii/discuss/694943/Python3-Clean-Concise-and-Easy-Recursive-Solution
class Solution: def canReach(self, arr: List[int], start: int) -> bool: def recurse(i): # check index bounds if i < 0: return False if i > len(arr)-1: return False # result will be an infinite loop if we continue to recurse on visited index if visited[i]: return False # this is the target result if arr[i] == 0: return True # visit the node visited[i] = True # recurse on both conditions return recurse(i+arr[i]) or recurse(i-arr[i]) # create a visited array visited = len(arr)*[False] # begin recursion return recurse(start)
jump-game-iii
[Python3] Clean Concise and Easy Recursive Solution
jonathan
0
49
jump game iii
1,306
0.631
Medium
19,506
https://leetcode.com/problems/jump-game-iii/discuss/463952/Python-3-(seven-lines)-(beats-100)-(BFS-Queue-Memo)
class Solution: def canReach(self, A: List[int], S: int) -> bool: L, V, C = len(A), set(), collections.deque([S]) while C: _, i = V.update(C), C.popleft() if A[i] == 0: return True if i-A[i] >=0 and i-A[i] not in V: C.append(i-A[i]) if i+A[i] < L and i+A[i] not in V: C.append(i+A[i]) return False - Junaid MAnsuri - Chicago, IL
jump-game-iii
Python 3 (seven lines) (beats 100%) (BFS, Queue, Memo)
junaidmansuri
-1
131
jump game iii
1,306
0.631
Medium
19,507
https://leetcode.com/problems/verbal-arithmetic-puzzle/discuss/1216642/Python3-backtracking
class Solution: def isSolvable(self, words: List[str], result: str) -> bool: if max(map(len, words)) > len(result): return False # edge case words.append(result) digits = [0]*10 mp = {} # mapping from letter to digit def fn(i, j, val): """Find proper mapping for words[i][~j] and result[~j] via backtracking.""" if j == len(result): return val == 0 # base condition if i == len(words): return val % 10 == 0 and fn(0, j+1, val//10) if j >= len(words[i]): return fn(i+1, j, val) if words[i][~j] in mp: if j and j+1 == len(words[i]) and mp[words[i][~j]] == 0: return # backtrack (no leading 0) if i+1 == len(words): return fn(i+1, j, val - mp[words[i][~j]]) else: return fn(i+1, j, val + mp[words[i][~j]]) else: for k, x in enumerate(digits): if not x and (k or j == 0 or j+1 < len(words[i])): mp[words[i][~j]] = k digits[k] = 1 if i+1 == len(words) and fn(i+1, j, val-k): return True if i+1 < len(words) and fn(i+1, j, val+k): return True digits[k] = 0 mp.pop(words[i][~j]) return fn(0, 0, 0)
verbal-arithmetic-puzzle
[Python3] backtracking
ye15
4
398
verbal arithmetic puzzle
1,307
0.348
Hard
19,508
https://leetcode.com/problems/verbal-arithmetic-puzzle/discuss/2547373/Python3-or-Related%3A-Counting-all-possibilities
class Solution: def isSolvable(self, words: List[str], result: str) -> bool: # reverse words words = [i[::-1] for i in words] result = result[::-1] allWords = words + [result] # chars that can not be 0 nonZero = set() for word in allWords: if len(word) > 1: nonZero.add(word[-1]) # numbers selected in backtracking selected = set() # char to Int map charToInt = dict() mxLen = max([len(i) for i in allWords]) def res(i = 0, c = 0, sm = 0): if c == mxLen: return 1 if sm == 0 else 0 elif i == len(words): num = sm % 10 carry = sm // 10 if c >= len(result): if num == 0: return res(0, c+1, carry) else: return 0 # result[c] should be mapped to num if a mapping exists if result[c] in charToInt: if charToInt[result[c]] != num: return 0 else: return res(0, c+1, carry) elif num in selected: return 0 # if mapping does not exist, create a mapping elif (num == 0 and result[c] not in nonZero) or num > 0: selected.add(num) charToInt[result[c]] = num ret = res(0, c + 1, carry) del charToInt[result[c]] selected.remove(num) return ret else: return 0 else: word = words[i] if c >= len(word): return res(i+1, c, sm) elif word[c] in charToInt: return res(i+1, c, sm + charToInt[word[c]]) else: ret = 0 # possibilities for word[c] for j in range(10): if (j == 0 and word[c] not in nonZero) or j > 0: if j not in selected: selected.add(j) charToInt[word[c]] = j ret += res(i + 1, c, sm + j) del charToInt[word[c]] selected.remove(j) return ret return res() > 0
verbal-arithmetic-puzzle
Python3 | Related: Counting all possibilities
DheerajGadwala
0
344
verbal arithmetic puzzle
1,307
0.348
Hard
19,509
https://leetcode.com/problems/verbal-arithmetic-puzzle/discuss/464236/Python-3-(DFS)-(beats-100)-(16-lines)
class Solution: def isSolvable(self, W: List[str], R: str) -> bool: LW, LR, F, ML, AW, V, LMap = len(W), len(R), set([w[0] for w in W+[R]]), max(map(len, W+[R])), W+[R], set(), {} if LR < ML: return False def dfs(d,i,c): if d == ML: return c == 0 if i == len(W) + 1: s = sum(LMap[w[-d-1]] if d < len(w) else 0 for w in W) + c return dfs(d+1,0,s//10) if s % 10 == LMap[R[-d-1]] else False if i < LW and d >= len(W[i]): return dfs(d,i+1,c) ch = AW[i][-d-1] if ch in LMap: return dfs(d,i+1,c) for x in range((ch in F), 10): if x not in V: LMap[ch], _ = x, V.add(x) if dfs(d,i+1,c): return True V.remove(LMap.pop(ch)) return dfs(0,0,0) - Junaid Mansuri - Chicago, IL
verbal-arithmetic-puzzle
Python 3 (DFS) (beats 100%) (16 lines)
junaidmansuri
-1
486
verbal arithmetic puzzle
1,307
0.348
Hard
19,510
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/470770/Python-3-(two-lines)-(beats-100)-(16-ms)-(With-Explanation)
class Solution: def freqAlphabets(self, s: str) -> str: for i in range(26,0,-1): s = s.replace(str(i)+'#'*(i>9),chr(96+i)) return s - Junaid Mansuri - Chicago, IL
decrypt-string-from-alphabet-to-integer-mapping
Python 3 (two lines) (beats 100%) (16 ms) (With Explanation)
junaidmansuri
107
6,100
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,511
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/491897/Python3-scan-string-backward
class Solution: def freqAlphabets(self, s: str) -> str: fn = lambda i: chr(96+int(i)) #convert number to char ans = [] i = len(s)-1 while i >= 0: if s[i] == "#": ans.append(fn(s[i-2:i])) i -= 3 else: ans.append(fn(s[i])) i -= 1 return "".join(reversed(ans))
decrypt-string-from-alphabet-to-integer-mapping
[Python3] scan string backward
ye15
3
197
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,512
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2169355/Python-Simple-Solution
class Solution: def freqAlphabets(self, s: str) -> str: d={'1':'a','2':'b','3':'c','4':'d','5':'e','6':'f','7':'g','8':'h','9':'i','10#':'j','11#':'k','12#':'l','13#':'m','14#':'n','15#':'o','16#':'p','17#':'q','18#':'r','19#':'s','20#':'t','21#':'u','22#':'v','23#':'w','24#':'x','25#':'y','26#':'z'} if len(s)==1: return d[s[0]] if len(s)==2: return d[s[0]]+d[s[1]] i=0 s1="" while(i<len(s)-2): if s[i+2]!='#': s1=s1+d[s[i]] else: s1=s1+d[s[i:i+3]] i=i+2 i=i+1 for i in range(i,len(s)): s1=s1+d[s[i]] return s1
decrypt-string-from-alphabet-to-integer-mapping
Python Simple Solution
pruthashouche
2
88
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,513
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2554688/Decrypt-String-from-Alphabet-to-Integer-Mapping
class Solution: def freqAlphabets(self, s: str) -> str: out = [] res=[] i= 0 while i <len(s)-2: if s[i+2] == "#": out.append(s[i:i+2]+"#") i+=3 else: out.append(s[i]) i+=1 out.append(s[i:]) for i in out : if "#"in i : res.append(chr(ord("a")+(int(i[:-1])-1))) else: for j in i: res.append(chr(ord("a")+int(j)-1)) return "".join(res)
decrypt-string-from-alphabet-to-integer-mapping
Decrypt String from Alphabet to Integer Mapping
dhananjayaduttmishra
1
17
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,514
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2554688/Decrypt-String-from-Alphabet-to-Integer-Mapping
class Solution: def freqAlphabets(self, s: str) -> str: res=[] i= 0 while i <len(s): if i + 2 < len(s) and s[i+2] == "#" : res.append(chr(int(s[i:i + 2]) + 96)) i+=3 else: res.append(chr(96+int(s[i]))) i+=1 return "".join(res)
decrypt-string-from-alphabet-to-integer-mapping
Decrypt String from Alphabet to Integer Mapping
dhananjayaduttmishra
1
17
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,515
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2456943/Python-Simple-Solution
class Solution: def freqAlphabets(self, s: str) -> str: ans = [] for i in range(len(s)): if s[i] == '#': del ans[-2:] ans.append(s[i-2:i]) else: ans.append(s[i]) return ''.join([chr(int(i)+96) for i in ans])
decrypt-string-from-alphabet-to-integer-mapping
Python Simple Solution
xinhuangtien
1
90
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,516
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2246455/Python3-simple-solution
class Solution: def freqAlphabets(self, s: str) -> str: result = "" length = len(s) i = 0 while i < length: if i < length - 2 and s[i+2] == "#": result+= chr(96+int(s[i]+s[i+1])) i+=3 else: result+=chr(96+int(s[i])) i+=1 return result
decrypt-string-from-alphabet-to-integer-mapping
📌 Python3 simple solution
Dark_wolf_jss
1
36
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,517
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1996794/Python-SImple-Logic-or-Easy-Understand
class Solution: def freqAlphabets(self, s: str) -> str: i = len(s) -1 ans = '' while i > -1: if s[i] == '#': temp = s[i-2] + s[i-1] ans = chr(int(temp) + 96) + ans i -= 3 else: ans = chr(int(s[i]) + 96) + ans i -= 1 return ans
decrypt-string-from-alphabet-to-integer-mapping
[Python] SImple Logic | Easy Understand
jamil117
1
75
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,518
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1062188/Python3-solution-efficient-O(n)-times-and-easy-to-understand.-Faster-than-96.55-with-runtime-24-ms
class Solution: def freqAlphabets(self, s: str) -> str: x=[] for i in s: if i!='#': x.append(chr(int(i)+96)) else: x.append(chr(int(str(ord(x.pop(-2)) - 96) + str(ord(x.pop(-1)) - 96))+96)) return ''.join(x)
decrypt-string-from-alphabet-to-integer-mapping
Python3 solution efficient O(n) times and easy to understand. Faster than 96.55 % with runtime 24 ms
coderash1998
1
160
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,519
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/947764/Python-4-lines-solution
class Solution: def freqAlphabets(self, s: str) -> str: for i in range(10,27): s=s.replace(str(i)+'#',chr(96+i)) for i in range(1,10): s=s.replace(str(i),chr(96+i)) return s
decrypt-string-from-alphabet-to-integer-mapping
Python 4-lines solution
lokeshsenthilkumar
1
291
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,520
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2844882/Python3-Solution-beats-92
class Solution: def freqAlphabets(self, s: str) -> str: i , ans = 0, "" while i < len(s): if i + 2 < len(s) and s[i + 2] == "#": ans += chr(96 + int(s[i:i+2])) i += 3 else: ans += chr(96 + int(s[i])) i += 1 return ans
decrypt-string-from-alphabet-to-integer-mapping
Python3 Solution - beats 92%
sipi09
0
1
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,521
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2809599/Decrypt-String-from-alphabet-or-PYTHON
class Solution: def freqAlphabets(self, s: str) -> str: return ''.join(chr(int(i[:2]) + 96) for i in re.findall(r'\d\d#|\d', s))
decrypt-string-from-alphabet-to-integer-mapping
Decrypt String from alphabet | PYTHON
jashii96
0
3
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,522
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2765167/python-solution-considering-string-as-a-stack
class Solution: def freqAlphabets(self, s: str) -> str: alphabet_map = {'1': 'a', '2': 'b', '3': 'c', '4': 'd', '5': 'e', '6': 'f', '7': 'g', '8': 'h', '9': 'i', '10': 'j', '11': 'k', '12': 'l', '13': 'm', '14': 'n', '15': 'o', '16': 'p', '17': 'q', '18': 'r', '19': 's', '20': 't', '21': 'u', '22': 'v', '23': 'w', '24': 'x', '25': 'y', '26': 'z'} new_s = [] i = len(s) - 1 while i >= 0: ch = s[i] if ch == "#": key = s[i-2:i] new_s.append(alphabet_map[key]) i -= 3 else: new_s.append(alphabet_map[ch]) i -= 1 return "".join(new_s[::-1])
decrypt-string-from-alphabet-to-integer-mapping
python solution considering string as a stack
samanehghafouri
0
2
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,523
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2760759/Python3-or-chr-or-map
class Solution: def freqAlphabets(self, s: str) -> str: i, n = 0, len(s) ans = '' while i < n: if i < n - 2 and s[i + 2] == '#': ans += str(chr(int(s[i:i+2]) + 96)) i += 3 else: ans += str(chr(int(s[i]) + 96)) i += 1 return ans
decrypt-string-from-alphabet-to-integer-mapping
Python3 | chr | map
joshua_mur
0
1
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,524
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2608712/An-O(n)-time-and-O(n)-space-approach-without-dictionaries
class Solution: def freqAlphabets(self, s: str) -> str: if len(s) == 1: return chr(int(s) + 96) result, i = [], len(s) - 1 while i >= 0: if s[i] == '#': result.append(chr(int(s[i-2:i]) + 96)) i -= 3 else: result.append(chr(int(s[i]) + 96)) i -= 1 return "".join(reversed(result))
decrypt-string-from-alphabet-to-integer-mapping
An O(n) time and O(n) space approach without dictionaries
kcstar
0
16
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,525
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2585727/Easy-Python-Solution
class Solution: def freqAlphabets(self, s: str) -> str: dic = { "1":"a","2":"b","3":"c","4":"d","5":"e","6":"f","7":"g","8":"h","9":"i","10#":"j","11#":"k","12#":"l","13#":"m","14#":"n","15#":"o","16#":"p","17#":"q","18#":"r","19#":"s","20#":"t","21#":"u","22#":"v","23#":"w","24#":"x","25#":"y","26#":"z" } ans = "" k = len(s) - 1 while k >= 0: temp = "" if s[k] == "#": temp = s[k-2]+ s[k-1] + "#" ans+=dic[temp] k-=3 continue ans+= dic[s[k]] k-=1 continue return ans[::-1]
decrypt-string-from-alphabet-to-integer-mapping
Easy Python Solution
aniketbhamani
0
26
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,526
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2478901/python3-easy-solution-(loop-dictionary)
class Solution: def freqAlphabets(self, s: str) -> str: dct = {'1': 'a', '2': 'b', '3': 'c', '4': 'd', '5': 'e', '6': 'f', '7': 'g', '8': 'h', '9': 'i', '10': 'j', '11': 'k', '12': 'l', '13': 'm', '14': 'n', '15': 'o', '16': 'p', '17': 'q', '18': 'r', '19': 's', '20': 't', '21': 'u', '22': 'v', '23': 'w', '24': 'x', '25': 'y', '26': 'z'} i = 0 res = '' while i < len(s): if i + 2 < len(s) and s[i + 2] == '#' : res += dct[s[i:i+2]] i += 3 else: res = res + dct[s[i]] i += 1 return res
decrypt-string-from-alphabet-to-integer-mapping
python3 easy solution (loop, dictionary)
katyasobol
0
37
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,527
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2412747/Simple-and-easy-to-implement-using-Hash
class Solution: def freqAlphabets(self, s: str) -> str: dat={} for x in range(1,10,1): dat[str(x)]=chr(ord('a')+x-1) for x in range(10,27): dat[str(x)+'#']=chr(ord('a')+x-1) n=len(s) re='' x=0 while x<n: if s[n-x-1] =='#': re=re+dat[s[n-x-3:n-x]] x+=3 else: re=re+dat[s[n-x-1]] x+=1 return re[::-1]
decrypt-string-from-alphabet-to-integer-mapping
Simple and easy to implement using Hash
godemode12
0
41
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,528
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2412458/Python3-Iterative-with-explanation
class Solution: def freqAlphabets(self, s: str) -> str: # This will use ASCII to convert numbers to characters # Uncomment the line below for some useful numbers # print(ord('a'), ord('z'), ord('z')-ord('a')) out = [] # Loop through the string # Read the else clause first. If the element of the input is not '#', append to the output: # - ASCII-to-character of the current character, plus 96 # - The 96 is because we are using '1' to represent 'a', but in ASCII, it is '97' # - So shifting the input number by 96 makes our mapping the same as ASCII # Now, if a "#" is detected: # - remove the last two characters from the output (since these numbers are actually a two-digit num) # - Take the two input numbers from before the # (s[i-2] and s[i-1]) # - add them as strings "1" + "1" = "11", then convert to integer with int() # - then add 96 to bring it to our ASCII mapping # - then convert it to a character from ASCII with chr() for i in range(len(s)): if s[i] == '#': out = out[:-2] out.append(chr( int(s[i-2] + s[i-1]) + 96 )) else: out.append(chr(int(s[i]) + 96)) return ''.join(out)
decrypt-string-from-alphabet-to-integer-mapping
[Python3] Iterative with explanation
connorthecrowe
0
49
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,529
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2373202/Python-Easy-solution-best-for-Interview!
class Solution: def freqAlphabets(self, s: str) -> str: ans="" i=0 while i<len(s): if i+2<len(s) and s[i+2]=="#": no=(int(s[i])*10)+(int(s[i+1])) ans+=chr(no+ord("a")-1) i+=3 else: ans+=chr(int(s[i])+ord("a")-1) i+=1 return ans
decrypt-string-from-alphabet-to-integer-mapping
Python - Easy solution best for Interview!
aady_02
0
60
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,530
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2284083/using-chr-and-traversing-in-reverse-order
class Solution: def freqAlphabets(self, s: str) -> str: out = '' count = len(s) -1 while count >=0: if s[count] == '#': out = chr(96 + int(s[count-2:count]) )+ out count = count - 3 else: out = chr(96 + int(s[count]) ) +out count = count - 1 return out
decrypt-string-from-alphabet-to-integer-mapping
using chr and traversing in reverse order
krishnamsgn
0
28
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,531
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2198731/Python-Simple-Python-Solution-Using-Dictionary
class Solution: def freqAlphabets(self, s: str) -> str: d = {'1':'a','2':'b','3':'c','4':'d','5':'e', '6':'f','7':'g','8':'h','9':'i','10#':'j', '11#':'k','12#':'l','13#':'m','14#':'n', '15#':'o','16#':'p','17#':'q','18#':'r', '19#':'s','20#':'t','21#':'u','22#':'v', '23#':'w','24#':'x','25#':'y','26#':'z'} result = '' i = 0 while i < len(s): if s[i:i+3] in d: result = result + d[s[i:i+3]] i = i + 3 elif s[i] in d: result = result + d[s[i]] i = i + 1 return result
decrypt-string-from-alphabet-to-integer-mapping
[ Python ] ✅✅ Simple Python Solution Using Dictionary 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
97
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,532
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2178804/Python-top-90-solution
class Solution: def freqAlphabets(self, s: str) -> str: from string import ascii_lowercase as let let = list(let) codes = [str(x) for x in range(1,10)]+[str(x)+'#' for x in range(10,27)] for i in range(9,26): s = s.replace(codes[i], let[i]) for i in range(9): s = s.replace(codes[i], let[i]) return s
decrypt-string-from-alphabet-to-integer-mapping
Python top 90 solution
StikS32
0
46
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,533
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2151668/Use-of-ascii_lowercase-method-Python
class Solution: def freqAlphabets(self, s: str) -> str: res = '' i = 0 while i<len(s): if '#' in s[i:i+3]: res +=string.ascii_lowercase[int(s[i:i+2])-1] i += 3 else: res +=string.ascii_lowercase[int(s[i:i+1])-1] i += 1 return res
decrypt-string-from-alphabet-to-integer-mapping
Use of ascii_lowercase method-Python
jayeshvarma
0
23
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,534
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/2004932/Python-Clean-and-Simple!
class Solution: def freqAlphabets(self, s): ans, n = "", len(s) i = 0 while i < n: if i < n-2 and s[i+2] == "#": ans += self.convert(s[i:i+2]) i += 3 else: ans += self.convert(s[i]) i += 1 return ans def convert(self, num): return chr(int(num)+ord("a")-1)
decrypt-string-from-alphabet-to-integer-mapping
Python - Clean and Simple!
domthedeveloper
0
76
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,535
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1990603/Python-or-Easy-to-Understand
class Solution: def freqAlphabets(self, s: str) -> str: # traverse from the end to the front arr = list(s) n = len(arr) i = n -1 result = [] while i >= 0: if arr[i] != '#': result.append(chr(ord('a') + int(arr[i]) - 1)) i -= 1 else: tmp = ''.join(arr[i-2: i]) result.append(chr(ord('a') + int(tmp) - 1)) i -= 3 return ''.join(result[::-1])
decrypt-string-from-alphabet-to-integer-mapping
Python | Easy to Understand
Mikey98
0
53
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,536
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1974407/Easiest-and-Simplest-Approach-or-Python3-Solution-or-100-Faster-or-Using-Dictionary-and-list
class Solution: def freqAlphabets(self, s: str) -> str: d={} temp=[] ss="" alpha=ord('a') for i in range(1,26+1): if i>9: d[str(i)+'#']=chr(alpha) else: d[str(i)]=chr(alpha) alpha+=1 for i in s: if i!='#': temp.append(i) else: st=temp[-2]+temp[-1]+'#' temp.pop() temp.pop() temp.append(st) # print(temp) for k in temp: if k in d: ss=ss+d[k] return (ss)
decrypt-string-from-alphabet-to-integer-mapping
Easiest & Simplest Approach | Python3 Solution | 100% Faster | Using Dictionary and list
RatnaPriya
0
66
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,537
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1870085/Easy-Python-Solution
class Solution: def freqAlphabets(self, s: str) -> str: r = "" i = 0 while i < len(s): if i+2 < len(s) and s[i+2] == "#": r = r + chr(int(s[i:i+2])+96) i = i + 3 else: r = r + chr(int(s[i])+96) i = i + 1 return r
decrypt-string-from-alphabet-to-integer-mapping
Easy Python Solution
shubhamranjan95
0
55
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,538
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1835114/ASCII-Python
class Solution: def freqAlphabets(self, s: str) -> str: last_digit = 0 n = len(s) res = '' while last_digit != n: if (last_digit + 2 <= n-1) and s[last_digit+2] == "#": res += chr(int(s[last_digit:last_digit+2])+96) last_digit = last_digit + 3 else: res += chr(int(s[last_digit])+96) last_digit += 1 return res
decrypt-string-from-alphabet-to-integer-mapping
ASCII Python
Yihang--
0
23
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,539
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1766561/Python-3-or-Simple-solution-or-O(n)
class Solution: def freqAlphabets(self, s: str) -> str: atoz = "abcdefghijklmnopqrstuvwxyz" i = 0 resStr = str() while(i<len(s)): if "#" in s[i:i+3]: resStr += (atoz[int(s[i:i+2])-1]) i += 3 continue resStr += (atoz[int(s[i])-1]) i += 1 return resStr
decrypt-string-from-alphabet-to-integer-mapping
Python 3 | Simple solution | O(n)
Coding_Tan3
0
54
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,540
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1730016/Python-dollarolution
class Solution: def freqAlphabets(self, s: str) -> str: x, i = '', 1 while i < len(s)+1: if s[-i] != '#': x = chr(int(s[-i])+96) + x i += 1 else: i += 1 x = chr(int(s[-i-1:-i+1])+96) + x i += 2 return x
decrypt-string-from-alphabet-to-integer-mapping
Python $olution
AakRay
0
128
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,541
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1718512/python3-or-regex
class Solution: def decrypt(self, value: str) -> str: if len(value) == 3: value = value[:-1] return chr(int(value) + 96) def freqAlphabets(self, s: str) -> str: words = re.findall("[0-9]{2}#|\d", s) return "".join(self.decrypt(l) for l in words)
decrypt-string-from-alphabet-to-integer-mapping
python3 | regex
khalidhassan3011
0
26
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,542
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1601220/Python-3-easy-solution
class Solution: def freqAlphabets(self, s: str) -> str: prev2 = prev1 = None res = '' for c in s: if c == '#': res += chr(int(prev2 + prev1) + 96) prev1 = prev2 = None else: if prev2 is not None: res += chr(int(prev2) + 96) prev2, prev1 = prev1, c if prev2 is not None: res += chr(int(prev2) + 96) if prev1 is not None: res += chr(int(prev1) + 96) return res
decrypt-string-from-alphabet-to-integer-mapping
Python 3 easy solution
dereky4
0
209
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,543
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1195304/My-clean-python
class Solution: def freqAlphabets(self, s: str) -> str: z = ['j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] a = ['X', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] n = s[::-1] q = "" while n != "": if n[0] != "#": q = a[int(n[0])] + q n = n[1:] else: q = z[int(n[2] + n[1]) - 10] + q n = n[3:] return q
decrypt-string-from-alphabet-to-integer-mapping
My clean python
pwily
0
83
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,544
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1137234/Python-Simple-Solution-99.78-faster.
class Solution: def freqAlphabets(self, s: str) -> str: import string l = string.ascii_letters[:26] ans = "" i = 0 while i < len(s): if i+2<len(s) and s[i+2]=='#': j = s[i] + s[i+1] ans += l[int(j)-1] i += 3 else: ans += l[int(s[i])-1] i += 1 return ans
decrypt-string-from-alphabet-to-integer-mapping
Python Simple Solution 99.78% faster.
g_anes_h
0
151
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,545
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1105074/Python-Easy-understanding-solution-faster-than-96.71-and-lesser-memory-than-98.40
class Solution: def freqAlphabets(self, s: str) -> str: res = "" letters = "abcdefghijklmnopqrstuvwxyz" i = 0 while i < len(s): if i+2 < len(s) and s[i+2] == "#": res += letters[int(s[i:i+2])-1] i += 3 else: res += letters[int(s[i])-1] i += 1 return res
decrypt-string-from-alphabet-to-integer-mapping
[Python] Easy-understanding solution, faster than 96.71% and lesser memory than 98.40%
Pandede
0
187
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,546
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/971620/Python3-or-Faster-than-94-using-dictionary-mapping-of-digits
class Solution: def freqAlphabets(self, s: str) -> str: output = "" if not s: return output mapping = { '1': 'a', '2': 'b', '3': 'c', '4': 'd', '5': 'e', '6': 'f', '7': 'g', '8': 'h', '9': 'i', '10#': 'j', '11#': 'k', '12#': 'l', '13#': 'm', '14#': 'n', '15#': 'o', '16#': 'p', '17#': 'q', '18#': 'r', '19#': 's', '20#': 't', '21#': 'u', '22#': 'v', '23#': 'w', '24#': 'x', '25#': 'y', '26#': 'z' } i = 0 while i < len(s): digit = s[i:i+3] if digit in mapping: output += mapping[digit] i += 3 else: output += mapping[s[i]] i += 1 return output
decrypt-string-from-alphabet-to-integer-mapping
Python3 | Faster than 94% using dictionary mapping of digits
sirajali05
0
157
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,547
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/863569/Python-clear-and-faster-greater95
class Solution: def freqAlphabets(self, s: str) -> str: idx = 0 res = [] while idx < len(s): if idx+2 < len(s) and s[idx+2] == "#": res.append( self.num2char( int(s[idx:idx+2]) ) ) idx = idx + 3 else: res.append( self.num2char( int(s[idx]) ) ) idx = idx + 1 return "".join(res) def num2char(self, num): return chr(96 + num)
decrypt-string-from-alphabet-to-integer-mapping
Python clear and faster >95%
dnstanciu
0
135
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,548
https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/discuss/1027230/Python3-easy-to-understand-simple-solution
class Solution: def freqAlphabets(self, s: str) -> str: x = '' i = len(s)-1 while i > -1: if s[i] == '#': if int(s[i-2:i])%26 == 0: x = chr(96+26) + x else: x = chr(96 + int(s[i-2:i])%26) + x i -= 3 else: x = chr(96 + int(s[i])%26) + x i -= 1 return x
decrypt-string-from-alphabet-to-integer-mapping
Python3 easy to understand simple solution
EklavyaJoshi
-1
58
decrypt string from alphabet to integer mapping
1,309
0.795
Easy
19,549
https://leetcode.com/problems/xor-queries-of-a-subarray/discuss/470834/Python-3-(two-lines)-(beats-100)-(412-ms)
class Solution: def xorQueries(self, A: List[int], Q: List[List[int]]) -> List[int]: B = [A[0]] for a in A[1:]: B.append(B[-1]^a) B.append(0) return [B[L-1]^B[R] for L,R in Q]
xor-queries-of-a-subarray
Python 3 (two lines) (beats 100%) (412 ms)
junaidmansuri
5
639
xor queries of a subarray
1,310
0.722
Medium
19,550
https://leetcode.com/problems/xor-queries-of-a-subarray/discuss/470834/Python-3-(two-lines)-(beats-100)-(412-ms)
class Solution: def xorQueries(self, A: List[int], Q: List[List[int]]) -> List[int]: B = list(itertools.accumulate(A, func = operator.xor)) + [0] return [B[L-1]^B[R] for L,R in Q] - Junaid Mansuri - Chicago, IL
xor-queries-of-a-subarray
Python 3 (two lines) (beats 100%) (412 ms)
junaidmansuri
5
639
xor queries of a subarray
1,310
0.722
Medium
19,551
https://leetcode.com/problems/xor-queries-of-a-subarray/discuss/491909/Python3-Prefix-XOR
class Solution: def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: prefix = [0] * (len(arr)+1) for i in range(len(arr)): prefix[i+1] = prefix[i] ^ arr[i] return [prefix[lo] ^ prefix[hi+1] for lo, hi in queries]
xor-queries-of-a-subarray
[Python3] Prefix XOR
ye15
1
197
xor queries of a subarray
1,310
0.722
Medium
19,552
https://leetcode.com/problems/xor-queries-of-a-subarray/discuss/2721475/Python3-or-Prefix-Xor-Array
class Solution: def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: n=len(arr) m=len(queries) prefixXor=[0 for i in range(n)] ans=[] for i in range(n): prefixXor[i]=prefixXor[i-1]^arr[i] if i>=1 else arr[i] for i in range(m): l,r=queries[i] ans.append(prefixXor[r]^prefixXor[l-1] if l>=1 else prefixXor[r]) return ans
xor-queries-of-a-subarray
[Python3] | Prefix Xor Array
swapnilsingh421
0
6
xor queries of a subarray
1,310
0.722
Medium
19,553
https://leetcode.com/problems/xor-queries-of-a-subarray/discuss/2711407/Simple-Prefix-Xor-Python
class Solution: def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: prefix_xor = [0] for i in arr: prefix_xor.append(prefix_xor[-1] ^ i) res = [] for i, j in queries: res.append(prefix_xor[i] ^ prefix_xor[j+1]) return res
xor-queries-of-a-subarray
Simple Prefix Xor Python
anu1rag
0
2
xor queries of a subarray
1,310
0.722
Medium
19,554
https://leetcode.com/problems/xor-queries-of-a-subarray/discuss/2705646/python-easy-solution-or-using-bit-manipulation
class Solution: def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: l=[arr[0]] for i in arr[1:]: l.append(l[-1]^i) #print(l) ans=[] for i in queries: if i[0]==0: ans.append(l[i[1]] ) else: ans.append(l[i[1]]^l[i[0]-1]) return ans
xor-queries-of-a-subarray
python easy solution | using bit manipulation
tush18
0
6
xor queries of a subarray
1,310
0.722
Medium
19,555
https://leetcode.com/problems/xor-queries-of-a-subarray/discuss/2683466/Python3-or-Bit-Manipulation-Trick-O(N-%2B-M)-Time-and-O(N)-Space-Solution!
class Solution: #Let n = len(arr) and m = len(queries)! #Time-Complexity: O(n + m) #Space-Complexity: O(n) def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: #Approach: To get prefix exclusive or from L to R indices, you can first get prefix exclusive #ors from index 0 to last index position in-bouds! Then, to get exclusive or using elements #from arr from L to R, then take prefix[L-1] ^ prefix[R] -> (arr[0] ^ arr[1] ^...^arr[L-1]) ^ #(arr[0] ^ arr[1] ^...^arr[R]) -> the pairs of arr[0] to arr[L-1] will exclusive or to 0 -> #chain of 0 exclusive ors -> the result is arr[L] ^ arr[L+1] ^...^arr[R]! #initialize prefix exclusive or with first element of input array! prefix_or = [arr[0]] for i in range(1, len(arr)): prefix_or.append(prefix_or[i-1] ^ arr[i]) #intialize answer! ans = [] for L, R in queries: L_one_less = None if(L == 0): ans.append(prefix_or[R]) continue L_one_less = prefix_or[L-1] sub_res = prefix_or[R] res = L_one_less ^ sub_res ans.append(res) return ans
xor-queries-of-a-subarray
Python3 | Bit Manipulation Trick O(N + M) Time and O(N) Space Solution!
JOON1234
0
89
xor queries of a subarray
1,310
0.722
Medium
19,556
https://leetcode.com/problems/xor-queries-of-a-subarray/discuss/2669438/Python3-Solution-oror-O(N)-Time-and-O(N)-Space-Complexity
class Solution: def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: xorSum=0 answer=[] for i in range(len(arr)): xorSum^=arr[i] arr[i]=xorSum for l,r in queries: if l==0: answer.append(arr[r]) else: answer.append(arr[r]^arr[l-1]) return answer
xor-queries-of-a-subarray
Python3 Solution || O(N) Time & O(N) Space Complexity
akshatkhanna37
0
6
xor queries of a subarray
1,310
0.722
Medium
19,557
https://leetcode.com/problems/xor-queries-of-a-subarray/discuss/1625207/Python-Solution-or-Bit-Manipulation-or-TC%3A-O(n)-or-90-Faster
class Solution: def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: #build prefix array prefix = [0] * len(arr) prefix[0] = prefix[0] ^ arr[0] for i in range(1, len(arr)): prefix[i] = prefix[i-1] ^ arr[i] result = [] for query in queries: start, end = query if start == 0: result.append(prefix[end]) else: result.append(prefix[start-1] ^ prefix[end]) return result
xor-queries-of-a-subarray
Python Solution | Bit Manipulation | TC: O(n) | 90% Faster
avi-arora
0
75
xor queries of a subarray
1,310
0.722
Medium
19,558
https://leetcode.com/problems/xor-queries-of-a-subarray/discuss/1606538/Python3-Prefix-XORs-solution
class Solution: def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: for i in range(1, len(arr)): arr[i] ^= arr[i - 1] res = [] for x, y in queries: if x == 0: res.append(arr[y]) else: res.append(arr[x - 1] ^ arr[y]) return res
xor-queries-of-a-subarray
[Python3] Prefix XORs solution
maosipov11
0
44
xor queries of a subarray
1,310
0.722
Medium
19,559
https://leetcode.com/problems/xor-queries-of-a-subarray/discuss/1292746/Python3-solution-using-dictionary
class Solution: def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: d = {} z = 0 for i in range(len(arr)): z ^= arr[i] d[(0,i)] = z res = [] for i in queries: if i[0] == i[1]: x = arr[i[0]] elif i[0] == 0: x = d[(i[0],i[1])] else: x = d[(0,i[1])]^arr[i[0]]^d[(0,i[0])] res.append(x) return res
xor-queries-of-a-subarray
Python3 solution using dictionary
EklavyaJoshi
0
49
xor queries of a subarray
1,310
0.722
Medium
19,560
https://leetcode.com/problems/xor-queries-of-a-subarray/discuss/1285590/Python-or-Prefix-of-xor-or-o(n)-TC-and-SC
class Solution: def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: prefix_xor = [num for num in arr] for index in range(1, len(arr)): prefix_xor[index] ^= prefix_xor[index-1] # print(prefix_xor) n = len(queries) ans = [0]*n for i in range(n): l, r = queries[i] ans[i] = prefix_xor[r] ^(prefix_xor[l-1] if l-1>= 0 else 0) return ans
xor-queries-of-a-subarray
Python | Prefix of xor | o(n) TC and SC
Sanjaychandak95
0
72
xor queries of a subarray
1,310
0.722
Medium
19,561
https://leetcode.com/problems/xor-queries-of-a-subarray/discuss/471428/Python3-simple-solution
class Solution: def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: temp,res = [arr[0]],[] for i in range(1,len(arr)): temp.append(temp[i-1]^arr[i]) for query in queries: a,b=query if a == 0: res.append(temp[b]) else: res.append(temp[b]^temp[a-1]) return res
xor-queries-of-a-subarray
Python3 simple solution
jb07
0
80
xor queries of a subarray
1,310
0.722
Medium
19,562
https://leetcode.com/problems/get-watched-videos-by-your-friends/discuss/491936/Python3-Breadth-first-search
class Solution: def watchedVideosByFriends(self, watchedVideos: List[List[str]], friends: List[List[int]], id: int, level: int) -> List[str]: queue = [id] count = 0 seen = set(queue) while queue and count < level: #bfs count += 1 temp = set() for i in queue: for j in friends[i]: if j not in seen: temp.add(j) seen.add(j) queue = temp movies = dict() for i in queue: for m in watchedVideos[i]: movies[m] = movies.get(m, 0) + 1 return [k for _, k in sorted((v, k) for k, v in movies.items())]
get-watched-videos-by-your-friends
[Python3] Breadth-first search
ye15
2
322
get watched videos by your friends
1,311
0.459
Medium
19,563
https://leetcode.com/problems/get-watched-videos-by-your-friends/discuss/1431209/Find-friends-of-friends-84-speed
class Solution: def watchedVideosByFriends(self, watchedVideos: List[List[str]], friends: List[List[int]], id: int, level: int) -> List[str]: friends = [set(lst) for lst in friends] row = friends[id] previous_friends = {id} for _ in range(1, level): previous_friends.update(row) new_row = set() for friend in row: new_row.update(friends[friend]) row = new_row - previous_friends videos = defaultdict(int) for friend in row: for video in watchedVideos[friend]: videos[video] += 1 return [v for _, v in sorted((freq, v) for v, freq in videos.items())]
get-watched-videos-by-your-friends
Find friends of friends, 84% speed
EvgenySH
1
106
get watched videos by your friends
1,311
0.459
Medium
19,564
https://leetcode.com/problems/get-watched-videos-by-your-friends/discuss/470998/Python-3-(six-lines)
class Solution: def watchedVideosByFriends(self, W: List[List[str]], F: List[List[int]], ID: int, K: int) -> List[str]: A, V = set(F[ID]), set([ID]) for _ in range(K-1): A = set(sum([F[a] for a in A],[])) - V - A V = V.union(A) C = collections.Counter(sorted(sum([W[a] for a in A], []))) return sorted(C.keys(), key = lambda x: C[x]) - Junaid Mansuri - Chicago, IL
get-watched-videos-by-your-friends
Python 3 (six lines)
junaidmansuri
1
152
get watched videos by your friends
1,311
0.459
Medium
19,565
https://leetcode.com/problems/get-watched-videos-by-your-friends/discuss/2779957/Python3-or-Dijkstra-%2B-Frequency-Map
class Solution: def watchedVideosByFriends(self, watchedVideos: List[List[str]], friends: List[List[int]], id: int, level: int) -> List[str]: def comparator(a,b): if a[1] > b[1]: return 1 elif a[1] < b[1]: return -1 else: if a[0] > b[0]: return 1 else: return -1 vis = set() freq = defaultdict(int) pq = [[0,id]] while pq: dist,node = heappop(pq) if node not in vis: if dist == level: for videos in watchedVideos[node]: freq[videos]+=1 else: for it in friends[node]: heappush(pq,[dist+1,it]) vis.add(node) sorted_keys = sorted(freq.items(),key = cmp_to_key(comparator)) ans = [key[0] for key in sorted_keys] return ans
get-watched-videos-by-your-friends
[Python3] | Dijkstra + Frequency Map
swapnilsingh421
0
5
get watched videos by your friends
1,311
0.459
Medium
19,566
https://leetcode.com/problems/get-watched-videos-by-your-friends/discuss/1024878/Self-explainatory-code-no-library-used-PYTHON3
class Solution: def watchedVideosByFriends(self, watchedVideos: List[List[str]], friends: List[List[int]], id: int, level: int) -> List[str]: friends_already_visited=set() friends_at_last_level=set([id]) for l in range(0,level): friends_already_visited.update(list(friends_at_last_level)) newfriends=set() for oldfriend in friends_at_last_level: newfriends.update(friends[oldfriend]) friends_at_last_level=newfriends.difference(friends_already_visited) video_freq=dict() for friend in friends_at_last_level: for video in watchedVideos[friend]: if video not in video_freq: video_freq[video]=0 video_freq[video]+=1 return [video for video,freq in sorted(video_freq.items(), key= lambda item : (item[1], item[0]))]
get-watched-videos-by-your-friends
Self explainatory code, no library used [PYTHON3]
v314b0i
0
72
get watched videos by your friends
1,311
0.459
Medium
19,567
https://leetcode.com/problems/get-watched-videos-by-your-friends/discuss/481168/Python3%3A-280ms-(faster-than-99.6)
class Solution: def watchedVideosByFriends(self, watchedVideos: List[List[str]], friends: List[List[int]], id: int, level: int) -> List[str]: visited = [0] * len(friends) visited[id] = level + 1 while level > 0: for i in range(len(visited)): if visited[i] == level + 1: for f in friends[i]: if not visited[f]: visited[f] = level level -= 1 from collections import Counter watched = Counter() for i in range(len(visited)): if visited[i] == 1: watched.update(watchedVideos[i]) d = {} for k, v in watched.items(): if v in d: d[v].append(k) else: d[v] = [k] return sum((sorted(d[freq]) for freq in sorted(d.keys())), [])
get-watched-videos-by-your-friends
Python3: 280ms (faster than 99.6%)
andnik
0
151
get watched videos by your friends
1,311
0.459
Medium
19,568
https://leetcode.com/problems/get-watched-videos-by-your-friends/discuss/470909/Python-BFS
class Solution: def watchedVideosByFriends(self, watchedVideos: List[List[str]], friends: List[List[int]], id: int, level: int) -> List[str]: graph = collections.defaultdict(list) for u, v in enumerate(friends): for i in v: graph[u].append(i) queue = collections.deque() queue.append((id, 0)) visited = set() visited.add(id) res = collections.defaultdict(int) while queue: id, l = queue.popleft() if l == level: for j in watchedVideos[id]: res[j] += 1 for v in graph[id]: if l+1 <= level and v not in visited: visited.add(v) queue.append((v, l+1)) from functools import cmp_to_key def func(x, y): if res[x] > res[y]: return -1 elif res[y] > res[x]: return 1 else: if x > y: return -1 elif y > x: return 1 else: return 0 return (sorted(res.keys(), key=cmp_to_key(func)))[::-1]
get-watched-videos-by-your-friends
Python BFS
aj_to_rescue
0
111
get watched videos by your friends
1,311
0.459
Medium
19,569
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/470856/Python-3-(four-lines)-(DP)-(LCS)
class Solution: def minInsertions(self, S: str) -> int: L = len(S) DP = [[0 for _ in range(L+1)] for _ in range(L+1)] for i,j in itertools.product(range(L),range(L)): DP[i+1][j+1] = DP[i][j] + 1 if S[i] == S[L-1-j] else max(DP[i][j+1],DP[i+1][j]) return L - DP[-1][-1] - Junaid Mansuri - Chicago, IL
minimum-insertion-steps-to-make-a-string-palindrome
Python 3 (four lines) (DP) (LCS)
junaidmansuri
2
537
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,570
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/937752/Python-3-LCS-%2B-LPS.
class Solution: def minInsertions(self, s: str) -> int: if s == s[::-1]: return 0 return len(s) - self.lps(s) def lps(self,s): revs = s[::-1] return self.lcs(s,revs) def lcs(self,a,b): n = len(a) m = len(b) if n==0 or m==0: return 0 dp = [[0]*(m+1) for i in range(n+1)] for i in range(1,n+1): for j in range(1,m+1): if a[i-1] == b[j-1]: dp[i][j] = 1 + dp[i-1][j-1] else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) return dp[n][m]
minimum-insertion-steps-to-make-a-string-palindrome
[Python 3] LCS + LPS.
tilak_
1
245
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,571
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/2815711/PYTHON-DFS-%2B-MEMO-EASY-TO-UNDERSTAND
class Solution: def minInsertions(self, s: str) -> int: @cache def dfs(left,right): if left >= right: return 0 # Characters are the same, move both both pointers inwards if s[left] == s[right]: return dfs(left+1,right-1) # Add char at right pointer, since char added there to match left pointer, left+=1 (vice versa for tryRight) tryLeft = dfs(left+1,right) tryRight = dfs(left,right-1) # Return min of tryLeft and tryRight, +1 because you had to add a char on either the right or left side return min(tryLeft,tryRight)+1 return dfs(0,len(s)-1)
minimum-insertion-steps-to-make-a-string-palindrome
PYTHON DFS + MEMO EASY TO UNDERSTAND
tupsr
0
2
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,572
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/2790172/Simple-Python-Solution-or-Top-Down-orDynamic-Programming
class Solution: def minInsertions(self, s: str) -> int: w1 = s m = len(s) w2 = s[::-1] n = len(s) t = [[-1 for i in range(n + 1)] for j in range(m + 1)] for i in range(m + 1): for j in range(n + 1): if i ==0 or j ==0: t[i][j] = 0 for i in range(1, m + 1): for j in range(1, n + 1): if w1[i - 1] == w2[j - 1]: t[i][j] = 1 + t[i - 1][j - 1] else: t[i][j] = max(t[i - 1][j], t[i][j - 1]) res = (m - t[m][n] ) return res
minimum-insertion-steps-to-make-a-string-palindrome
Simple Python Solution | Top Down |Dynamic Programming
nikhitamore
0
11
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,573
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/2759380/Python-Least-Common-Subsequence.
class Solution: def minInsertions(self, s: str) -> int: dp = [[0 for _ in range(len(s)+1)] for _ in range(len(s)+1)] r = s[::-1] for i in range(1,len(s)+1): for j in range(1,len(s)+1): if s[i-1] == r[j-1]: dp[i][j] = 1+dp[i-1][j-1] else: dp[i][j] = max(dp[i][j-1],dp[i-1][j]) return len(s)-dp[-1][-1]
minimum-insertion-steps-to-make-a-string-palindrome
Python - Least Common Subsequence.
aryan_codes_here
0
6
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,574
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/2664109/Python-by-Reduction-to-LCS-2D-DP-w-Comment
class Solution: def minInsertions(self, s: str) -> int: # we only need k insertion for those non-palindrome characters # where k + len_of_max_palindrome = len(s) len_of_max_palindrome = self.maxPalinSeq(s) return len(s) - len_of_max_palindrome # ------------------------------- @cache def maxPalinSeq(self, s: str) -> int: # input: string # output: maximal length of palindrome sequence inside s ## Base case: # Empty string if not s: return 0 # Only one character if len(s) == 1: return 1 # Only two characters if len(s) == 2: if s[0] == s[1]: return 2 else: return 1 ## General cases if s[0] == s[-1]: # head and tail are with the same character return self.maxPalinSeq( s[1:-1] ) + 2 else: # head and tail are different return max( self.maxPalinSeq( s[:-1] ), self.maxPalinSeq( s[1:] ) )
minimum-insertion-steps-to-make-a-string-palindrome
Python by Reduction to LCS // 2D DP [w/ Comment]
brianchiang_tw
0
28
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,575
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/2599461/Python-Easy-Tabular-Method-oror-DP-oror-LCS-Variation
class Solution: def minInsertions(self, s: str) -> int: def lcs(x,y,n,m): dp = [[-1 for _ in range(m+1)] for _ in range(n+1)] for i in range(n+1): for j in range(m+1): if i==0 or j==0: dp[i][j] = 0 elif x[i-1] == y[j-1]: dp[i][j] = 1 + dp[i-1][j-1] else: dp[i][j] = max(dp[i-1][j],dp[i][j-1]) # for i in dp: # print(i) return dp[-1][-1] return len(s)-lcs(s,s[::-1],len(s),len(s))
minimum-insertion-steps-to-make-a-string-palindrome
Python Easy Tabular Method || DP || LCS Variation
ajinkyabhalerao11
0
50
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,576
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/2592407/Easy-memoization-in-Python
class Solution: @cache def dp(self, s, l, r): if r - l <= 1: return 0 if s[l] == s[r-1]: return self.dp(s, l + 1, r - 1) return min(self.dp(s, l + 1, r), self.dp(s, l, r - 1)) + 1 def minInsertions(self, s: str) -> int: return self.dp(s, 0, len(s))
minimum-insertion-steps-to-make-a-string-palindrome
Easy memoization in Python
metaphysicalist
0
60
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,577
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/2458672/Python-easy-to-read-and-understand-or-lcs
class Solution: def lcs(self, x, y, m, n): if m == 0 or n == 0: return 0 if x[m-1] == y[n-1]: return1 + self.lcs(x, y, m-1, n-1) else: return max(self.lcs(x, y, m-1, n), self.lcs(x, y, m, n-1)) def minInsertions(self, s: str) -> int: m, n = len(s), len(s) return m - self.lcs(s, s[::-1], m, n)
minimum-insertion-steps-to-make-a-string-palindrome
Python easy to read and understand | lcs
sanial2001
0
68
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,578
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/2458672/Python-easy-to-read-and-understand-or-lcs
class Solution: def lcs(self, x, y, m, n): if m == 0 or n == 0: return 0 if (m, n) in self.d: return self.d[(m, n)] if x[m-1] == y[n-1]: self.d[(m, n)] = 1 + self.lcs(x, y, m-1, n-1) return self.d[(m, n)] else: self.d[(m, n)] = max(self.lcs(x, y, m-1, n), self.lcs(x, y, m, n-1)) return self.d[(m, n)] def minInsertions(self, s: str) -> int: self.d = {} m, n = len(s), len(s) return m - self.lcs(s, s[::-1], m, n)
minimum-insertion-steps-to-make-a-string-palindrome
Python easy to read and understand | lcs
sanial2001
0
68
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,579
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/2409250/Python-DP-Solution
class Solution: def minInsertions(self, s: str) -> int: t = "" for i in s: t = i+t d = {} if s == t: return 0 slen = tlen = len(s) def longestCommonSubSequence(index1, index2): if index1 < 0 or index2 < 0: return 0 if s[index1] == t[index2]: if (index1-1, index2-1) not in d: return 1+longestCommonSubSequence(index1-1, index2-1) else: return 1+d[(index1-1, index2-1)] else: if (index1-1, index2) not in d: decreaseSByOne = longestCommonSubSequence(index1-1, index2) else: decreaseSByOne = d[(index1-1, index2)] if (index1, index2-1) not in d: decreaseTByOne = longestCommonSubSequence(index1, index2-1) else: decreaseTByOne = d[(index1, index2-1)] d[(index1, index2)] = max(decreaseTByOne, decreaseSByOne) return d[(index1, index2)] return (slen - longestCommonSubSequence(slen-1, slen-1))
minimum-insertion-steps-to-make-a-string-palindrome
Python DP Solution
DietCoke777
0
23
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,580
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/2324273/Variation-of-LCS-(4-different-Solutions)
class Solution: def minInsertions(self, s: str) -> int: #Tabulation with Optimized Space: t = s[::-1] m = len(s) n = len(t) prev = [0 for j in range(n+1)] for indx1 in range(1, m+1): dp = [0 for j in range(n+1)] for indx2 in range(1, n+1): if s[indx1-1] == t[indx2-1]: dp[indx2] = 1 + prev[indx2-1] else: dp[indx2] = 0 + max(prev[indx2], dp[indx2-1]) prev = dp return len(s) - prev[n] ''' #Memoization to Tabulation t = s[::-1] m = len(s) n = len(t) dp = [[0 for j in range(n+1)] for i in range(m+1)] for indx1 in range(1, m+1): for indx2 in range(1, n+1): if s[indx1-1] == t[indx2-1]: dp[indx1][indx2] = 1 + dp[indx1-1][indx2-1] else: dp[indx1][indx2] = 0 + max(dp[indx1-1][indx2], dp[indx1][indx2-1]) return len(s) - dp[m][n] ''' ''' #Recursion to Memoization def check(indx1, indx2): if indx1 < 0 or indx2 < 0: return 0 if dp[indx1][indx2] != -1: return dp[indx1][indx2] if s[indx1] == t[indx2]: dp[indx1][indx2] = 1 + check(indx1-1, indx2-1) return dp[indx1][indx2] dp[indx1][indx2] = 0 + max(check(indx1-1, indx2), check(indx1, indx2-1)) return dp[indx1][indx2] t = s[::-1] m = len(s) n = len(t) dp = [[-1 for j in range(n)] for i in range(m)] #print(dp) check(m-1, n-1) return len(s) - dp[m-1][n-1] ''' ''' #Recursion $TLE def check(indx1, indx2): if indx1 < 0 or indx2 < 0: return 0 if s[indx1] == t[indx2]: return 1 + check(indx1-1, indx2-1) return 0 + max(check(indx1-1, indx2), check(indx1, indx2-1)) t = s[::-1] m = len(s) n = len(t) return len(s) - check(m-1, n-1) '''
minimum-insertion-steps-to-make-a-string-palindrome
Variation of LCS (4 different Solutions)
iqti005
0
16
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,581
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/1791348/easy-way-with-memoization-but-15
class Solution: def minInsertions(self, s: str) -> int: s1=s[::-1] global table table=[[-1 for i in range(len(s)+1)] for j in range(len(s)+1)] def pss(s,s1,m,n): global table if m==len(s) or n==len(s): return 0 if table[m][n]!=-1: return table[m][n] if s[m]==s1[n]: table[m][n]=1+pss(s,s1,m+1,n+1) else: table[m][n]=max(pss(s,s1,m+1,n),pss(s,s1,m,n+1)) return table[m][n] a=pss(s,s1,0,0) return abs(len(s)-a)
minimum-insertion-steps-to-make-a-string-palindrome
easy way with memoization but 15%
lakanavarapu
0
46
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,582
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/1469458/python-3-solution-greater-len(s)-LCS-(-s-reverse-(s)-)
class Solution: def minInsertions(self, s: str) -> int: def LCS(x,y): n=len(x) m=len(y) dp=[[-1]*(m+1)for i in range (n+1)] #printing length of longest common subsequence for i in range(n+1): for j in range(m+1): if i==0 or j==0: dp[i][j]=0 elif x[i-1]==y[j-1]: dp[i][j]=1+dp[i-1][j-1] else: dp[i][j]=max(dp[i-1][j],dp[i][j-1]) return dp[n][m] return len(s)-LCS(s,s[::-1])
minimum-insertion-steps-to-make-a-string-palindrome
python 3 solution => len(s) - LCS ( s, reverse (s) )
minato_namikaze
0
115
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,583
https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/discuss/549902/Diagonally-filling-the-dp-matrix
class Solution: def minInsertions(self, s: str) -> int: n = len(s) dp = [[0 for i in range(n)] for j in range(n)] r=0 c=1 while c<n: ci = c r = 0 while ci<n and r<n: if s[ci] == s[r]: dp[r][ci] = dp[r+1][ci-1] else: dp[r][ci] = 1+min(dp[r+1][ci],dp[r][ci-1]) ci+=1 r+=1 c+=1 return dp[0][n-1]
minimum-insertion-steps-to-make-a-string-palindrome
Diagonally filling the dp matrix
cursydd
0
171
minimum insertion steps to make a string palindrome
1,312
0.657
Hard
19,584
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/478426/Python-3-(one-line)-(beats-100)
class Solution: def decompressRLElist(self, N: List[int]) -> List[int]: L, A = len(N), [] for i in range(0,L,2): A.extend(N[i]*[N[i+1]]) return A
decompress-run-length-encoded-list
Python 3 (one line) (beats 100%)
junaidmansuri
23
3,400
decompress run length encoded list
1,313
0.859
Easy
19,585
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/478426/Python-3-(one-line)-(beats-100)
class Solution: def decompressRLElist(self, N: List[int]) -> List[int]: return sum([N[i]*[N[i+1]] for i in range(0,len(N),2)],[]) - Junaid Mansuri - Chicago, IL
decompress-run-length-encoded-list
Python 3 (one line) (beats 100%)
junaidmansuri
23
3,400
decompress run length encoded list
1,313
0.859
Easy
19,586
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2011729/Python-Solution
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: answer = [] for i in range(len(nums)//2): answer += nums[2*i]*[nums[2*i+1]] return answer
decompress-run-length-encoded-list
🔴 Python Solution 🔴
alekskram
4
167
decompress run length encoded list
1,313
0.859
Easy
19,587
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1275371/or-Python-3-or-Easy-or-98.76-faster-solution-or
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: result = [] for i in range(0, len(nums), 2): n = [nums[i + 1]] * nums[i] result.extend(n) return result # With love for you
decompress-run-length-encoded-list
| Python 3 | Easy | 98.76% faster solution |
anotherprogramer
3
166
decompress run length encoded list
1,313
0.859
Easy
19,588
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2745785/Python-Solution-or-Beginners-Friendly-or-Explained
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: res =[] for i in range(len(nums)//2): freq,val = nums[2*i] , nums[(2*i)+1] for j in range(freq+1): if j >=1 : res.append(val) return res
decompress-run-length-encoded-list
Python Solution | Beginners Friendly | Explained
sahil193101
2
25
decompress run length encoded list
1,313
0.859
Easy
19,589
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1759518/1313-Decompress-Run-Length-Encoded-List-python-solution-with-runtime-variance
class Solution(object): def decompressRLElist(self, nums): decompressed_list = [] j = 0 for j in range(int(len(nums)/2)): freq , val = nums[2*j] , nums[2*j+1] decompressed_list += [val]*freq return decompressed_list
decompress-run-length-encoded-list
1313 - Decompress Run Length Encoded List - python solution with runtime variance
ankit61d
2
36
decompress run length encoded list
1,313
0.859
Easy
19,590
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2814602/58ms-fastest-python-solution-99.72-fast-and-98.48-memory-efficient
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: k=[] for i in range(0,int(len(nums)/2)): k.extend(list((nums[(2*i)+1],))*nums[2*i]) return k
decompress-run-length-encoded-list
58ms fastest python solution 99.72% fast and 98.48% memory efficient
nandagaonkartik
1
93
decompress run length encoded list
1,313
0.859
Easy
19,591
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1866997/Simple-And-Easy-Python-solution
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: result = [] index = 0 while index < len(nums): frequence = nums[index] value = nums[index + 1] result.extend([value] * frequence) index = index + 2 return result
decompress-run-length-encoded-list
Simple And Easy Python solution
hardik097
1
42
decompress run length encoded list
1,313
0.859
Easy
19,592
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/1372575/Simple-fast-and-easy-to-understand-python-code
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: res = [] for i in range(0, len(nums), 2): j = i + 1 for x in range(0, nums[i]): res.append(int(nums[j])) return res
decompress-run-length-encoded-list
Simple, fast and easy to understand python code
umdowney
1
92
decompress run length encoded list
1,313
0.859
Easy
19,593
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/498102/Python3-faster-than-97.12-with-explanation
class Solution(object): def decompressRLElist(self, nums): # If an array has only two items # we just returns an array with second value and multiply it. if len(nums) == 2: return [nums[1]] * nums[0] result = [] # We're iterating over an array # and checks only even indexes and their value # if an index is even, we need to multiply by the next value # e.g arr[i + 1] for i, v in enumerate(nums): if i % 2 == 0: result += [nums[i + 1]] * v return result
decompress-run-length-encoded-list
Python3, faster than 97.12%, with explanation
myusko
1
175
decompress run length encoded list
1,313
0.859
Easy
19,594
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2849544/Simple-python3-solution
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: ans = [] st = False for i,n in enumerate(nums): if st: for j in range(nums[i-1]): ans.append(n) st=False else: st=True return ans
decompress-run-length-encoded-list
Simple python3 solution
abelops
0
1
decompress run length encoded list
1,313
0.859
Easy
19,595
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2821994/Python3-one-line-beats-97.39
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: return [x for i in range(1,len(nums),2) for x in [nums[i]] * nums[i-1]]
decompress-run-length-encoded-list
Python3 one line beats 97.39%
hcodecode
0
1
decompress run length encoded list
1,313
0.859
Easy
19,596
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2813301/Python3-easy-solution
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: result = [] i = 0 length_nums = len(nums) while (i+i+1) < length_nums: result.extend([nums[2*i+1]]*nums[2*i]) i+=1 return result
decompress-run-length-encoded-list
Python3 easy solution
akhilesh1kr
0
2
decompress run length encoded list
1,313
0.859
Easy
19,597
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2770563/oror-Python-Easy-solution-oror
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: n = len(nums) i = 0 ans = [] while i < n: ans += [nums[i + 1]]*nums[i] i += 2 return ans
decompress-run-length-encoded-list
💯 || Python Easy solution || 💯
parth_panchal_10
0
4
decompress run length encoded list
1,313
0.859
Easy
19,598
https://leetcode.com/problems/decompress-run-length-encoded-list/discuss/2715439/Python%2BNumPy
class Solution: def decompressRLElist(self, nums: List[int]) -> List[int]: import numpy as np return np.concatenate([[nums[i]]*nums[i-1] for i, x in enumerate(nums) if i%2==1])
decompress-run-length-encoded-list
Python+NumPy
Leox2022
0
1
decompress run length encoded list
1,313
0.859
Easy
19,599