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/goat-latin/discuss/1723113/Python-67.79-Faster-98.21-Less-Memory-32ms | class Solution:
def toGoatLatin(self, sentence: str) -> str:
#vars
output = ''
goatword = ''
vowles = list(['a', 'e', 'i', 'o', 'u'])
#enumerate and iterate
for idx, word in enumerate(sentence.split(' ')):
goatword = ''
if word[0:1].l... | goat-latin | Python 67.79 Faster, 98.21% Less Memory, 32ms | ovidaure | -1 | 78 | goat latin | 824 | 0.678 | Easy | 13,400 |
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/2074946/Python-3-or-Three-Methods-(Binary-Search-CounterHashmap-Math)-or-Explanation | class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
ages.sort() # sort the `ages`
ans = 0
n = len(ages)
for idx, age in enumerate(ages): # for each age
lb = age # lower bound
... | friends-of-appropriate-ages | Python 3 | Three Methods (Binary Search, Counter/Hashmap, Math) | Explanation | idontknoooo | 5 | 416 | friends of appropriate ages | 825 | 0.464 | Medium | 13,401 |
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/2074946/Python-3-or-Three-Methods-(Binary-Search-CounterHashmap-Math)-or-Explanation | class Solution(object):
def numFriendRequests(self, ages):
count = [0] * 121 # counter: count frequency of each age
for age in ages:
count[age] += 1
ans = 0
for ageA, countA in enumerate(count): # nested loop, pretty straightforward
... | friends-of-appropriate-ages | Python 3 | Three Methods (Binary Search, Counter/Hashmap, Math) | Explanation | idontknoooo | 5 | 416 | friends of appropriate ages | 825 | 0.464 | Medium | 13,402 |
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/2074946/Python-3-or-Three-Methods-(Binary-Search-CounterHashmap-Math)-or-Explanation | class Solution:
def numFriendRequests(self, ages):
count = [0] * 121 # counter: count frequency of each age
for age in ages:
count[age] += 1
prefix = [0] * 121 # prefix-sum: prefix sum of frequency, we will use this for r... | friends-of-appropriate-ages | Python 3 | Three Methods (Binary Search, Counter/Hashmap, Math) | Explanation | idontknoooo | 5 | 416 | friends of appropriate ages | 825 | 0.464 | Medium | 13,403 |
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/2505228/Python-Time%3A-O(max(N-120))-Space-O(1)-Prefixsum-and-Numbersort-Solution | class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
# make a number sort
sort_ages = [0]*120
# sort the ages
for age in ages:
sort_ages[age-1] += 1
# make prefix sum
for age in range(2,121):
sort_... | friends-of-appropriate-ages | [Python] - Time: O(max(N, 120)) - Space O(1) - Prefixsum and Numbersort Solution | Lucew | 1 | 95 | friends of appropriate ages | 825 | 0.464 | Medium | 13,404 |
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/1847580/python-3-oror-two-solutions | class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
deque = collections.deque()
ages.sort(reverse=True)
res = 0
curSame = 0
for i, age in enumerate(ages):
if i and age >= 15 and age == ages[i-1]:
curSame += 1
e... | friends-of-appropriate-ages | python 3 || two solutions | dereky4 | 1 | 140 | friends of appropriate ages | 825 | 0.464 | Medium | 13,405 |
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/1847580/python-3-oror-two-solutions | class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
prefixSum = collections.Counter(ages)
for i in range(2, 121):
prefixSum[i] += prefixSum[i-1]
res = 0
for age in ages:
left = int(0.5*age + 7)
if age > left:
... | friends-of-appropriate-ages | python 3 || two solutions | dereky4 | 1 | 140 | friends of appropriate ages | 825 | 0.464 | Medium | 13,406 |
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/1646281/Python-Easy-Solution-or-Best-Approach | class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
count = 0
ages = Counter(ages)
for x in ages:
xCount = ages[x]
for y in ages:
if not (y <= 0.5*x+7 or y > x):
yCount = ages[y]
if x != y:
count += xCount*yCount
else:
count += xCount*(xCount-1)
return coun... | friends-of-appropriate-ages | Python Easy Solution | Best Approach ✔ | leet_satyam | 1 | 220 | friends of appropriate ages | 825 | 0.464 | Medium | 13,407 |
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/934783/Python3-two-approaches | class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
ages.sort()
ans = lo = hi = 0
for x in ages:
while hi < len(ages) and x == ages[hi]: hi += 1
while lo+1 < hi and ages[lo] <= x//2 + 7: lo += 1
ans += hi - lo - 1
return ans | friends-of-appropriate-ages | [Python3] two approaches | ye15 | 1 | 89 | friends of appropriate ages | 825 | 0.464 | Medium | 13,408 |
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/934783/Python3-two-approaches | class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
freq = {}
for x in ages: freq[x] = 1 + freq.get(x, 0)
ans = 0
for x in freq:
for y in freq:
if 0.5*x + 7 < y <= x:
ans += freq[x] * freq[y]
... | friends-of-appropriate-ages | [Python3] two approaches | ye15 | 1 | 89 | friends of appropriate ages | 825 | 0.464 | Medium | 13,409 |
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/1785489/Python-Binary-Search | class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
if len(ages) == 1:
return 0
self.counts = collections.defaultdict(int)
def binary_search(ages, idx):
start, end = 0, idx-1
result = -1
while sta... | friends-of-appropriate-ages | Python - Binary Search | shubhamadep007 | 0 | 170 | friends of appropriate ages | 825 | 0.464 | Medium | 13,410 |
https://leetcode.com/problems/friends-of-appropriate-ages/discuss/527552/Python3-simple-solution | class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
requests = 0
ages_le = [0 for _ in range(121)]
for age in ages:
ages_le[age] += 1
for index in range(1, 121):
ages_le[index] += ages_le[index-1]
for age in ages:
age_lower... | friends-of-appropriate-ages | Python3 simple solution | tjucoder | 0 | 98 | friends of appropriate ages | 825 | 0.464 | Medium | 13,411 |
https://leetcode.com/problems/most-profit-assigning-work/discuss/2603913/Python3-or-Solved-Using-Binary-Search-W-Sorting-O((n%2Bm)*logn)-Runtime-Solution! | class Solution:
#Time-Complexity: O(n + nlogn + n + mlog(n)) -> O((n+m) *logn)
#Space-Complexity: O(n)
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
#Approach: First of all, linearly traverse each and every corresponding index
#position of... | most-profit-assigning-work | Python3 | Solved Using Binary Search W/ Sorting O((n+m)*logn) Runtime Solution! | JOON1234 | 2 | 134 | most profit assigning work | 826 | 0.446 | Medium | 13,412 |
https://leetcode.com/problems/most-profit-assigning-work/discuss/1409945/Simple-Python-O(nlogn%2Bmlogm)-sort%2Bgreedy-solution | class Solution:
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
# sort difficulty and profit together as a tuple
difficulty, profit = zip(*sorted(zip(difficulty, profit)))
ret = max_profit = idx = 0
for ability in sorted(worker):
... | most-profit-assigning-work | Simple Python O(nlogn+mlogm) sort+greedy solution | Charlesl0129 | 1 | 170 | most profit assigning work | 826 | 0.446 | Medium | 13,413 |
https://leetcode.com/problems/most-profit-assigning-work/discuss/934864/Python3-two-approaches | class Solution:
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
mp = {}
mx = 0
for x, y in sorted(zip(difficulty, profit)):
mp[x] = max(mp.get(x, 0), mx := max(mx, y))
arr = list(mp.keys()) # ordered since 3.6
... | most-profit-assigning-work | [Python3] two approaches | ye15 | 1 | 91 | most profit assigning work | 826 | 0.446 | Medium | 13,414 |
https://leetcode.com/problems/most-profit-assigning-work/discuss/934864/Python3-two-approaches | class Solution:
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
job = sorted(zip(difficulty, profit))
ans = i = mx = 0
for w in sorted(worker):
while i < len(job) and job[i][0] <= w:
mx = max(mx, job[i][1])
... | most-profit-assigning-work | [Python3] two approaches | ye15 | 1 | 91 | most profit assigning work | 826 | 0.446 | Medium | 13,415 |
https://leetcode.com/problems/most-profit-assigning-work/discuss/2835053/Binary-search-and-precalculate-max-profit | class Solution:
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
res = 0
for i in range(len(worker)):
max_p = 0
for j in range(len(difficulty)):
if difficulty[j] <= worker[i]:
max_p = max(m... | most-profit-assigning-work | Binary search and precalculate max profit | michaelniki | 0 | 2 | most profit assigning work | 826 | 0.446 | Medium | 13,416 |
https://leetcode.com/problems/most-profit-assigning-work/discuss/2835053/Binary-search-and-precalculate-max-profit | class Solution:
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
res = 0
hashmap_profit = defaultdict(int)
for i in range(len(difficulty)):
hashmap_profit[difficulty[i]] = max(hashmap_profit[difficulty[i]], profit[i])
... | most-profit-assigning-work | Binary search and precalculate max profit | michaelniki | 0 | 2 | most profit assigning work | 826 | 0.446 | Medium | 13,417 |
https://leetcode.com/problems/most-profit-assigning-work/discuss/2779564/Python-Binary-Search | class Solution:
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
jobs = list(zip(difficulty, profit))
jobs.append((0,0))
jobs.sort(key=lambda j: [j[0], j[1]])
maxPay = 0
for i, job in enumerate(jobs):
jobDiff, jo... | most-profit-assigning-work | Python - Binary Search | GavSwe | 0 | 8 | most profit assigning work | 826 | 0.446 | Medium | 13,418 |
https://leetcode.com/problems/most-profit-assigning-work/discuss/2761062/Pythonoror-Binary-Search-Solution-Easy-to-understand | class Solution:
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
"""
idea:
- zip difficulty and profit
- sort by difficulty
- iterate through each worker's ability (worker)
- find the greatest difficulty u... | most-profit-assigning-work | Python|| Binary Search Solution Easy to understand | avgpersonlargetoes | 0 | 17 | most profit assigning work | 826 | 0.446 | Medium | 13,419 |
https://leetcode.com/problems/most-profit-assigning-work/discuss/2733966/Binary-search | class Solution:
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
"""
Greedy --> pair up each worker with job of largest profit that can be done
brute force -> pair up profit and difficulty arrays, sort by profit, then for each worker, find th... | most-profit-assigning-work | Binary search | berkeley_upe | 0 | 11 | most profit assigning work | 826 | 0.446 | Medium | 13,420 |
https://leetcode.com/problems/most-profit-assigning-work/discuss/2023243/Python-O(n)-hashtable-solution | class Solution:
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
d = defaultdict(int)
for k,v in zip(difficulty,profit):
d[k] = max(d[k],v)
bucket = [0 for _ in range(max(worker)+1)]
val = 0
for i in range(len(buck... | most-profit-assigning-work | Python O(n) hashtable solution | yusianglin11010 | 0 | 59 | most profit assigning work | 826 | 0.446 | Medium | 13,421 |
https://leetcode.com/problems/making-a-large-island/discuss/1340782/Python-Clean-DFS | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
N = len(grid)
DIRECTIONS = [(-1, 0), (0, -1), (0, 1), (1, 0)]
address = {}
def dfs(row, column, island_id):
queue = deque([(row, column, island_id)])
visited.add((row, column))
... | making-a-large-island | [Python] Clean DFS | soma28 | 4 | 1,000 | making a large island | 827 | 0.447 | Hard | 13,422 |
https://leetcode.com/problems/making-a-large-island/discuss/1310016/Python3-union-find | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
n = len(grid)
v = 2
freq = defaultdict(int)
for r in range(n):
for c in range(n):
if grid[r][c] == 1:
stack = [(r, c)]
grid[r][c] = v
... | making-a-large-island | [Python3] union-find | ye15 | 4 | 371 | making a large island | 827 | 0.447 | Hard | 13,423 |
https://leetcode.com/problems/making-a-large-island/discuss/1377243/python-3-solution-oror-clean-oror-80-fast-oror-dfs | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
directions=[[1,0],[-1,0],[0,1],[0,-1]]
def getislandsize(grid,i,j,islandID):
if i <0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j]!=1:
return 0
grid[i][j]=islandID
... | making-a-large-island | python 3 solution || clean || 80 % fast || dfs | minato_namikaze | 2 | 301 | making a large island | 827 | 0.447 | Hard | 13,424 |
https://leetcode.com/problems/making-a-large-island/discuss/1376002/Python-simple-python-dfs-with-value-markers | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
gsize = [0, 0] # Size of each group, index start from 2
cur = 2
def dfs(i, j, cur):
if i < 0 or j < 0 or i == len(grid) or j == len(grid[0]) or grid[i][j] != 1: return
gsize[cur] += 1
... | making-a-large-island | [Python] simple python dfs with value markers | cyshih | 2 | 437 | making a large island | 827 | 0.447 | Hard | 13,425 |
https://leetcode.com/problems/making-a-large-island/discuss/995707/python-dfs-solution | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
# inside dfs, update the area of the current island and
# update the boundary cell to include this island as adjacent
def _dfs(i, j):
visited.add((i,j))
areas[id] += 1
for dx, dy in [(-1, 0),... | making-a-large-island | python dfs solution | ChiCeline | 1 | 386 | making a large island | 827 | 0.447 | Hard | 13,426 |
https://leetcode.com/problems/making-a-large-island/discuss/919336/Python3-DFS-(easy-to-understand) | class Solution:
def __init__(self):
self.res = 0
self.island_id = 2
def largestIsland(self, grid: List[List[int]]) -> int:
ans = 0
def dfs(i, j):
if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] == 1 and (i, ... | making-a-large-island | Python3 DFS (easy to understand) | ermolushka2 | 1 | 205 | making a large island | 827 | 0.447 | Hard | 13,427 |
https://leetcode.com/problems/making-a-large-island/discuss/2812348/Python-check-boundaries-of-islands. | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
n = len(grid)
directions = [-1, 0, 1, 0, -1]
islands = list()
visited = [[False for _ in range(n)] for _ in range(n)]
def helper(i, j):
visited[i][j] = True
island = [(i, j)]
... | making-a-large-island | Python, check boundaries of islands. | yiming999 | 0 | 2 | making a large island | 827 | 0.447 | Hard | 13,428 |
https://leetcode.com/problems/making-a-large-island/discuss/2480366/python-3-or-dfs-or-O(n2)O(n2) | class Solution:
DIRECTIONS = (-1, 0), (1, 0), (0, -1), (0, 1)
def neighbours(self, i, j):
return ((i + di, j + dj) for di, dj in Solution.DIRECTIONS
if 0 <= i + di < self.n and 0 <= j + dj < self.n)
def largestIsland(self, grid: List[List[int]]) -> int:
self.n = len... | making-a-large-island | python 3 | dfs | O(n^2)/O(n^2) | dereky4 | 0 | 51 | making a large island | 827 | 0.447 | Hard | 13,429 |
https://leetcode.com/problems/making-a-large-island/discuss/2442710/Making-a-large-island-oror-Python3-oror-DFS | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
# map to strore index of component along with number of nodes in that component
map = {}
index = 1
for i in range(0, len(grid)):
for j in range(0, len(grid[0])):
if(gr... | making-a-large-island | Making a large island || Python3 || DFS | vanshika_2507 | 0 | 36 | making a large island | 827 | 0.447 | Hard | 13,430 |
https://leetcode.com/problems/making-a-large-island/discuss/2103318/Python-oror-DFS-oror-Beats-90%2B | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
n = len(grid)
stack = []
tag, area_dict = 2, {}
for i in range(n):
for j in range(n):
if grid[i][j] == 1:
area = 0
stack.append((i, j))
... | making-a-large-island | Python || DFS || Beats 90%+ | Tequila-Sunrise | 0 | 64 | making a large island | 827 | 0.447 | Hard | 13,431 |
https://leetcode.com/problems/making-a-large-island/discuss/2063690/Python-easy-to-read-and-understand-or-graph | class Solution:
def dfs(self, grid, row, col):
if row < 0 or col < 0 or row == len(grid) or col == len(grid[0]) or grid[row][col] != 1:
return 0
grid[row][col] = 2
x1 = self.dfs(grid, row-1, col)
x2 = self.dfs(grid, row, col-1)
x3 = self.dfs(grid, row+1, col)
... | making-a-large-island | Python easy to read and understand | graph | sanial2001 | 0 | 85 | making a large island | 827 | 0.447 | Hard | 13,432 |
https://leetcode.com/problems/making-a-large-island/discuss/2063690/Python-easy-to-read-and-understand-or-graph | class Solution:
def dfs(self, grid, row, col, Id):
if row < 0 or col < 0 or row == len(grid) or col == len(grid[0]) or grid[row][col] != 1:
return 0
grid[row][col] = Id
t = self.dfs(grid, row-1, col, Id)
l = self.dfs(grid, row, col-1, Id)
d = self.dfs(grid, row+1,... | making-a-large-island | Python easy to read and understand | graph | sanial2001 | 0 | 85 | making a large island | 827 | 0.447 | Hard | 13,433 |
https://leetcode.com/problems/making-a-large-island/discuss/1939285/Python-DFS-readable-solution-O(n2) | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
islands = {}
position_mapping = {}
island_id = 0
largest_island = [1]
for y in range(len(grid)):
for x in range(len(grid[0])):
if grid[y][x] == 1 and (y,x) not in position_mappi... | making-a-large-island | Python DFS readable solution O(n^2) | user1267LD | 0 | 81 | making a large island | 827 | 0.447 | Hard | 13,434 |
https://leetcode.com/problems/making-a-large-island/discuss/1746947/Python-O(N)-solution | class Solution:
def __init__(self):
self.directions = [(-1,0),(0,1),(1,0),(0,-1)]
def largestIsland(self, grid: List[List[int]]) -> int:
self.grid = grid
self.rows, self.cols = len(self.grid), len(self.grid[0])
self.visited, self.islands, self.zeros = set(), [], set()
for i in range(self.rows):
for j in... | making-a-large-island | Python O(N) solution | dotaneli | 0 | 110 | making a large island | 827 | 0.447 | Hard | 13,435 |
https://leetcode.com/problems/making-a-large-island/discuss/1618228/Python-Solution | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
island_map = [[0]*len(grid) for _ in range(len(grid))]
#Colour coding the islands
def dfs(row,col,curr):
if island_map[col][row] == 0 and grid[col][row]==1:
island_map[col][row] = curr... | making-a-large-island | Python Solution | user7387N | 0 | 89 | making a large island | 827 | 0.447 | Hard | 13,436 |
https://leetcode.com/problems/making-a-large-island/discuss/1612945/Help-with-Py3-code | class Solution:
def largestIsland(self, grid) -> int:
def search(i,j,color):
nonlocal m,n,d
if i<0 or j<0 or i>=m or j>=n or grid[i][j]!=1:
return
grid[i][j] = color
d[color] += 1
search(i-1,j,color)
search(i+1,j,color)
... | making-a-large-island | Help with Py3 code | ys258 | 0 | 41 | making a large island | 827 | 0.447 | Hard | 13,437 |
https://leetcode.com/problems/making-a-large-island/discuss/1560934/Well-commented-clean-python3-DFS-O(M*N)-Beats-time-83.04-space-75.5 | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
# If there is nothing in the grid then return zero
if not grid:
return 0
# Mark every conntected island with a unique number, starting from 2
island_num = 2
# Nes... | making-a-large-island | Well commented, clean python3, DFS, O(M*N), Beats time 83.04%, space 75.5% | hiqbal | 0 | 93 | making a large island | 827 | 0.447 | Hard | 13,438 |
https://leetcode.com/problems/making-a-large-island/discuss/1242056/Python-DFS-with-a-clean-implementation | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
rowcount = colcount = len(grid)
areas = {}
maxsize = 0
#Index is used to mark discovered cells. Starts from 2 to avoid collision with already existing values 0 and 1.
index = 2
#Simpl... | making-a-large-island | Python, DFS with a clean implementation | swissified | 0 | 208 | making a large island | 827 | 0.447 | Hard | 13,439 |
https://leetcode.com/problems/making-a-large-island/discuss/1165633/Python-O(N2)-Explore-beaches | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
n = len(grid)
shifts = [(-1, 0), (1, 0), (0, 1), (0, -1)]
islands = {0: [0, set()]} # island_id: [area, beach]. 0 to represent ocean
def move(x, y):
for dx, dy in shifts:
if (... | making-a-large-island | [Python] O(N^2) Explore beaches | louis925 | 0 | 106 | making a large island | 827 | 0.447 | Hard | 13,440 |
https://leetcode.com/problems/making-a-large-island/discuss/643108/Python3-O(N2)-Time-O(1)-Extra-Space-100-fast-100-memory | class Solution:
def largestIsland(self, grid: List[List[int]]) -> int:
mark = 2
islands = []
n = len(grid)
def mark_it(x, y): # marks island and returns square of that island
grid[x][y] = mark
s = 1
for xn in (x - 1, x + 1):
... | making-a-large-island | [Python3] O(N^2) Time; O(1) Extra Space; 100% fast; 100% memory | timetoai | 0 | 176 | making a large island | 827 | 0.447 | Hard | 13,441 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/2140546/Python-O(n)-with-intuition-step-by-step-thought-process | class Solution:
def uniqueLetterString(self, s: str) -> int:
r=0
for i in range(len(s)):
for j in range(i, len(s)):
ss=s[i:j+1]
unique=sum([ 1 for (i,v) in Counter(ss).items() if v == 1 ])
r+=unique
return r | count-unique-characters-of-all-substrings-of-a-given-string | Python O(n) with intuition / step-by-step thought process | alskdjfhg123 | 6 | 354 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,442 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/2140546/Python-O(n)-with-intuition-step-by-step-thought-process | class Solution:
def uniqueLetterString(self, s: str) -> int:
def does_char_appear_once(sub, t):
num=0
for c in sub:
if c==t:
num+=1
return num==1
r=0
for c in string.ascii_uppercase:
for i in ran... | count-unique-characters-of-all-substrings-of-a-given-string | Python O(n) with intuition / step-by-step thought process | alskdjfhg123 | 6 | 354 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,443 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/2140546/Python-O(n)-with-intuition-step-by-step-thought-process | class Solution:
def uniqueLetterString(self, s):
indices=defaultdict(list)
for i in range(len(s)):
indices[s[i]].append(i)
r=0
for k,v in indices.items():
for i in range(len(v)):
if i==0:
prev=-1
els... | count-unique-characters-of-all-substrings-of-a-given-string | Python O(n) with intuition / step-by-step thought process | alskdjfhg123 | 6 | 354 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,444 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/2140546/Python-O(n)-with-intuition-step-by-step-thought-process | class Solution:
def uniqueLetterString(self, s):
indices=defaultdict(list)
for i in range(len(s)):
indices[s[i]].append(i)
r=0
for k,v in indices.items():
for i in range(len(v)):
curr=v[i]
if i==0:
p... | count-unique-characters-of-all-substrings-of-a-given-string | Python O(n) with intuition / step-by-step thought process | alskdjfhg123 | 6 | 354 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,445 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/1377763/Python3-greedy | class Solution:
def uniqueLetterString(self, s: str) -> int:
locs = [[-1] for _ in range(26)]
for i, x in enumerate(s): locs[ord(x)-65].append(i)
ans = 0
for i in range(26):
locs[i].append(len(s))
for k in range(1, len(locs[i])-1):
... | count-unique-characters-of-all-substrings-of-a-given-string | [Python3] greedy | ye15 | 3 | 644 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,446 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/2564451/Python-simple-O(n) | class Solution:
def uniqueLetterString(self, s: str) -> int:
prev = [-1] * len(s)
nex = [len(s)] * len(s)
index = {}
for i, c in enumerate(s):
if c in index:
prev[i] = index[c]
index[c] = i
index = {}
for i in range(len(s) - 1, -1, -1):
if s[i] in index:
nex[i] = index[s[i]]
index... | count-unique-characters-of-all-substrings-of-a-given-string | Python simple O(n) | shubhamnishad25 | 1 | 106 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,447 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/750028/Two-approaches-in-Python-(One-is-AC-and-the-other-is-TLE) | class Solution:
def uniqueLetterString(self, s: str) -> int:
mem, res, mod = [[-1] for _ in range(26)], 0, 1000000007
# ord('A') = 65
for i in range(len(s)):
l = mem[ord(s[i]) - 65]
l.append(i)
if len(l) > 2: res = (res + (l[-1] - l[-2]) * (l[-2] - l[-3]))... | count-unique-characters-of-all-substrings-of-a-given-string | Two approaches in Python (One is AC and the other is TLE) | samparly | 1 | 408 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,448 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/2774192/7-Line-Python-DP-O(N)-Time-O(1)-Space | class Solution:
def uniqueLetterString(self, s: str) -> int:
pre, ans, last_i, second_last_i = 0, 0, [-1] * 26, [-1] * 26
for i in range(len(s)):
order = ord(s[i]) - ord('A')
pre += i - last_i[order] - (last_i[order] - second_last_i[order])
ans += pre
... | count-unique-characters-of-all-substrings-of-a-given-string | 7 Line Python / DP / O(N) Time / O(1) Space | GregHuang | 0 | 7 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,449 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/2757889/Python-Solution | class Solution:
def uniqueLetterString(self, s: str) -> int:
# default value from left is -1 and default value from the right is length of array
# the difference is right minus left pointer
LENGTH = len(s)
left_map: dict[str, int] = {}
left: list[int] = []
right_map:... | count-unique-characters-of-all-substrings-of-a-given-string | Python Solution | ugookoh | 0 | 8 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,450 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/2631577/Very-short-one-pass-O(n)-solution-in-Python-easy-to-understand | class Solution:
def uniqueLetterString(self, s: str) -> int:
last = {}
ans = 0
step_sum = 0
for i, c in enumerate(s):
if c not in last:
last[c] = [-1, i]
else:
step_sum -= (last[c][1] - last[c][0])
last[c] = [las... | count-unique-characters-of-all-substrings-of-a-given-string | Very short one-pass O(n) solution in Python, easy to understand | metaphysicalist | 0 | 36 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,451 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/2602956/Python-3-or-simple-solution-or-O(n)O(1) | class Solution:
def uniqueLetterString(self, s: str) -> int:
prev = collections.defaultdict(lambda: (-1, -1))
res = 0
for i, c in enumerate(s):
prev2, prev1 = prev[c]
res += (prev1 - prev2) * (i - prev1)
prev[c] = prev1, i
n = len(s)
... | count-unique-characters-of-all-substrings-of-a-given-string | Python 3 | simple solution | O(n)/O(1) | dereky4 | 0 | 99 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,452 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/2315652/Python-Idea-explain-Very-simple-solution | class Solution:
def uniqueLetterString(self, s: str) -> int:
## RC ##
## APPROACH: SUBARRAY ##
## LOGIC ##
## 1. Translate this prob to sub-prob => what is the max len of subarray where s[i] is unique ?
## 2. Particular character s[i] is unique can be found checking the next ... | count-unique-characters-of-all-substrings-of-a-given-string | [Python] Idea explain, Very simple solution, | 101leetcode | 0 | 303 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,453 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/2270505/Shortest-Python-Solution-you'll-see.-O(26n) | class Solution:
def uniqueLetterString(self, s: str) -> int:
prev,curr = defaultdict(int),defaultdict(int)
ans = 0
for i,x in enumerate(s):
curr[x] = i - prev[x] + 1
ans += sum(curr.values())
prev[x] = i + 1
return ans | count-unique-characters-of-all-substrings-of-a-given-string | Shortest Python Solution you'll see. O(26n) | pradhyumnjain10 | 0 | 181 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,454 |
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string/discuss/1832382/Dynamic-Programming-Solution-(time-limit-exceeded-) | class Solution:
def uniqueLetterString(self, s: str) -> int:
res = 0
n = len(s)
# dp_keys stores all keys in a substring
dp_keys = [[set() for _ in range(n)] for _ in range(n)]
# dp_unqs stores all unique characters in a substring
dp_unqs = [[set() for _ in range(n)] for _ in range(n)]
# dp_count... | count-unique-characters-of-all-substrings-of-a-given-string | Dynamic Programming Solution (time limit exceeded ) | Mujojo | 0 | 365 | count unique characters of all substrings of a given string | 828 | 0.517 | Hard | 13,455 |
https://leetcode.com/problems/consecutive-numbers-sum/discuss/1466133/8-lines-Python3-code | class Solution:
def consecutiveNumbersSum(self, n: int) -> int:
csum=0
result=0
for i in range(1,n+1):
csum+=i-1
if csum>=n:
break
if (n-csum)%i==0:
result+=1
return result | consecutive-numbers-sum | 8 lines Python3 code | tongho | 6 | 705 | consecutive numbers sum | 829 | 0.415 | Hard | 13,456 |
https://leetcode.com/problems/consecutive-numbers-sum/discuss/2150748/PYTHON-oror-EXPLAINED-oror | class Solution:
def consecutiveNumbersSum(self, n: int) -> int:
i=1
res=0
k=int((n*2)**0.5)
while i<=k:
if i%2:
if n%i==0:
res+=1
elif (n-(i//2))%i==0:
res+=1
i+=1
return res | consecutive-numbers-sum | ✔️ PYTHON || EXPLAINED || ;] | karan_8082 | 5 | 328 | consecutive numbers sum | 829 | 0.415 | Hard | 13,457 |
https://leetcode.com/problems/consecutive-numbers-sum/discuss/994601/Python3-6-lines-O(sqrt(n))-solution-with-simple-math | class Solution:
def consecutiveNumbersSum(self, N: int) -> int:
'''
let a be the starting number and k be the number of terms
a + (a + 1) + ... (a + k - 1) = N
(2a + k - 1) * k / 2 = N
Since (k + 2a - 1) * k = 2N, k < sqrt(2N)
On the other hand, the above equation can be turned i... | consecutive-numbers-sum | Python3 6 lines O(sqrt(n)) solution with simple math | haozhu233 | 3 | 516 | consecutive numbers sum | 829 | 0.415 | Hard | 13,458 |
https://leetcode.com/problems/consecutive-numbers-sum/discuss/1603568/Python-simple-and-easy-no-SQRT-no-complex-math | class Solution:
def consecutiveNumbersSum(self, n: int) -> int:
count = 0
i = 1
while (n > 0):
n -= i
if n%i == 0:
count += 1
i += 1
return count | consecutive-numbers-sum | Python simple & easy no SQRT, no complex math | ranasaani | 2 | 477 | consecutive numbers sum | 829 | 0.415 | Hard | 13,459 |
https://leetcode.com/problems/consecutive-numbers-sum/discuss/1136982/Logical-Sliding-window-approach-or-Python-3-or-Linear | class Solution:
def consecutiveNumbersSum(self, n: int) -> int:
start = 1
end = 1
curr = 0
res = 0
while end <= n:
curr += end
while curr >= n:
if curr == n:
res += 1
curr -= start
... | consecutive-numbers-sum | Logical Sliding window approach | Python 3 | Linear | abhyasa | 2 | 461 | consecutive numbers sum | 829 | 0.415 | Hard | 13,460 |
https://leetcode.com/problems/consecutive-numbers-sum/discuss/2353539/Python-Quick-maths-(slightly-different-from-other-solns) | class Solution:
def consecutiveNumbersSum(self, n: int) -> int:
count = 0
k = floor(math.sqrt(2*n))
for i in range(1,k+1):
if (2*n)%i==0 and (2*n/i+i)%2!=0:
count +=1
return count | consecutive-numbers-sum | [Python] Quick maths (slightly different from other solns) | In_Ctrl | 1 | 99 | consecutive numbers sum | 829 | 0.415 | Hard | 13,461 |
https://leetcode.com/problems/consecutive-numbers-sum/discuss/2835487/Math-solution | class Solution:
def consecutiveNumbersSum(self, n: int) -> int:
i = 2
count = 1
while True:
start = n // i - ((i - 1) // 2)
if start < 1: break
if (start* 2 + i - 1) * i // 2 == n:
count += 1
i += 1
return count | consecutive-numbers-sum | Math solution | yukiyukiyeahyeah | 0 | 1 | consecutive numbers sum | 829 | 0.415 | Hard | 13,462 |
https://leetcode.com/problems/consecutive-numbers-sum/discuss/1924171/Python-3Math-Method | class Solution:
def consecutiveNumbersSum(self, n: int) -> int:
m = 1
count = 0
while m*(m-1)/2 < n:
a = float(n / m - (m - 1) / 2)
m += 1
if a.is_integer():
count += 1
return count | consecutive-numbers-sum | [Python 3]Math Method | kkimm | 0 | 127 | consecutive numbers sum | 829 | 0.415 | Hard | 13,463 |
https://leetcode.com/problems/consecutive-numbers-sum/discuss/1505191/Python3-enumeration | class Solution:
def consecutiveNumbersSum(self, n: int) -> int:
ans = 0
for x in range(1, int(sqrt(2*n))+1):
if (n - x*(x+1)//2) % x == 0: ans += 1
return ans | consecutive-numbers-sum | [Python3] enumeration | ye15 | 0 | 189 | consecutive numbers sum | 829 | 0.415 | Hard | 13,464 |
https://leetcode.com/problems/positions-of-large-groups/discuss/1831860/Python-simple-and-elegant-multiple-solutions-%22Streak%22 | class Solution(object):
def largeGroupPositions(self, s):
s += " "
streak, char, out = 0, s[0], []
for i,c in enumerate(s):
if c != char:
if streak >= 3:
out.append([i-streak, i-1])
streak, cha... | positions-of-large-groups | Python - simple and elegant - multiple solutions - "Streak" | domthedeveloper | 1 | 83 | positions of large groups | 830 | 0.518 | Easy | 13,465 |
https://leetcode.com/problems/positions-of-large-groups/discuss/1831860/Python-simple-and-elegant-multiple-solutions-%22Streak%22 | class Solution(object):
def largeGroupPositions(self, s):
s += " "
start, char, out = 0, s[0], []
for i,c in enumerate(s):
if c != char:
if i-start >= 3:
out.append([start, i-1])
start, char = ... | positions-of-large-groups | Python - simple and elegant - multiple solutions - "Streak" | domthedeveloper | 1 | 83 | positions of large groups | 830 | 0.518 | Easy | 13,466 |
https://leetcode.com/problems/positions-of-large-groups/discuss/1831860/Python-simple-and-elegant-multiple-solutions-%22Streak%22 | class Solution(object):
def largeGroupPositions(self, s):
streak, out = 0, []
for i in range(len(s)):
streak += 1
if i == len(s)-1 or s[i] != s[i+1]:
if streak >= 3:
out.append([i-streak+1, i])
... | positions-of-large-groups | Python - simple and elegant - multiple solutions - "Streak" | domthedeveloper | 1 | 83 | positions of large groups | 830 | 0.518 | Easy | 13,467 |
https://leetcode.com/problems/positions-of-large-groups/discuss/1831860/Python-simple-and-elegant-multiple-solutions-%22Streak%22 | class Solution(object):
def largeGroupPositions(self, s):
start, out = 0, []
for i in range(len(s)):
if i == len(s)-1 or s[i] != s[i+1]:
if i-start+1 >= 3:
out.append([start, i])
start = i+1
re... | positions-of-large-groups | Python - simple and elegant - multiple solutions - "Streak" | domthedeveloper | 1 | 83 | positions of large groups | 830 | 0.518 | Easy | 13,468 |
https://leetcode.com/problems/positions-of-large-groups/discuss/1620405/Python-3-faster-than-99 | class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
start = 0
cur = s[0]
res = []
for i, c in enumerate(s[1:] + ' ', start=1):
if c != cur:
if i - start >= 3:
res.append([start, i-1])
start = i
... | positions-of-large-groups | Python 3 faster than 99% | dereky4 | 1 | 146 | positions of large groups | 830 | 0.518 | Easy | 13,469 |
https://leetcode.com/problems/positions-of-large-groups/discuss/1353412/python-3-solution-easy-to-understand | class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
lst=[]
n=len(s)
if s=="":
return []
i=0
while(i<n):
start=i
end=i
for j in range(i+1,n):
... | positions-of-large-groups | python 3 solution easy to understand | minato_namikaze | 1 | 71 | positions of large groups | 830 | 0.518 | Easy | 13,470 |
https://leetcode.com/problems/positions-of-large-groups/discuss/1016683/Easy-and-Clear-Solution-Python-3 | class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
i,j,n=0,1,len(s)
tab,aux=[],[]
while j<n:
if s[i]==s[j]:
aux,j=[i,j],j+1
elif aux:
if aux[1]-aux[0]>=2:
tab.append(aux)
i,j,au... | positions-of-large-groups | Easy & Clear Solution Python 3 | moazmar | 1 | 190 | positions of large groups | 830 | 0.518 | Easy | 13,471 |
https://leetcode.com/problems/positions-of-large-groups/discuss/453124/Beats-97-in-run-time-and-100-in-memory. | class Solution:
def largeGroupPositions(self, S: str) -> List[List[int]]:
""" """
out = []
i =0
while i < (len(S) -1):#iteration for non repeating elements
j = i
while j < (len(S) -1) and S[j] == S[j+1]: #iteration for repeating elements
j += 1... | positions-of-large-groups | Beats 97% in run time and 100% in memory. | sudhirkumarshahu80 | 1 | 203 | positions of large groups | 830 | 0.518 | Easy | 13,472 |
https://leetcode.com/problems/positions-of-large-groups/discuss/2690091/Python3-Readable-and-easy-Solution | class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
# make two pointers to start and end
start = 0
result = []
# the end pointer will be the index
for idx, char in enumerate(s):
# check whether it is different to previous char
... | positions-of-large-groups | [Python3] - Readable and easy Solution | Lucew | 0 | 9 | positions of large groups | 830 | 0.518 | Easy | 13,473 |
https://leetcode.com/problems/positions-of-large-groups/discuss/2547912/Two-pointer-approach | class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
l, r = 0, 1
res = []
while r < len(s):
if s[l] == s[r]:
r += 1
else:
if r - l >= 3:
res.append([l, r - 1])
l =... | positions-of-large-groups | Two pointer approach | ankurbhambri | 0 | 18 | positions of large groups | 830 | 0.518 | Easy | 13,474 |
https://leetcode.com/problems/positions-of-large-groups/discuss/2421730/Python-Two-Pointer-Solution | class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
start = 0
res = []
for end in range(len(s)):
if end == len(s) -1 and s[start] == s[end] and end - start + 1>=3: ## For test cases like "aaa" or "a"
res.append([start, end])
##Reg... | positions-of-large-groups | Python Two Pointer Solution | theReal007 | 0 | 25 | positions of large groups | 830 | 0.518 | Easy | 13,475 |
https://leetcode.com/problems/positions-of-large-groups/discuss/2380674/Python3-Easy | class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
i=0
c=1
prev=""
l=len(s)
ans=[]
while i<l:
if s[i]==prev:
c+=1
if (i==l-1) & (c>=3):
ans.append([i+1-c,i])
... | positions-of-large-groups | [Python3] Easy | sunakshi132 | 0 | 37 | positions of large groups | 830 | 0.518 | Easy | 13,476 |
https://leetcode.com/problems/positions-of-large-groups/discuss/1988451/Python-Easy-Solution-or-Faster-87-submits | class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
tmp = ''
index = 0
res = []
for i in range(len(s)) :
if s[i] != tmp :
if i - index >= 3 :
res.append([index, i-1])
tmp = s[i]
... | positions-of-large-groups | [ Python ] Easy Solution | Faster 87% submits | crazypuppy | 0 | 71 | positions of large groups | 830 | 0.518 | Easy | 13,477 |
https://leetcode.com/problems/positions-of-large-groups/discuss/1979271/Python-easy-to-read-and-understand | class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
i, j = 0, 1
n = len(s)
res = []
while i < n and j < n:
if s[i] == s[j]:
j += 1
else:
if j-i >= 3:
res.append([i, j-1])
... | positions-of-large-groups | Python easy to read and understand | sanial2001 | 0 | 47 | positions of large groups | 830 | 0.518 | Easy | 13,478 |
https://leetcode.com/problems/positions-of-large-groups/discuss/1851820/PYTHON-SIMPLE-ONE-pointer-solution-step-by-step-(36ms) | class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
#Sentinel at the end
s = s + '!';
#Then find the length
LENGTH = len ( s );
#Make a list for the solution
soln = [ ];
#Initialize the previous as the ... | positions-of-large-groups | PYTHON SIMPLE ONE pointer solution step-by-step (36ms) | greg_savage | 0 | 65 | positions of large groups | 830 | 0.518 | Easy | 13,479 |
https://leetcode.com/problems/positions-of-large-groups/discuss/1337702/Python3-dollarolution | class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
x = s[0]
a, y, v = 0, 1, []
for i in range(1,len(s)):
if s[i] == x:
y += 1
if y > 2:
b = i
else:
if y > 2:
... | positions-of-large-groups | Python3 $olution | AakRay | 0 | 75 | positions of large groups | 830 | 0.518 | Easy | 13,480 |
https://leetcode.com/problems/positions-of-large-groups/discuss/1255602/Simple-and-Easy-or-or-Python-oror-830.-Positions-of-Large-Groups | class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
res=[]
count=1
for i in range(1,len(s)):
if s[i]==s[i-1]:
count+=1
else:
if count >= 3:
res.append([i-count,i-1])
count=1
... | positions-of-large-groups | Simple and Easy | | Python || 830. Positions of Large Groups | jaipoo | 0 | 83 | positions of large groups | 830 | 0.518 | Easy | 13,481 |
https://leetcode.com/problems/positions-of-large-groups/discuss/1219155/Python3-simple-solution-using-list-beats-90-users | class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
res = []
x = 0
for i in range(len(s)):
if i == len(s) - 1 or s[i] != s[i+1]:
if i-x+1 >= 3:
res.append([x, i])
x = i+1
return res | positions-of-large-groups | Python3 simple solution using list beats 90% users | EklavyaJoshi | 0 | 47 | positions of large groups | 830 | 0.518 | Easy | 13,482 |
https://leetcode.com/problems/positions-of-large-groups/discuss/458518/Python3-super-simple-solution-using-a-for()-loop | class Solution:
def largeGroupPositions(self, S: str) -> List[List[int]]:
stack,res = [],[]
for i in range(len(S)):
if not stack or stack[-1][1] == S[i]:
stack.append((i, S[i]))
if i != len(S) - 1: continue
if len(stack) >= 3: res.append([stack[0][0],stack[-1][0]])
stack = [(i, S[i])]
return res | positions-of-large-groups | Python3 super simple solution using a for() loop | jb07 | 0 | 55 | positions of large groups | 830 | 0.518 | Easy | 13,483 |
https://leetcode.com/problems/masking-personal-information/discuss/1868652/3-Lines-Python-Solution-oror-98-Faster-oror-Memory-less-than-87 | class Solution:
def maskPII(self, s: str) -> str:
if '@' in s: return f'{s[0].lower()}*****{s[s.index("@")-1].lower()+"".join([x.lower() for x in s[s.index("@"):]])}'
s=''.join([x for x in s if x not in '()- +'])
return ('' if len(s)<=10 else '+'+'*'*(len(s)-10)+'-')+f'***-***-{s[-4:]}' | masking-personal-information | 3-Lines Python Solution || 98% Faster || Memory less than 87% | Taha-C | 1 | 94 | masking personal information | 831 | 0.47 | Medium | 13,484 |
https://leetcode.com/problems/masking-personal-information/discuss/1868652/3-Lines-Python-Solution-oror-98-Faster-oror-Memory-less-than-87 | class Solution:
def maskPII(self, s: str) -> str:
if '@' in s:
user,domain=s.split('@')
return f'{user[0].lower()}{"*"*5}{user[-1].lower()}@{domain.lower()}'
s=''.join([x for x in s if x.isdigit()]) ; n=0
return f'+{"*"*(n-10)}-***-***-{s[-4:]}' if n>10 else f'***-**... | masking-personal-information | 3-Lines Python Solution || 98% Faster || Memory less than 87% | Taha-C | 1 | 94 | masking personal information | 831 | 0.47 | Medium | 13,485 |
https://leetcode.com/problems/masking-personal-information/discuss/2762680/Python3-oror-Split-and-Filter-oror-Easy | class Solution:
def maskPII(self, s: str) -> str:
if '@' in s:
s = s.lower()
name, rest = s.split('@')
name = name[0] + '*****' + name[-1]
return name + '@' + rest
else:
num = ''.join([n for n in s if n in '1234567890'])
if len(... | masking-personal-information | Python3 || Split & Filter || Easy | joshua_mur | 0 | 17 | masking personal information | 831 | 0.47 | Medium | 13,486 |
https://leetcode.com/problems/masking-personal-information/discuss/1426661/Python-3-or-f-string-or-Explanation-(This-should-be-an-EASY-question) | class Solution:
def maskPII(self, s: str) -> str:
if '@' in s:
user, domain = s.split('@')
return f'{user[0].lower()}{"*"*5}{user[-1].lower()}@{domain.lower()}'
else:
s = ''.join([c for c in s if c.isdigit()])
n = len(s)
return f'+{"*"*... | masking-personal-information | Python 3 | f-string | Explanation (This should be an EASY question) | idontknoooo | 0 | 79 | masking personal information | 831 | 0.47 | Medium | 13,487 |
https://leetcode.com/problems/masking-personal-information/discuss/937240/Python3-straightforward-soln | class Solution:
def maskPII(self, S: str) -> str:
if "@" in S: # email address
name, domain = S.lower().split("@")
return f"{name[0]}*****{name[-1]}@{domain}"
else: # phone number
d = "".join(c for c in S if c.isdigit())
ans = f"***-***-{d[-4:]}"
... | masking-personal-information | [Python3] straightforward soln | ye15 | 0 | 69 | masking personal information | 831 | 0.47 | Medium | 13,488 |
https://leetcode.com/problems/flipping-an-image/discuss/1363051/PYTHON-VERY-VERY-EASY-SOLN.-3-solutions-explained-O(n).-With-or-without-inbuilt-functions. | class Solution:
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
"""
Simple & striaghtforward without using inbuilt functions.
In actual the run time is very less as we are iterating only n/2 time
for each image list.
Time complexity : O(n * ... | flipping-an-image | [PYTHON] VERY VERY EASY SOLN. 3 solutions explained O(n). With or without inbuilt functions. | er1shivam | 10 | 663 | flipping an image | 832 | 0.805 | Easy | 13,489 |
https://leetcode.com/problems/flipping-an-image/discuss/1780606/Python3-Solution-or-Easy-to-understand | class Solution:
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
for i in range(len(image)):
image[i] = image[i][::-1]
for j in range(len(image[i])):
if image[i][j] == 0:
image[i][j] = 1
else:
... | flipping-an-image | Python3 Solution | Easy to understand | Coding_Tan3 | 7 | 313 | flipping an image | 832 | 0.805 | Easy | 13,490 |
https://leetcode.com/problems/flipping-an-image/discuss/1225285/32ms-Python-(with-comments) | class Solution(object):
def flipAndInvertImage(self, image):
"""
:type image: List[List[int]]
:rtype: List[List[int]]
"""
#create a variable to store the result
result = []
#create a variable for storing the number of elements in each sublist as we need it later, saving s... | flipping-an-image | 32ms, Python (with comments) | Akshar-code | 3 | 290 | flipping an image | 832 | 0.805 | Easy | 13,491 |
https://leetcode.com/problems/flipping-an-image/discuss/1287794/Python3-96-time-one-liner-with-list-comprehension-explained | class Solution:
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
return [[0 if n else 1 for n in i] for i in [item[::-1] for item in image]] | flipping-an-image | Python3, 96% time, one liner, with list comprehension, explained | albezx0 | 2 | 123 | flipping an image | 832 | 0.805 | Easy | 13,492 |
https://leetcode.com/problems/flipping-an-image/discuss/2558075/EASY-PYTHON3-SOLUTION | class Solution:
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
result = []
for i in range(len(image)):
for j in range(len(image[i])):
if image[i][j] == 1:
image[i][j] = 0
else:image[i][j] = 1
result.append(... | flipping-an-image | ✅✔🔥 EASY PYTHON3 SOLUTION 🔥✅✔ | rajukommula | 1 | 102 | flipping an image | 832 | 0.805 | Easy | 13,493 |
https://leetcode.com/problems/flipping-an-image/discuss/2410950/Simple-python-code-with-explanation | class Solution:
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
#iterate over the list of list--> image
for i in range(len(image)):
#reverse every list in image list using reverse keyword
image[i].reverse()... | flipping-an-image | Simple python code with explanation | thomanani | 1 | 32 | flipping an image | 832 | 0.805 | Easy | 13,494 |
https://leetcode.com/problems/flipping-an-image/discuss/2205721/Python3-O(rc)-oror-O(1)-Runtime%3A-54ms-89.52-Memory%3A-13.8mb-64.60 | class Solution:
# O(r,c) || O(1)
# Runtime: 54ms 89.52% Memory: 13.8mb 64.60%
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
def reverse(image):
for row in range(len(image)):
image[row] = image[row][::-1]
... | flipping-an-image | Python3 O(r,c) || O(1) # Runtime: 54ms 89.52% Memory: 13.8mb 64.60% | arshergon | 1 | 47 | flipping an image | 832 | 0.805 | Easy | 13,495 |
https://leetcode.com/problems/flipping-an-image/discuss/2009557/Python-3-Solution-Two-Pointers-fast | class Solution:
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
# Flipping Horizontally
for k in range(len(image)):
i, j = 0, len(image[k]) - 1
while i < j:
image[k][i], image[k][j] = image[k][j], image[k][i]
i += 1
... | flipping-an-image | Python 3 Solution, Two Pointers, fast | AprDev2011 | 1 | 58 | flipping an image | 832 | 0.805 | Easy | 13,496 |
https://leetcode.com/problems/flipping-an-image/discuss/1950486/Easy-Beginner-Solution-With-Slicing | class Solution:
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
final = []
cols = len(image[0])
for i in range(len(image)):
c = image[i][::-1]
for j in range(len(image)):
if c[j] == 1:
c[j] = 0
... | flipping-an-image | Easy Beginner Solution With Slicing | itsmeparag14 | 1 | 23 | flipping an image | 832 | 0.805 | Easy | 13,497 |
https://leetcode.com/problems/flipping-an-image/discuss/1386440/Python-One-Liner-Fast | class Solution:
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
return [[1-val for val in row[::-1]] for row in image] | flipping-an-image | Python One Liner Fast | peatear-anthony | 1 | 75 | flipping an image | 832 | 0.805 | Easy | 13,498 |
https://leetcode.com/problems/flipping-an-image/discuss/1154296/Python-Easy-To-Understand-Solution | class Solution:
def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]:
for i in range(0, len(image)):
image[i].reverse()
for j in range(0, len(image[0])):
if image[i][j] == 0:
image[i][j]... | flipping-an-image | Python Easy To Understand Solution | saurabhkhurpe | 1 | 152 | flipping an image | 832 | 0.805 | Easy | 13,499 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.