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/number-of-students-unable-to-eat-lunch/discuss/1751095/Easy-Python-solution
class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: for i in sandwiches: if i in students: sandwiches = sandwiches[1:] students.remove(i) else: break return len(students)
number-of-students-unable-to-eat-lunch
Easy Python solution
MengyingLin
0
86
number of students unable to eat lunch
1,700
0.679
Easy
24,600
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/discuss/1545572/Python3-solution
class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: cnt = Counter(students) st = deque(sandwiches) # stack while st and cnt[st[0]] > 0: cnt[st[0]] -= 1 st.popleft() return len(st)
number-of-students-unable-to-eat-lunch
Python3 solution
dalechoi
0
66
number of students unable to eat lunch
1,700
0.679
Easy
24,601
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/discuss/1485656/Counter-for-students-97-speed
class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: cnt = Counter(students) while sandwiches and sandwiches[0] in cnt: i = students.index(sandwiches[0]) students = students[i + 1:] + students[:i] cnt[sandwiches[0]] -= 1 if cnt[sandwiches[0]] == 0: cnt.pop(sandwiches[0]) sandwiches.pop(0) return len(students)
number-of-students-unable-to-eat-lunch
Counter for students, 97% speed
EvgenySH
0
136
number of students unable to eat lunch
1,700
0.679
Easy
24,602
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/discuss/1332624/Python3-or-easy-understanding
class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: while sandwiches[0] in students: if students[0] == sandwiches[0]: sandwiches.pop(0) else: students.append(students[0]) students.pop(0) if len(sandwiches) == 0: break return len(students)
number-of-students-unable-to-eat-lunch
Python3 | easy-understanding
Wyhever
0
65
number of students unable to eat lunch
1,700
0.679
Easy
24,603
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/discuss/1304432/Python-28-ms
class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: student_counters = {1: sum(students)} student_counters[0] = len(students) - student_counters[1] students_queue = collections.deque(students) for sandwich in sandwiches: if student_counters[sandwich] == 0: break while students_queue[0] != sandwich: students_queue.append(students_queue.popleft()) student_counters[sandwich] -= 1 students_queue.popleft() return len(students_queue)
number-of-students-unable-to-eat-lunch
Python, 28 ms
MihailP
0
107
number of students unable to eat lunch
1,700
0.679
Easy
24,604
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/discuss/1169571/Python-approach-using-stack-operations
class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: rot_cnt = len(sandwiches) total_length = rot_cnt eat = 0 while(rot_cnt): if students[0] == sandwiches[0]: eat += 1 students.pop(0) sandwiches.pop(0) rot_cnt = len(students) else: temp = students[0] students.pop(0) students.append(temp) rot_cnt -= 1 return total_length - eat
number-of-students-unable-to-eat-lunch
Python- approach using stack operations
ShivamBhirud
0
112
number of students unable to eat lunch
1,700
0.679
Easy
24,605
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/discuss/1092434/Python3-O(n)-faster-than-93
class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: count = Counter(students) for i, s in enumerate(sandwiches): if not count[s]: break count[s]-=1 else: i = len(sandwiches) return len(sandwiches)-i
number-of-students-unable-to-eat-lunch
Python3 O(n) faster than 93%
amol1729
0
61
number of students unable to eat lunch
1,700
0.679
Easy
24,606
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/discuss/1040726/Python3-easy-solution
class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: x = 0 a = len(students) while len(students) > 0: if students[0] == sandwiches[0]: students.pop(0) sandwiches.pop(0) x += 1 elif sandwiches[0] in students: students.append(students.pop(0)) else: break return a - x
number-of-students-unable-to-eat-lunch
Python3 easy solution
EklavyaJoshi
0
65
number of students unable to eat lunch
1,700
0.679
Easy
24,607
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/discuss/1003643/Python3-matches-students-with-sandwiches
class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: ss = sum(students) for i, x in enumerate(sandwiches): if (x and not ss) or (not x and ss == len(sandwiches) - i): return len(sandwiches)-i ss -= x return 0
number-of-students-unable-to-eat-lunch
[Python3] matches students with sandwiches
ye15
0
74
number of students unable to eat lunch
1,700
0.679
Easy
24,608
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/discuss/990290/Runtime-100-faster-and-Memory-100-less
class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: students_counter = collections.Counter(students) for sandwich in sandwiches: if students_counter[sandwich]>0: students_counter[sandwich]-=1 else: break return students_counter[0]+students_counter[1]
number-of-students-unable-to-eat-lunch
Runtime 100 % faster and Memory 100% less
WiseLin
0
102
number of students unable to eat lunch
1,700
0.679
Easy
24,609
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/discuss/987544/python-3-easy-to-understand-solution
class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: while students and self.helper(students,sandwiches): first = students.pop(0) if first == sandwiches[0]: sandwiches.pop(0) else: students.append(first) return len(students) def helper(self,students, sandwiches):#The while loop should stop if there are no students or if all the students want #the same shape and the shape the want is different that the first sandwich in the stack summ = sum(students) if summ == 0:#It means that all students are 0 if sandwiches[0] != 0: return False else: True if summ == len(students):#It means that all students are 1 if sandwiches[0] != 1: return False else: True return True
number-of-students-unable-to-eat-lunch
python 3 easy to understand solution
GiorgosMarga
0
45
number of students unable to eat lunch
1,700
0.679
Easy
24,610
https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/discuss/987496/Python-no-dictionary.-Time%3A-O(N)-Space%3A-O(1)-or-100-100
class Solution: def countStudents(self, students: List[int], sandwiches: List[int]) -> int: counts = [len(students) - sum(students), sum(students)] for s in sandwiches: if not counts[s]: return sum(counts) counts[s] -= 1 return 0
number-of-students-unable-to-eat-lunch
Python, no dictionary. Time: O(N), Space: O(1) | 100% / 100%
blue_sky5
-1
59
number of students unable to eat lunch
1,700
0.679
Easy
24,611
https://leetcode.com/problems/average-waiting-time/discuss/1236349/Python3-Simple-And-Fast-Solution
class Solution: def averageWaitingTime(self, customers: List[List[int]]) -> float: arr = [] time = 0 for i , j in customers: if(i > time): time = i + j else: time += j arr.append(time - i) return sum(arr) / len(arr)
average-waiting-time
[Python3] Simple And Fast Solution
VoidCupboard
6
194
average waiting time
1,701
0.624
Medium
24,612
https://leetcode.com/problems/average-waiting-time/discuss/2542272/Python3-or-Self-explanatory-1-pass-solution
class Solution: def averageWaitingTime(self, customers: List[List[int]]) -> float: waits_total = 0 end_prev_order = customers[0][0] for arrival, time in customers: overhead = end_prev_order - arrival if end_prev_order > arrival else 0 waits_total += time + overhead end_prev_order = max(end_prev_order, arrival) + time return waits_total / len(customers)
average-waiting-time
Python3 | Self-explanatory 1-pass solution
Ploypaphat
0
13
average waiting time
1,701
0.624
Medium
24,613
https://leetcode.com/problems/average-waiting-time/discuss/2524399/Python-or-Easy-to-solve
class Solution: def averageWaitingTime(self, customers: List[List[int]]) -> float: ans = 0 c = -1 for i in range(0,len(customers)): if c>customers[i][0]: c = c+customers[i][1] ans+=(c)-customers[i][0] else: ans+=customers[i][1] c = customers[i][1]+customers[i][0] return ans/len(customers)
average-waiting-time
Python | Easy to solve
Brillianttyagi
0
16
average waiting time
1,701
0.624
Medium
24,614
https://leetcode.com/problems/average-waiting-time/discuss/2402264/Simple-Python-Solution-or-Beats-74-in-TC-and-88-in-SC
class Solution: def averageWaitingTime(self, customers: List[List[int]]) -> float: s=0 count=0 flag=1 for start, time in customers: if flag: count+=start flag=0 if count>=start: count+=time s+=(count-start) else: s+=(time) count=start+time print(count, s) return s/len(customers)
average-waiting-time
Simple Python Solution | Beats 74% in TC & 88% in SC
Siddharth_singh
0
7
average waiting time
1,701
0.624
Medium
24,615
https://leetcode.com/problems/average-waiting-time/discuss/2192265/Simplest-Intuition
class Solution: def averageWaitingTime(self, customers: List[List[int]]) -> float: totalTime = 0 current = 0 for arr, time in customers: current = max(current, arr) + time totalTime += current - arr return totalTime/len(customers)
average-waiting-time
Simplest Intuition
Vaibhav7860
0
11
average waiting time
1,701
0.624
Medium
24,616
https://leetcode.com/problems/average-waiting-time/discuss/1777411/Python-3-very-easy-O(n)-O(1)
class Solution: def averageWaitingTime(self, customers: List[List[int]]) -> float: totalWait = curTime = 0 for arrival, time in customers: curTime = max(curTime, arrival) + time totalWait += curTime - arrival return totalWait / len(customers)
average-waiting-time
Python 3, very easy, O(n) / O(1)
dereky4
0
49
average waiting time
1,701
0.624
Medium
24,617
https://leetcode.com/problems/average-waiting-time/discuss/1402183/Python3-simple-solution
class Solution: def averageWaitingTime(self, customers: List[List[int]]) -> float: time = 0 n = len(customers) free = 0 for i in range(n): a,b = customers[i] if free <= a: free += a - free + b time += b else: time += free-a + b free += b return time/n
average-waiting-time
Python3 simple solution
EklavyaJoshi
0
52
average waiting time
1,701
0.624
Medium
24,618
https://leetcode.com/problems/average-waiting-time/discuss/1003331/Python3-sweep
class Solution: def averageWaitingTime(self, customers: List[List[int]]) -> float: ans = t = 0 for arrvl, tt in customers: t = max(t, arrvl) + tt ans += t - arrvl return ans/len(customers)
average-waiting-time
[Python3] sweep
ye15
0
37
average waiting time
1,701
0.624
Medium
24,619
https://leetcode.com/problems/average-waiting-time/discuss/988689/PYTHON-oror-EASY-oror-O(N)-TIME-oror-O(1)-SPACE
class Solution(object): def averageWaitingTime(self, customers): current_time = 0 wait_time = 0 for arrival_time, duration in customers: current_time = max(current_time, arrival_time) + duration wait_time += (current_time - arrival_time) return wait_time / len(customers)
average-waiting-time
PYTHON || EASY || O(N) TIME || O(1) SPACE
akashgkrishnan
0
43
average waiting time
1,701
0.624
Medium
24,620
https://leetcode.com/problems/average-waiting-time/discuss/987569/Python-Solution
class Solution: def averageWaitingTime(self, customers: List[List[int]]) -> float: next_wt = customers[0][0]+customers[0][1] time = [] time.append(next_wt-customers[0][0]) for i in range(1,len(customers)): arr = customers[i][0] dep = customers[i][1] if arr < next_wt: wt = (next_wt-arr)+(arr+dep) else: wt = arr+dep time.append(wt - arr) next_wt = wt #print(time) return sum(time)/len(time)
average-waiting-time
Python Solution
SaSha59
0
26
average waiting time
1,701
0.624
Medium
24,621
https://leetcode.com/problems/maximum-binary-string-after-change/discuss/1382851/python-3-oror-clean-oror-easy-approach
lass Solution: def maximumBinaryString(self, s: str) -> str: #count of 0 c=0 #final ans string will contain only one zero.therefore shift the first 0 to c places.Initialize ans string with all 1s lst=["1"]*len(s) for i in range (0,len(s)): if s[i]=="0": c+=1 for i in range (0,len(s)): #finding the ist 0 if s[i]=="0": lst[i+c-1]="0" return "".join(lst) return s
maximum-binary-string-after-change
python 3 || clean || easy approach
minato_namikaze
4
175
maximum binary string after change
1,702
0.462
Medium
24,622
https://leetcode.com/problems/maximum-binary-string-after-change/discuss/1492879/Python-3-or-Greedy-or-Explanation
class Solution: def maximumBinaryString(self, binary: str) -> str: zero = binary.count('0') # count number of '0' zero_idx = binary.index('0') if zero > 0 else 0 # find the index of fist '0' if exists one = len(binary) - zero_idx - zero # count number of '1' (not including leading '1's) return f"{binary[:zero_idx]}{'1'*(zero-1)}{'0'*min(zero, 1)}{'1'*one}"
maximum-binary-string-after-change
Python 3 | Greedy | Explanation
idontknoooo
2
178
maximum binary string after change
1,702
0.462
Medium
24,623
https://leetcode.com/problems/maximum-binary-string-after-change/discuss/1173116/Easy-Python-2-lines-Beats-100
class Solution: ''' Runtime: 44 ms, faster than 100.00% of Python3 online submissions for Maximum Binary String After Change. Memory Usage: 15.6 MB, less than 59.78% of Python3 online submissions for Maximum Binary String After Change. ''' ''' 1. Variable Description -> first_zero => first occurence index of 0, num_zeros => Total number of zeros in the input binary string. 2. If there are no zeros, directly return the binary string (Already in the desired form). 3. Put all the ones(1) till first 0 occurence as it is and covert remaining num_zeros - 1 zeros to 1. 3. Put the last remaining 0 now as we can't do anything about it. 4. Now put the remaining 1s => (total length of binary string) - (count of zeros) - (count of ones at the beginning) ''' def maximumBinaryString(self, binary: str) -> str: first_zero, num_zeros = binary.find('0'), binary.count('0') return ('1' * ( first_zero + num_zeros - 1 )) + '0' + ('1' * (len(binary) - num_zeros - first_zero)) if zeros else binary
maximum-binary-string-after-change
Easy Python - 2 lines - Beats 100%
shubhsaxena
1
122
maximum binary string after change
1,702
0.462
Medium
24,624
https://leetcode.com/problems/maximum-binary-string-after-change/discuss/2604358/Maximum-binary-string-oror-Python-oror-O(n)-Time-Complexity-oror-Solution-with-explanation-and-comments
class Solution: def maximumBinaryString(self, binary: str) -> str: # Find the first zero first_zero = binary.find('0') # If there are no zeroes then no optimization is needed if(first_zero == -1): return binary # Counting number of zeros count_zeroes = binary.count('0', first_zero) return '1' * (first_zero) + '1' * (count_zeroes-1) + '0' + '1' * (len(binary) - first_zero - count_zeroes)
maximum-binary-string-after-change
Maximum binary string || Python || O(n) Time Complexity || Solution with explanation and comments
vanshika_2507
0
10
maximum binary string after change
1,702
0.462
Medium
24,625
https://leetcode.com/problems/maximum-binary-string-after-change/discuss/987983/Python3-One-liner
class Solution: def maximumBinaryString(self, binary: str) -> str: return '1'*binary.find('0')+reduce(lambda a,b:a+b if b=='1' else a[:-1]+'10', sorted(binary[max(binary.find('0'),0):]))
maximum-binary-string-after-change
[Python3] One-liner
vilchinsky
0
73
maximum binary string after change
1,702
0.462
Medium
24,626
https://leetcode.com/problems/maximum-binary-string-after-change/discuss/987414/Python3-Detailed-explanation
class Solution: def maximumBinaryString(self, binary: str) -> str: leading_one, count_zero, count_one = 0, 0, 0 for x in binary: if x == '1': count_one+=1 if not count_zero: leading_one+=1 else: count_zero+=1 if count_zero > 1: return '1'*(leading_one+count_zero-1)+'0'+'1'*(count_one-leading_one) else: return binary
maximum-binary-string-after-change
[Python3] Detailed explanation
vilchinsky
0
68
maximum binary string after change
1,702
0.462
Medium
24,627
https://leetcode.com/problems/maximum-binary-string-after-change/discuss/1003614/Python3-string-processing
class Solution: def maximumBinaryString(self, binary: str) -> str: n = len(binary) binary = binary.lstrip("1") ones = n - len(binary) # count of leading "1"s zeros = binary.count("0") if zeros <= 1: return ones*"1" + binary # nothing to do return (ones + zeros - 1)*"1" + "0" + (len(binary) - zeros)*"1"
maximum-binary-string-after-change
[Python3] string processing
ye15
-1
111
maximum binary string after change
1,702
0.462
Medium
24,628
https://leetcode.com/problems/maximum-binary-string-after-change/discuss/1003614/Python3-string-processing
class Solution: def maximumBinaryString(self, binary: str) -> str: if binary.count("0") <= 1: return binary ones = binary.count("1", binary.index("0")) return (len(binary)-ones-1)*"1" + "0" + ones*"1"
maximum-binary-string-after-change
[Python3] string processing
ye15
-1
111
maximum binary string after change
1,702
0.462
Medium
24,629
https://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones/discuss/1002574/Python3-1-pass-O(N)
class Solution: def minMoves(self, nums: List[int], k: int) -> int: ii = val = 0 ans = inf loc = [] # location of 1s for i, x in enumerate(nums): if x: loc.append(i) m = (ii + len(loc) - 1)//2 # median val += loc[-1] - loc[m] - (len(loc)-ii)//2 # adding right if len(loc) - ii > k: m = (ii + len(loc))//2 # updated median val -= loc[m] - loc[ii] - (len(loc)-ii)//2 # removing left ii += 1 if len(loc)-ii == k: ans = min(ans, val) # len(ones) - ii effective length return ans
minimum-adjacent-swaps-for-k-consecutive-ones
[Python3] 1-pass O(N)
ye15
2
572
minimum adjacent swaps for k consecutive ones
1,703
0.423
Hard
24,630
https://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones/discuss/1002574/Python3-1-pass-O(N)
class Solution: def minMoves(self, nums: List[int], k: int) -> int: loc = [i for i, x in enumerate(nums) if x] prefix = [0] for x in loc: prefix.append(prefix[-1] + x) ans = inf for i in range(len(loc)-k+1): ans = min(ans, (prefix[i+k] - prefix[i+(k+1)//2]) - (prefix[i+k//2] - prefix[i])) return ans - (k//2)*((k+1)//2)
minimum-adjacent-swaps-for-k-consecutive-ones
[Python3] 1-pass O(N)
ye15
2
572
minimum adjacent swaps for k consecutive ones
1,703
0.423
Hard
24,631
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/991430/Runtime-is-faster-than-98-and-the-memory-usage-is-less-than-90-Python-3-Accepted
class Solution: def halvesAreAlike(self, s: str) -> bool: vowels = set('aeiouAEIOU') count = 0 for i in range(len(s)//2): if s[i] in vowels: count+=1 if s[-i-1] in vowels: count-=1 return count == 0
determine-if-string-halves-are-alike
Runtime is faster than 98% and the memory usage is less than 90% Python 3 [Accepted]
WiseLin
5
341
determine if string halves are alike
1,704
0.774
Easy
24,632
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2669019/PYTHON-3-STRING-SLICING-or-EASY-or-EXPLANATION
class Solution: def halvesAreAlike(self, s: str) -> bool: vowels = ['a', 'e', 'i', 'o', 'u'] # initializing the first half of the word and the back half variables first, firstc = [*s][:len(s)//2], 0 back, backc = [*s][len(s)//2:], 0 # [*s] creates a list # [len(s)//2] finds the middle position of the list # counts the vowels in first and back half for x in first: if x.lower() in vowels: firstc += 1 for y in back: if y.lower() in vowels: backc += 1 # returns whether the counts are equal to each other return firstc == backc
determine-if-string-halves-are-alike
[PYTHON 3] STRING SLICING | EASY | EXPLANATION
omkarxpatel
3
108
determine if string halves are alike
1,704
0.774
Easy
24,633
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1222548/python-or-98.51-or-easy
class Solution: def halvesAreAlike(self, s: str) -> bool: y=['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] a=b=0 for i in range(len(s)//2): if s[i] in y: a+=1 if s[len(s)//2 + i] in y: b+=1 return a==b
determine-if-string-halves-are-alike
python | 98.51% | easy
chikushen99
3
161
determine if string halves are alike
1,704
0.774
Easy
24,634
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1565203/simple-fast-python-solution
class Solution: def halvesAreAlike(self, s: str) -> bool: vowels = {'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'} n = len(s) m = n // 2 return sum(s[i] in vowels for i in range(m)) == sum(s[i] in vowels for i in range(m, n))
determine-if-string-halves-are-alike
simple, fast python solution
dereky4
2
63
determine if string halves are alike
1,704
0.774
Easy
24,635
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2693513/Python-oror-Simple-oror-Runtime-57-ms-Beats-62.96-Memory-13.9-MB-Beats-78.52-oror-O(N)
class Solution: def halvesAreAlike(self, s: str) -> bool: n=len(s) s=s.lower() a,b=s[:n//2],s[n//2:] vol="aeiou" c1,c2=0,0 for i in a: if i in vol: c1+=1 for i in b: if i in vol: c2+=1 return c1==c2
determine-if-string-halves-are-alike
Python || Simple || Runtime 57 ms Beats 62.96% Memory 13.9 MB Beats 78.52% || O(N)
Sneh713
1
49
determine if string halves are alike
1,704
0.774
Easy
24,636
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2429959/Python-Simple-Solution-(-Brute-Force-Approach-)
class Solution: def halvesAreAlike(self, s: str) -> bool: f1,f2 = s[:len(s)//2] , s[len(s)//2:] c1 , c2 = 0,0 v = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] for i in f1: if i in v: c1 += 1 for j in f2: if j in v: c2 += 1 return c1 == c2
determine-if-string-halves-are-alike
Python Simple Solution ( Brute Force Approach )
SouravSingh49
1
25
determine if string halves are alike
1,704
0.774
Easy
24,637
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1483661/Set-of-vowels-100-speed
class Solution: vowels = set("aeiouAEIOU") def halvesAreAlike(self, s: str) -> bool: half = len(s) // 2 return (sum(c in Solution.vowels for c in s[:half]) == sum(c in Solution.vowels for c in s[half:]))
determine-if-string-halves-are-alike
Set of vowels, 100% speed
EvgenySH
1
65
determine if string halves are alike
1,704
0.774
Easy
24,638
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1385763/Easy-to-Understand-or-Faster-than-86
class Solution: def halvesAreAlike(self, s: str) -> bool: count= 0 l = len(s) d = l//2 s = s.lower() lis = ['a', 'e', 'i', 'o', 'u'] for i in range(0,d): if s[i] in lis: count+=1 for j in range(d,l): if s[j] in lis: count-=1 if count == 0: return True else: return False
determine-if-string-halves-are-alike
Easy to Understand | Faster than 86%
kdevharsh2001
1
52
determine if string halves are alike
1,704
0.774
Easy
24,639
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1298413/Python3-weird-one-liner
class Solution: def halvesAreAlike(self, s: str) -> bool: return sum(map(s[:len(s)//2].lower().count, 'aeiou')) == sum(map(s[len(s)//2:].lower().count, 'aeiou'))
determine-if-string-halves-are-alike
[Python3] weird one-liner
denizen-ru
1
37
determine if string halves are alike
1,704
0.774
Easy
24,640
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1173214/Python-Simple-Zip-Loop
class Solution: def halvesAreAlike(self, s: str) -> bool: left = right = 0 vowels = {'a','e','i','o','u','A', 'E','I','O','U'} for (x,y) in zip(s[:len(s)//2], s[len(s)//2:]): if x in vowels: left+=1 if y in vowels: right+=1 return left == right
determine-if-string-halves-are-alike
Python Simple Zip Loop
X00X
1
73
determine if string halves are alike
1,704
0.774
Easy
24,641
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1126182/Python-Faster-than-95-of-Submissions
class Solution: def halvesAreAlike(self, s: str) -> bool: vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} count = 0 for i in range(floor(len(s)/2)): if s[i] in vowels: count -= 1 if s[-i - 1] in vowels: count += 1 return count == 0
determine-if-string-halves-are-alike
Python Faster than 95% of Submissions
APet99
1
114
determine if string halves are alike
1,704
0.774
Easy
24,642
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1120322/Runtime%3A-faster-than-95.81Memory-Usage%3A-less-than-97.18-or-Easy-Beginner
class Solution: def halvesAreAlike(self, s: str) -> bool: count =0 temp=0 arr= "AEIOUaeiou" for x in range(len(s)//2): if s[x] in arr: count+=1 for x in range(len(s)//2,len(s)): if s[x] in arr: temp+=1 if (count == temp): return True return False ```
determine-if-string-halves-are-alike
Runtime: faster than 95.81%,Memory Usage: less than 97.18% | Easy, Beginner
ak-akkinapelli
1
53
determine if string halves are alike
1,704
0.774
Easy
24,643
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/988509/Python-solution%3A-faster-than-100.00-submissions-in-runtime
class Solution: def halvesAreAlike(self, s: str) -> bool: a,b=s[:len(s)//2],s[len(s)//2:] x1,x2=0,0 for i in a: if i in ['a','e','i','o','u','A','E','I','O','U']: x1+=1 for i in b: if i in ['a','e','i','o','u','A','E','I','O','U']: x2+=1 return x1==x2
determine-if-string-halves-are-alike
Python solution: faster than 100.00% submissions in runtime
thisisakshat
1
105
determine if string halves are alike
1,704
0.774
Easy
24,644
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/988430/Python3-count-O(N)
class Solution: def halvesAreAlike(self, s: str) -> bool: cnt = 0 for i, c in enumerate(s): if c in "aeiouAEIOU": cnt += 1 if i < len(s)//2 else -1 return cnt == 0
determine-if-string-halves-are-alike
[Python3] count O(N)
ye15
1
46
determine if string halves are alike
1,704
0.774
Easy
24,645
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2847085/Python-easy-Solution
class Solution: def halvesAreAlike(self, s: str) -> bool: vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} mid = len(s) // 2 left = s[:mid] right = s[mid:] vowels_count = 0 for char in left: if char in vowels: vowels_count += 1 for char in right: if char in vowels: vowels_count -= 1 return vowels_count == 0
determine-if-string-halves-are-alike
Python easy Solution
namashin
0
1
determine if string halves are alike
1,704
0.774
Easy
24,646
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2811811/Python-Solution-oror-Simple-and-Fast-oror-Understandable
class Solution: def halvesAreAlike(self, s): cnt1 = 0 cnt2 = 0 for i in range(0, int(len(s)/2)): if s[i].lower() in 'aeiou': cnt1 += 1 for i in range(int(len(s)/2), len(s)): if s[i].lower() in 'aeiou': cnt2 += 1 return cnt1 == cnt2
determine-if-string-halves-are-alike
Python Solution || Simple & Fast || Understandable
qiy2019
0
1
determine if string halves are alike
1,704
0.774
Easy
24,647
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2786283/Python-solution-95.97-faster
class Solution: def halvesAreAlike(self, s: str) -> bool: vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} len_s = len(s) half_index = len_s // 2 a = s[:half_index] b = s[half_index:] count_v_in_a = 0 count_v_in_b = 0 for ch in a: if ch in vowels: count_v_in_a += 1 for ch in b: if ch in vowels: count_v_in_b += 1 return count_v_in_a == count_v_in_b
determine-if-string-halves-are-alike
Python solution 95.97% faster
samanehghafouri
0
5
determine if string halves are alike
1,704
0.774
Easy
24,648
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2780654/Python-3-O(n)-two-pointers-approach
class Solution: def halvesAreAlike(self, s: str) -> bool: l, r = 0, len(s) - 1 lv, rv = 0, 0 vowel = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'} while l <= r: if s[l] in vowel: lv += 1 if s[r] in vowel: rv += 1 l += 1 r -= 1 return lv == rv
determine-if-string-halves-are-alike
Python 3 O(n) two pointers approach
WJTTW
0
1
determine if string halves are alike
1,704
0.774
Easy
24,649
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2591599/Python-5-Solutions-or-Easy-or-Clean-or-Straightforward
class Solution: def halvesAreAlike(self, s: str) -> bool: vowels, count = 'aeiouAEIOU', 0 for i in range(len(s) // 2): count += (s[i] in vowels) - (s[~i] in vowels) return not count
determine-if-string-halves-are-alike
[Python] 5 Solutions | Easy | Clean | Straightforward
andy2167565
0
19
determine if string halves are alike
1,704
0.774
Easy
24,650
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2591599/Python-5-Solutions-or-Easy-or-Clean-or-Straightforward
class Solution: def halvesAreAlike(self, s: str) -> bool: return not sum((s[i] in 'aeiouAEIOU') - (s[~i] in 'aeiouAEIOU') for i in range(len(s) // 2))
determine-if-string-halves-are-alike
[Python] 5 Solutions | Easy | Clean | Straightforward
andy2167565
0
19
determine if string halves are alike
1,704
0.774
Easy
24,651
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2591599/Python-5-Solutions-or-Easy-or-Clean-or-Straightforward
class Solution: def halvesAreAlike(self, s: str) -> bool: s, count = [s[:len(s) // 2], s[len(s) // 2:]], 0 for i, half in enumerate(s): for vowel in set(half).intersection('aeiouAEIOU'): count += half.count(vowel) if not i else -half.count(vowel) return not count
determine-if-string-halves-are-alike
[Python] 5 Solutions | Easy | Clean | Straightforward
andy2167565
0
19
determine if string halves are alike
1,704
0.774
Easy
24,652
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2591599/Python-5-Solutions-or-Easy-or-Clean-or-Straightforward
class Solution: def halvesAreAlike(self, s: str) -> bool: from collections import Counter C, count = list(map(Counter, (s[:len(s) // 2], s[len(s) // 2:]))), 0 for i, half in enumerate(C): for char in half: if char in 'aeiouAEIOU': count += half[char] if not i else -half[char] return not count
determine-if-string-halves-are-alike
[Python] 5 Solutions | Easy | Clean | Straightforward
andy2167565
0
19
determine if string halves are alike
1,704
0.774
Easy
24,653
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2591599/Python-5-Solutions-or-Easy-or-Clean-or-Straightforward
class Solution: def halvesAreAlike(self, s: str) -> bool: return sum(char in 'aeiouAEIOU' for char in s[:len(s) // 2]) == sum(char in 'aeiouAEIOU' for char in s[len(s) // 2:])
determine-if-string-halves-are-alike
[Python] 5 Solutions | Easy | Clean | Straightforward
andy2167565
0
19
determine if string halves are alike
1,704
0.774
Easy
24,654
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2591599/Python-5-Solutions-or-Easy-or-Clean-or-Straightforward
class Solution: def halvesAreAlike(self, s: str) -> bool: count = 0 for i, char in enumerate(s): if char in 'aeiouAEIOU': count += 1 if i < len(s) // 2 else -1 return not count
determine-if-string-halves-are-alike
[Python] 5 Solutions | Easy | Clean | Straightforward
andy2167565
0
19
determine if string halves are alike
1,704
0.774
Easy
24,655
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2591599/Python-5-Solutions-or-Easy-or-Clean-or-Straightforward
class Solution: def halvesAreAlike(self, s: str) -> bool: return not sum(((i < len(s) // 2) * 2 - 1) * (char in 'aeiouAEIOU') for i, char in enumerate(s))
determine-if-string-halves-are-alike
[Python] 5 Solutions | Easy | Clean | Straightforward
andy2167565
0
19
determine if string halves are alike
1,704
0.774
Easy
24,656
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2572541/python3-oror-Optimized-solution
class Solution: def halvesAreAlike(self, s: str) -> bool: dt = { 'a' : 1,'A':1, 'e' : 1,'E':1, 'i' : 1,'I':1, 'o' : 1,'O':1, 'u' : 1,'U':1 } i = 0 j= len(s)-1 count1 = count2 = 0 length = len(s)//2 while i<=length and j>=length: if dt.get(s[i],False): count1+=1 i+=1 if dt.get(s[j],False): count2+=1 j-=1 return count1==count2
determine-if-string-halves-are-alike
python3 || Optimized solution
shacid
0
11
determine if string halves are alike
1,704
0.774
Easy
24,657
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2546279/Easy-python-solution
class Solution: def halvesAreAlike(self, s: str) -> bool: c1=0 c2=0 vowels=["a","e","i","o","u"] s=s.lower() s1=s[:len(s)//2] s2=s[len(s)//2:] for i in vowels: c1=c1+s1.count(i) c2=c2+s2.count(i) return c1==c2
determine-if-string-halves-are-alike
Easy python solution
keertika27
0
14
determine if string halves are alike
1,704
0.774
Easy
24,658
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2535914/Python-oror-Easy-and-well-explained-solution
class Solution(object): def halvesAreAlike(self, s): """ :type s: str :rtype: bool """ length = len(s) firstHalf = s[:length/2] secondHalf = s[length/2:] countFirstHalf = 0 countSecondHalf = 0 for i in range(len(firstHalf)): if (firstHalf[i] == "a" or firstHalf[i] == "A"): countFirstHalf+=1 if (firstHalf[i] == "e" or firstHalf[i] == "E"): countFirstHalf+=1 if (firstHalf[i] == "i" or firstHalf[i] == "I"): countFirstHalf+=1 if (firstHalf[i] == "o" or firstHalf[i] == "O"): countFirstHalf+=1 if (firstHalf[i] == "u" or firstHalf[i] == "U"): countFirstHalf+=1 for i in range(len(secondHalf)): if (secondHalf[i] == "a" or secondHalf[i] == "A"): countSecondHalf+=1 if (secondHalf[i] == "e" or secondHalf[i] == "E"): countSecondHalf+=1 if (secondHalf[i] == "i" or secondHalf[i] == "I"): countSecondHalf+=1 if (secondHalf[i] == "o" or secondHalf[i] == "O"): countSecondHalf+=1 if (secondHalf[i] == "u" or secondHalf[i] == "U"): countSecondHalf+=1 if countFirstHalf == countSecondHalf: return True else: return False # print(firstHalf) # print(secondHalf)
determine-if-string-halves-are-alike
Python || Easy and well explained solution
ride-coder
0
8
determine if string halves are alike
1,704
0.774
Easy
24,659
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2535194/Python-O(n)-solution
class Solution: def halvesAreAlike(self, s: str) -> bool: vowels = ['a', 'e', 'i', 'o', 'u'] left_vowels_counter: int = 0 right_vowels_counter: int = 0 middle: int = int(len(s) / 2) for index, letter in enumerate(s): if letter.lower() in vowels: if index < middle: left_vowels_counter += 1 else: right_vowels_counter += 1 return left_vowels_counter == right_vowels_counter
determine-if-string-halves-are-alike
Python O(n) solution
anton_python
0
10
determine if string halves are alike
1,704
0.774
Easy
24,660
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2409788/Python3-93-faster
class Solution: def halvesAreAlike(self, s: str) -> bool: k = len(s)//2 vowels=['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] a=0 b=0 for i in range(len(s)): if(s[i] in vowels): if(i<k): a+=1 else: b+=1 return a==b
determine-if-string-halves-are-alike
Python3 93% faster
morrismoppp
0
7
determine if string halves are alike
1,704
0.774
Easy
24,661
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2299049/easy-python-solution
class Solution: def halvesAreAlike(self, s: str) -> bool: x = s[len(s)//2:] y = s[:len(s)//2] v = "aeiouAEIOU" p, q = 0, 0 for i in x: if i in v: p += 1 for i in y: if i in v: q += 1 if p == q: return 1 return 0
determine-if-string-halves-are-alike
easy python solution
rohansardar
0
10
determine if string halves are alike
1,704
0.774
Easy
24,662
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2293229/Simple-Python3-Solution
class Solution: def halvesAreAlike(self, s: str) -> bool: vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] length = len(s) // 2 a, b = s[:length], s[length:] cnt1 = cnt2 = 0 for c1 in a: if c1 in vowels: cnt1 +=1 for c2 in b: if c2 in vowels: cnt2 +=1 return cnt1 == cnt2
determine-if-string-halves-are-alike
Simple Python3 Solution
vem5688
0
11
determine if string halves are alike
1,704
0.774
Easy
24,663
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2242296/Beginner-Friendly-Solution-oror-41ms-Faster-Than-85-oror-Python
class Solution: def halvesAreAlike(self, s: str) -> bool: # Set up: - the half-way index number for s. # - a list of vowels to perform property checks with. halfway = len(s) // 2 vowels = ['a','e','i','o','u','A','E','I','O','U'] # Initialize vowel counters for each of the two halves. first_count = 0 second_count = 0 # Start counting the two halves both in each iteration of the for-loop. for i in range(halfway): if s[i] in vowels: first_count += 1 if s[i + halfway] in vowels: second_count += 1 # Return the result. return first_count == second_count
determine-if-string-halves-are-alike
Beginner Friendly Solution || 41ms, Faster Than 85% || Python
cool-huip
0
18
determine if string halves are alike
1,704
0.774
Easy
24,664
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2077711/Basic-solution-O(N)
class Solution: def halvesAreAlike(self, s: str) -> bool: vowels = "aeiou" s = s.lower() s1, s2 = s[:len(s) // 2], s[len(s) // 2:] return sum([1 for c in s1 if c in vowels]) == sum([1 for c in s2 if c in vowels])
determine-if-string-halves-are-alike
Basic solution O(N)
andrewnerdimo
0
19
determine if string halves are alike
1,704
0.774
Easy
24,665
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2030653/Runtime%3A-23-ms-93.06
class Solution(object): def halvesAreAlike(self, s): """ :type s: str :rtype: bool """ vowels = ('a', 'e', 'i', 'o', 'u') i, j = 0 , 0 s = s.lower() l = len(s)//2 for v in vowels: i += s[:l].count(v) for v in vowels: j += s[l:].count(v) return i == j
determine-if-string-halves-are-alike
Runtime: 23 ms 93.06% ට වඩා වේගවත්
akilaocj
0
17
determine if string halves are alike
1,704
0.774
Easy
24,666
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/2004839/python-solution
class Solution: def halvesAreAlike(self, s: str) -> bool: vowels =('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U') i = 0 cnt1 = cnt2 = 0 k = j = len(s) // 2 while(i != k): if s[i] in vowels: cnt1 += 1 if s[j] in vowels: cnt2 += 1 i += 1 j += 1 if cnt1 == cnt2: return True else: return False
determine-if-string-halves-are-alike
python solution
dharmrajrathod98
0
19
determine if string halves are alike
1,704
0.774
Easy
24,667
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1938762/Python-Solution-or-Simple-Vowel-Counting-Approach-or-Over-90-Faster
class Solution: def countVowels(self,s): count = 0 for e in s: if e in "aeiouAEIOU": count += 1 return count def halvesAreAlike(self, s: str) -> bool: return self.countVowels(s[:len(s)//2]) == self.countVowels(s[len(s)//2:])
determine-if-string-halves-are-alike
Python Solution | Simple Vowel Counting Approach | Over 90% Faster
Gautam_ProMax
0
17
determine if string halves are alike
1,704
0.774
Easy
24,668
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1906932/Python-Solution
class Solution: def halvesAreAlike(self, s: str) -> bool: n = len(s) // 2 target = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] s1 = s2 = 0 for char1, char2 in zip(s[:n], s[n:]): s1 += (0, 1)[char1 in target] s2 += (0, 1)[char2 in target] return s1 == s2
determine-if-string-halves-are-alike
Python Solution
hgalytoby
0
25
determine if string halves are alike
1,704
0.774
Easy
24,669
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1906932/Python-Solution
class Solution: def halvesAreAlike(self, s: str) -> bool: n = len(s) // 2 target = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] return len(list(filter(lambda x: x in target, s[:n]))) == len(list(filter(lambda x: x in target, s[n:])))
determine-if-string-halves-are-alike
Python Solution
hgalytoby
0
25
determine if string halves are alike
1,704
0.774
Easy
24,670
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1808588/Easy-and-Fast-solution-95
class Solution: def halvesAreAlike(self, s: str) -> bool: a=s[:len(s)//2] b=s[len(s)//2:] v='aeiouAEIOU' c,d=0,0 for i in a: if i in v: c+=1 #count number of vowels in first half for j in b: if j in v: d+=1 #count number of vowels in second half return c==d
determine-if-string-halves-are-alike
Easy and Fast solution 95%
adityabaner
0
48
determine if string halves are alike
1,704
0.774
Easy
24,671
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1700168/Python-with-functions
class Solution: def halvesAreAlike(self, s: str) -> bool: mid = len(s)//2 return self.sumVowal(s[:mid]) == self.sumVowal(s[mid:]) def isVowal(self, c) -> bool: if c in "aeiouAEIOU": return True return False def sumVowal(self, s) -> int: return sum([1 for c in s if self.isVowal(c)])
determine-if-string-halves-are-alike
Python with functions
CleverUzbek
0
41
determine if string halves are alike
1,704
0.774
Easy
24,672
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1690640/Python-Easy-Understanding
class Solution: def halvesAreAlike(self, s: str) -> bool: mid = len(s)//2 s1_half = s[0:mid] s2_half = s[mid:] vow = ['a','e','i','o','u','A','E','I','O','U'] c=0 d=0 for i in range(len(s1_half)): if s1_half[i] in vow: c = c+1 if s2_half[i] in vow: d = d+1 if c == d: return True else: return False
determine-if-string-halves-are-alike
[Python] Easy Understanding
Ron99
0
50
determine if string halves are alike
1,704
0.774
Easy
24,673
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1690329/**-Python-code%3A
class Solution: def halvesAreAlike(self, s: str) -> bool: vowels="aeiouAEIOU" l=len(s)//2 a,b=s[:l],s[l:] aC,bC=0,0 for i in range(l): if a[i] in vowels: aC+=1 if b[i] in vowels: bC+=1 return aC==bC
determine-if-string-halves-are-alike
** Python code:
Anilchouhan181
0
25
determine if string halves are alike
1,704
0.774
Easy
24,674
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1659442/Super-simple-Python3-solution-faster-than-100-of-submissions
class Solution: def halvesAreAlike(self, s: str) -> bool: half_index = int(len(s)/2) first_half = s[:half_index] second_half = s[half_index:] vowels_first_half = [i for i in first_half if i in "aeiouAEIOU"] vowels_second_half = [i for i in second_half if i in "aeiouAEIOU"] if len(vowels_first_half) == len(vowels_second_half): return True else: return False
determine-if-string-halves-are-alike
Super simple Python3 solution, faster than 100% of submissions
y-arjun-y
0
59
determine if string halves are alike
1,704
0.774
Easy
24,675
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1412707/Runtime%3A-24-ms-faster-than-98.83-of-Python3-online-submissions-f
class Solution: def halvesAreAlike(self, s: str) -> bool: a=0 n=len(s) if n%2!=0: return False d={"a","e","i","o","u","A","E","I","O","U"} for i in range((n//2)): if s[i] in d: a+=1 for i in range((n//2),n): if s[i] in d: a-=1 return False if a else True
determine-if-string-halves-are-alike
Runtime: 24 ms, faster than 98.83% of Python3 online submissions f
harshmalviya7
0
80
determine if string halves are alike
1,704
0.774
Easy
24,676
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1411064/Python-Best-Solution
class Solution: def halvesAreAlike(self, s: str) -> bool: lst = set("aeiouAEIOU") n = len(s)//2 count = 0 for i in s[:n]: if i in lst: count +=1 for i in s[n:]: if i in lst: count -=1 return True if count ==0 else False
determine-if-string-halves-are-alike
Python Best Solution
phuoctfx11137
0
45
determine if string halves are alike
1,704
0.774
Easy
24,677
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1148976/Python-3-or-Two-pointer-or-Set
class Solution: def halvesAreAlike(self, s: str) -> bool: lc = 0 rc = 0 temp = set(['a', 'e', 'i', 'o','u', 'A','E', 'I','O', 'U']) start = 0 end = len(s) - 1 while start < end: if s[start] in temp: lc += 1 if s[end] in temp: rc += 1 start += 1 end -= 1 return lc == rc
determine-if-string-halves-are-alike
Python 3 | Two pointer | Set
abhyasa
0
35
determine if string halves are alike
1,704
0.774
Easy
24,678
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1147173/Python-3-count-vowel-one-pass-in-place
class Solution: def halvesAreAlike(self, s: str) -> bool: """ Place pointer a at the index 0 and pointer b at last index. Traverse the string s up to half of its length. Count vowel with helper function Time: O(N) Space: O(1) """ # Function name and parameter itself should be self-explainable, to avoid the need of having additional comments. def is_vowel(letter): return a in "aeiouAEIOU" a,b = 0,0 L = len(s) for i in range(L // 2): if is_vowel(s[i]): a += 1 if is_vowel(s[L - 1 - i]): b += 1 return a == b
determine-if-string-halves-are-alike
[Python 3] count vowel one pass in place
anonymous1127
0
23
determine if string halves are alike
1,704
0.774
Easy
24,679
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/1009743/Ultra-Simple-CppPython3-Solution-or-Suggestions-for-optimization-are-welcomed
class Solution: def halvesAreAlike(self, s: str) -> bool: count=0 for i in range(0,int(len(s)/2)): if s[i]=='a' or s[i]=='e' or s[i]=='i' or s[i]=='o' or s[i]=='u' or s[i]=='A' or s[i]=='E' or s[i]=='I' or s[i]=='O' or s[i]=='U': count=count+1 for i in range(int(len(s)/2),len(s)): if s[i]=='a' or s[i]=='e' or s[i]=='i' or s[i]=='o' or s[i]=='u' or s[i]=='A' or s[i]=='E' or s[i]=='I' or s[i]=='O' or s[i]=='U': count=count-1 if count==0: return 1 return 0
determine-if-string-halves-are-alike
Ultra Simple Cpp/Python3 Solution | Suggestions for optimization are welcomed
angiras_rohit
0
17
determine if string halves are alike
1,704
0.774
Easy
24,680
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/994657/Python-One-line-Faster-than-96
class Solution: def halvesAreAlike(self, s: str) -> bool: vowels,size = {"a","e","i","o","u","A","E","I", "O","U"}, len(s) return sum([ch in vowels for ch in s[0:size//2]]) == sum([ch in vowels for ch in s[size//2:]])
determine-if-string-halves-are-alike
Python One line, Faster than 96%
selimtanriverdien
0
108
determine if string halves are alike
1,704
0.774
Easy
24,681
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/993723/Short-Python-Solution
class Solution: def halvesAreAlike(self, s: str) -> bool: a, b = s[:len(s)//2], s[len(s)//2:] return True if sum(map(a.lower().count, "aeiou")) == sum(map(b.lower().count, "aeiou")) else False
determine-if-string-halves-are-alike
Short Python Solution
BirdLQ
0
59
determine if string halves are alike
1,704
0.774
Easy
24,682
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/988713/Intuitive-approach-and-code-explain-itself-well
class Solution: def halvesAreAlike(self, s: str) -> bool: s, s_half_len = s.lower(), len(s) // 2 a, b = s[:s_half_len], s[s_half_len:] a_vows_len = len(list(filter(lambda e: e in 'aeiou', a))) b_vows_len = len(list(filter(lambda e: e in 'aeiou', b))) return a_vows_len == b_vows_len
determine-if-string-halves-are-alike
Intuitive approach and code explain itself well
puremonkey2001
0
20
determine if string halves are alike
1,704
0.774
Easy
24,683
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/988685/PYTHON-oror-EASY-oror-O(N)-TIME-oror-O(1)-SPACE
class Solution: def halvesAreAlike(self, string: str) -> bool: j = len(string) // 2 a = 0 b = 0 vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] for i in range(len(string) // 2): a_c = string[i] b_c = string[j + i] if a_c in vowels: a += 1 if b_c in vowels: b += 1 return a == b # sol = Solution() # print(sol.halvesAreAlike("MerryChristmas"))
determine-if-string-halves-are-alike
PYTHON || EASY || O(N) TIME || O(1) SPACE
akashgkrishnan
0
46
determine if string halves are alike
1,704
0.774
Easy
24,684
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/988344/Python-one-for-loop
class Solution: def halvesAreAlike(self, s: str) -> bool: vowel = set('aeiouAEIOU') count = 0 for i in range(len(s)//2): if s[i] in vowel: count += 1 if s[-i-1] in vowel: count -= 1 return count == 0
determine-if-string-halves-are-alike
Python, one for loop
blue_sky5
0
30
determine if string halves are alike
1,704
0.774
Easy
24,685
https://leetcode.com/problems/determine-if-string-halves-are-alike/discuss/988344/Python-one-for-loop
class Solution: def halvesAreAlike(self, s: str) -> bool: return sum(1 if i < len(s)//2 else -1 for i, c in enumerate(s) if c in 'aeiouAEIOU') == 0
determine-if-string-halves-are-alike
Python, one for loop
blue_sky5
0
30
determine if string halves are alike
1,704
0.774
Easy
24,686
https://leetcode.com/problems/maximum-number-of-eaten-apples/discuss/988437/Python3-priority-queue
class Solution: def eatenApples(self, apples: List[int], days: List[int]) -> int: ans = 0 pq = [] # min-heap for i, (x, d) in enumerate(zip(apples, days)): while pq and pq[0][0] <= i: heappop(pq) # rotten if x: heappush(pq, (i+d, x)) if pq: ii, x = heappop(pq) if x-1: heappush(pq, (ii, x-1)) ans += 1 i += 1 while pq: ii, x = heappop(pq) x = min(x, ii-i) ans += x i += x return ans
maximum-number-of-eaten-apples
[Python3] priority queue
ye15
3
113
maximum number of eaten apples
1,705
0.381
Medium
24,687
https://leetcode.com/problems/maximum-number-of-eaten-apples/discuss/995033/Sorting-doesn't-work-here-(only-4463-test-cases-passed)
class Solution: def eatenApples(self, apples: List[int], days: List[int]) -> int: n=len(days) l=[] le=0 for i in range(n): if days[i]!=0 or apples[i]!=0: l.append([apples[i],days[i]+i]) le+=1 l.sort(key=lambda x:x[1]) day=0 res=0 i=0 while(i<le): A=l[i][0] D=l[i][1] if day<D: diff_days=D-day mn=min(diff_days,A) day+=mn res+=mn i+=1 return res
maximum-number-of-eaten-apples
Sorting doesn't work here (only 44/63 test cases passed)
_Rehan12
1
78
maximum number of eaten apples
1,705
0.381
Medium
24,688
https://leetcode.com/problems/where-will-the-ball-fall/discuss/1443268/Python-3-or-DFS-Simulation-or-Explanation
class Solution: def findBall(self, grid: List[List[int]]) -> List[int]: m, n = len(grid), len(grid[0]) @cache def helper(r, c): if r == m: return c elif grid[r][c] == 1 and c+1 < n and grid[r][c+1] == 1: return helper(r+1, c+1) elif grid[r][c] == -1 and 0 <= c-1 and grid[r][c-1] == -1: return helper(r+1, c-1) else: return -1 return [helper(0, j) for j in range(n)]
where-will-the-ball-fall
Python 3 | DFS, Simulation | Explanation
idontknoooo
28
1,400
where will the ball fall
1,706
0.716
Medium
24,689
https://leetcode.com/problems/where-will-the-ball-fall/discuss/2765461/EASY-PYTHON-SOLUTON-oror-O(mxn)-oror-Comments
class Solution: def findBall(self, grid: List[List[int]]) -> List[int]: m,n = len(grid),len(grid[0]) def check(row,col): ### If a ball get out of the box, return col if row==m: return col ### note that since grid contains 1 and -1 representing to right and to left, ### we can just add the grid[row][col] to current collumn to get the new column new_col = col+grid[row][col] ### if the new column is already out of the box ### or the neighbor cell doesn't equal to grid[row][col] ### the ball will get stuck and we just return -1 if new_col==n or new_col==-1 or grid[row][new_col]!=grid[row][col]: return -1 else: return check(row+1,new_col) res = [] for i in range(n): res.append(check(0,i)) return res
where-will-the-ball-fall
EASY PYTHON SOLUTON || O(mxn) || Comments
raghavdabra
4
189
where will the ball fall
1,706
0.716
Medium
24,690
https://leetcode.com/problems/where-will-the-ball-fall/discuss/988447/Python3-simulation
class Solution: def findBall(self, grid: List[List[int]]) -> List[int]: m, n = len(grid), len(grid[0]) # dimensions ans = [-1]*n for j in range(n): k = j for i in range(m): kk = k + grid[i][k] if not 0 <= kk < n or grid[i][k] * grid[i][kk] < 0: break k = kk else: ans[j] = k # no break return ans
where-will-the-ball-fall
[Python3] simulation
ye15
2
166
where will the ball fall
1,706
0.716
Medium
24,691
https://leetcode.com/problems/where-will-the-ball-fall/discuss/2765400/Python-Solution-with-Explanation-and-Diagram-or-97-Faster
class Solution: def findBall(self, grid: List[List[int]]) -> List[int]: r_len = len(grid) c_len = len(grid[0]) output = list(range(c_len)) for r in range(r_len): for i in range(c_len): c = output[i] if c == -1: continue c_nxt = c + grid[r][c] if c_nxt < 0 or c_nxt >= c_len or grid[r][c_nxt] == -grid[r][c]: output[i] = -1 continue output[i] += grid[r][c] return output
where-will-the-ball-fall
✔️ Python Solution with Explanation and Diagram | 97% Faster 🔥
pniraj657
1
109
where will the ball fall
1,706
0.716
Medium
24,692
https://leetcode.com/problems/where-will-the-ball-fall/discuss/2765283/SIMPLE-SOLUTION-USING-DFS
class Solution: def dfs(self,x,y,m,n,grid,visited): if visited[x][y]!=None: return visited[x][y] if y+grid[x][y]<0 or y+grid[x][y]>=n or (grid[x][y]+grid[x][y+grid[x][y]]==0): visited[x][y]=-1 return -1 visited[x][y]=y+grid[x][y] if x+1<m: return self.dfs(x+1,y+grid[x][y],m,n,grid,visited) else: return visited[x][y] def findBall(self, grid: List[List[int]]) -> List[int]: m=len(grid) n=len(grid[0]) visited=[[None]*n for _ in range(m)] result=[] for i in range(n): x=self.dfs(0,i,m,n,grid,visited) result.append(x) return result
where-will-the-ball-fall
SIMPLE SOLUTION USING DFS
beneath_ocean
1
58
where will the ball fall
1,706
0.716
Medium
24,693
https://leetcode.com/problems/where-will-the-ball-fall/discuss/2764916/Python-3-oror-DFS-solution
class Solution: def findBall(self, grid: List[List[int]]) -> List[int]: rows, cols = len(grid), len(grid[0]) def dfs(r, c): if r == rows-1: if grid[r][c] == 1 and c+1 < cols and grid[r][c] == grid[r][c+1]: return c + grid[r][c] if grid[r][c] == -1 and c-1 > -1 and grid[r][c] == grid[r][c-1]: return c + grid[r][c] return -1 elif grid[r][c] == 1 and c+1 < cols and grid[r][c] == grid[r][c+1]: return dfs(r+1, c+1) elif grid[r][c] == -1 and c-1 > -1 and grid[r][c] == grid[r][c-1]: return dfs(r+1, c-1) else: return -1 ans = [] for c in range(cols): ans.append(dfs(0, c)) return ans
where-will-the-ball-fall
Python 3 || DFS solution
sagarhasan273
1
73
where will the ball fall
1,706
0.716
Medium
24,694
https://leetcode.com/problems/where-will-the-ball-fall/discuss/2428020/O(n)-BFS-by-swapping-two-queues-and-moving-pattern-(easy-understanding-with-examples)
class Solution: def findBall(self, grid: List[List[int]]) -> List[int]: """ """ m, n = len(grid), len(grid[0]) p = {i: (0,i) for i in range(n)} ans = [-1] * n while len(p)>0: print(p) p1 = {} for k in p: r, c = p[k] if r==m: ans[k] = c else: if grid[r][c]==1 and c+1<=n-1 and grid[r][c+1]==1: p1[k] = (r+1, c+1) elif grid[r][c]==-1 and c-1>=0 and grid[r][c-1]==-1: p1[k] = (r+1, c-1) p = p1 print("ans: ", ans) print("=" * 20) return ans # print = lambda *a, **aa: ()
where-will-the-ball-fall
O(n) BFS by swapping two queues and moving pattern (easy understanding with examples)
dntai
1
41
where will the ball fall
1,706
0.716
Medium
24,695
https://leetcode.com/problems/where-will-the-ball-fall/discuss/2826586/Well-explained-with-commented-code-easy-to-follow-python-solution.
class Solution: def findBall(self, grid: List[List[int]]) -> List[int]: def helper(row, balls): if row == len(grid): return for ball, col in enumerate(balls): if col != -1: #the ball is not already stuck if grid[row][col] == 1: #ball goes right in next row if col + 1 < cols: balls[ball] += 1 else: #ball is out of boundary from right side balls[ball] = -1 elif grid[row][col] == -1: # ball goes left in next row if col - 1 >= 0: balls[ball] -= 1 else: # ball is out of boundary from left side balls[ball] = -1 else: #ball is in grid where it will get stuck balls[ball] = -1 helper(row+1, balls) #mark all cells where ball will get stuck as 0 for r, row in enumerate(grid): for c in range(1, len(grid[0])): if grid[r][c-1] == 1 and grid[r][c] == -1: grid[r][c-1] = 0 grid[r][c] = 0 cols = len(grid[0]) ans = [i for i in range(cols)] helper(0, ans) return ans
where-will-the-ball-fall
Well explained with commented code, easy to follow python solution.
dkashi
0
1
where will the ball fall
1,706
0.716
Medium
24,696
https://leetcode.com/problems/where-will-the-ball-fall/discuss/2824482/Python3-Easy-solution
class Solution: def findBall(self, grid: List[List[int]]) -> List[int]: len_row, len_col = len(grid),len(grid[0]) answer = [] for c in range(len_col): row, col = 0, c for _ in range(len_row): # Stuck conditions if (grid[row][col] == 1 and (col == len_col-1 or grid[row][col+1] == -1)) or \ (grid[row][col] == -1 and (col == 0 or grid[row][col-1] == 1)): answer.append(-1) break # Last Row if row == len_row-1: if grid[row][col] == 1: answer.append(col+1) else: answer.append(col-1) # Change Row, Column if grid[row][col] == 1: col += 1 else: col -= 1 row += 1 return answer
where-will-the-ball-fall
Python3 Easy solution
Coaspe
0
1
where will the ball fall
1,706
0.716
Medium
24,697
https://leetcode.com/problems/where-will-the-ball-fall/discuss/2816878/Python-short-recursive-solution-or-O(M-*-N)-or-O(M)
class Solution: def findBall(self, grid: List[List[int]]) -> List[int]: max_row, max_col = len(grid), len(grid[0]) def fall_pos(i, j, layer): if layer == "above": if (grid[i][j] == 1 and (j == max_col-1 or grid[i][j+1] == -1)) or (grid[i][j] == -1 and (j == 0 or grid[i][j-1] == 1)): return -1 else: return fall_pos(i, j+1, "below") if grid[i][j] == 1 else fall_pos(i, j-1, "below") else: return j if i == max_row-1 else fall_pos(i+1, j, "above") return [fall_pos(0, j, "above") for j in range(max_col)]
where-will-the-ball-fall
Python short recursive solution | O(M * N) | O(M)
xyp7x
0
2
where will the ball fall
1,706
0.716
Medium
24,698
https://leetcode.com/problems/where-will-the-ball-fall/discuss/2814138/Python-(Simple-DFS)
class Solution: def findBall(self, grid): m, n = len(grid), len(grid[0]) def dfs(i,j): if i == m: return j if (j == 0 and grid[i][j] == -1) or (j == n-1 and grid[i][j] == 1) or (grid[i][j] == 1 and grid[i][j+1] == -1) or (grid[i][j] == -1 and grid[i][j-1] == 1): return -1 if grid[i][j] == 1: return dfs(i+1,j+1) if grid[i][j] == -1: return dfs(i+1,j-1) return [dfs(0,i) for i in range(n)]
where-will-the-ball-fall
Python (Simple DFS)
rnotappl
0
1
where will the ball fall
1,706
0.716
Medium
24,699