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/longest-substring-without-repeating-characters/discuss/1342438/Easy-Python-Solution-w-Comments
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: max_len = 0 #holds the current non repeating substring passed = [] for i in range(len(s)): if s[i] not in passed: passed.append(s[i]) max_len = max(len(passed), max_len) else: #if repeat found, update max_len with current su...
longest-substring-without-repeating-characters
Easy Python Solution w/ Comments
mguthrie45
4
413
longest substring without repeating characters
3
0.338
Medium
100
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2670619/Simple-python-code-with-explanation
class Solution: def lengthOfLongestSubstring(self, s): #create a variable and assign value 0 to it longest_substring = 0 #create a pointer l and assign 0 to it l = 0 #create a pointer r and assign 0 to it r = 0 #iterate over the elements in string(s) ...
longest-substring-without-repeating-characters
Simple python code with explanation
thomanani
3
274
longest substring without repeating characters
3
0.338
Medium
101
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2670619/Simple-python-code-with-explanation
class Solution: #just used the iterator as r pointer def lengthOfLongestSubstring(self, s): summ = 0 l = 0 for r in range(len(s)): if len(s[l:r+1]) == len(set(s[l:r+1])): summ = max(summ,r-l+1) else: l = l + 1 return summ
longest-substring-without-repeating-characters
Simple python code with explanation
thomanani
3
274
longest substring without repeating characters
3
0.338
Medium
102
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2450783/Python3%3A-Sliding-window-O(N)-greater-Faster-than-99.7
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: seen = {} left = 0 output = 0 for right, char in enumerate(s): # If char not in seen dictionary, we can keep increasing the window size by moving right pointer if char not in seen: ...
longest-substring-without-repeating-characters
Python3: Sliding window O(N) => Faster than 99.7%
cquark
3
294
longest substring without repeating characters
3
0.338
Medium
103
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2423128/Faster-than-99.5-Python-Solution
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: substr = deque() index = set() max_len = 0 for char in s: if char not in index: substr.append(char) index.add(char) else: while substr: ...
longest-substring-without-repeating-characters
Faster than 99.5% Python Solution
vyalovvldmr
3
332
longest substring without repeating characters
3
0.338
Medium
104
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2383487/Fastest-Solution-Explained0ms100-O(n)time-complexity-O(n)space-complexity
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: seen = {} l = 0 output = 0 for r in range(len(s)): """ If s[r] not in seen, we can keep increasing the window size by moving right pointer """ if s[r] not in seen: ...
longest-substring-without-repeating-characters
[Fastest Solution Explained][0ms][100%] O(n)time complexity O(n)space complexity
cucerdariancatalin
3
808
longest substring without repeating characters
3
0.338
Medium
105
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2377883/Python-Solution-or-Classic-Two-Pointer-Sliding-Window-Based
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: store = set() maxLen = 0 left = 0 for right in range(len(s)): # to skip consecutive repeating characters while s[right] in store: store.remove(s[left]) # lower window size ...
longest-substring-without-repeating-characters
Python Solution | Classic Two Pointer - Sliding Window Based
Gautam_ProMax
3
188
longest substring without repeating characters
3
0.338
Medium
106
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/673878/Simple-python
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: length, start = 0, 0 seen = {} for idx, c in enumerate(s): if c in seen and start <= seen[c]: start = seen[c] + 1 length = max(length, idx-start+1) seen[c] = idx ret...
longest-substring-without-repeating-characters
Simple python
etherealoptimist
3
737
longest substring without repeating characters
3
0.338
Medium
107
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/266333/Python-Solution-faster-than-100.00-other-solutions
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if len(s): result = [] temp = s[0] for i in s[1:]: if i not in temp: temp += i elif i == temp[0]: temp = temp[1:] + i ...
longest-substring-without-repeating-characters
Python Solution faster than 100.00% other solutions
_l0_0l_
3
586
longest substring without repeating characters
3
0.338
Medium
108
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2778596/Python-3-O(n)-faster-than-99.94
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: seen = {} best = 0 front = -1 for i, c in enumerate(s): if c in seen and front < seen[c]: front = seen[c] seen[c] = i if (...
longest-substring-without-repeating-characters
Python 3 O(n) faster than 99.94%
Zachii
2
756
longest substring without repeating characters
3
0.338
Medium
109
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2775814/Efficient-Python-solution-(SLIDING-WINDOW)
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: length = 0 count = {} l = 0 for r in range(len(s)): count[s[r]] = count.get(s[r], 0)+1 while count[s[r]] > 1: count[s[l]] -= 1 l += 1 length = max(l...
longest-substring-without-repeating-characters
Efficient Python solution (SLIDING WINDOW)
really_cool_person
2
1,100
longest substring without repeating characters
3
0.338
Medium
110
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2764542/Python-Simple-Python-Solution
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: m=0 for i in range(len(s)): l=[] c=0 for j in range(i,len(s)): if s[j] not in l: l.append(s[j]) c+=1 m=max(m,c) ...
longest-substring-without-repeating-characters
[ Python ]🐍🐍Simple Python Solution ✅✅
sourav638
2
18
longest substring without repeating characters
3
0.338
Medium
111
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2488819/Python-simple-solution.-O(n)
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if len(s) == 0: return 0 global_max = 1 left = 0 used = {} for right in range(0,len(s)): if s[right] in used: left = max(left, used[s[right]]+1) ...
longest-substring-without-repeating-characters
Python simple solution. O(n)
haruka980209
2
180
longest substring without repeating characters
3
0.338
Medium
112
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2403381/C%2B%2BPython-O(N)-Solution-with-better-and-faster-approach
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: start = maxLength = 0 usedChar = {} for i in range(len(s)): if s[i] in usedChar and start <= usedChar[s[i]]: start = usedChar[s[i]] + 1 else: ...
longest-substring-without-repeating-characters
C++/Python O(N) Solution with better and faster approach
arpit3043
2
422
longest substring without repeating characters
3
0.338
Medium
113
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2359311/Python-runtime-63.07-memory-49.48
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: max_len = 0 sub = set() start_delete_index = 0 for index, character in enumerate(s): while character in sub: sub.remove(s[start_delete_index]) start_delete_index += 1 ...
longest-substring-without-repeating-characters
Python, runtime 63.07%, memory 49.48%
tsai00150
2
190
longest substring without repeating characters
3
0.338
Medium
114
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2359311/Python-runtime-63.07-memory-49.48
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: max_len = start = 0 seen = {} for index, character in enumerate(s): if character in seen and start <= seen[character]: start = seen[character] + 1 seen[character] = index ma...
longest-substring-without-repeating-characters
Python, runtime 63.07%, memory 49.48%
tsai00150
2
190
longest substring without repeating characters
3
0.338
Medium
115
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2233725/Simple-python-solution
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: mapping = {} output = 0 start = 0 for char in range(len(s)): if s[char] in mapping: start = max(mapping[s[char]] + 1,start) mapping[s[char]] = char ...
longest-substring-without-repeating-characters
Simple python solution
asaffari101
2
182
longest substring without repeating characters
3
0.338
Medium
116
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2119781/Python-or-Easy-Solution-using-hashset
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: res = 0 charSet = set() l = 0 for r in range(len(s)): while s[r] in charSet: charSet.remove(s[l]) l += 1 charSet.add(s[r]) res = max(res,r-l+1) ...
longest-substring-without-repeating-characters
Python | Easy Solution using hashset
__Asrar
2
272
longest substring without repeating characters
3
0.338
Medium
117
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2078071/Python3-sliding-window-solution-in-O(n)-77ms-14MB
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: chars = set() l = r = 0 top = 0 while r < len(s): while s[r] in chars: chars.remove(s[l]) l += 1 chars.add(s[r]) top = max(top, len(chars)) ...
longest-substring-without-repeating-characters
Python3 sliding window solution in O(n) 77ms 14MB
GeorgeD8
2
190
longest substring without repeating characters
3
0.338
Medium
118
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/1889624/Simple-python3-solution-oror-two-pointer
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: i=0 j=i+1 max_len = 0 temp = [] while(i<len(s)): temp.append(s[i]) while(j<len(s) and s[j] not in temp): temp.append(s[j]) j+=1 ...
longest-substring-without-repeating-characters
Simple python3 solution || two pointer
darshanraval194
2
348
longest substring without repeating characters
3
0.338
Medium
119
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/1860113/Clean-Python-3-solution-(sliding-window-%2B-hash-map)
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: dic = dict() max_len = 0 l = 0 r = 0 while l < len(s) and r < len(s): if s[r] not in dic: dic[s[r]] = True r += 1 max_len = max(max_len, r - l) ...
longest-substring-without-repeating-characters
Clean Python 3 solution (sliding window + hash map)
Hongbo-Miao
2
128
longest substring without repeating characters
3
0.338
Medium
120
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/1829631/Python-or-Walkthrough-%2B-Complexity-or-Sliding-Window
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: def has_duplicates(start, end): seen = set() while start <= end: letter = s[start] if letter in seen: return True else: seen.add(...
longest-substring-without-repeating-characters
Python | Walkthrough + Complexity | Sliding Window
leetbeet73
2
520
longest substring without repeating characters
3
0.338
Medium
121
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/1829631/Python-or-Walkthrough-%2B-Complexity-or-Sliding-Window
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: start = longest = 0 seen = {} for end in range(len(s)): letter = s[end] if letter in seen: start = max(seen[letter] + 1, start) longest = max(longest, end - start + 1) ...
longest-substring-without-repeating-characters
Python | Walkthrough + Complexity | Sliding Window
leetbeet73
2
520
longest substring without repeating characters
3
0.338
Medium
122
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/1077043/Python3-O(1)-space!
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: if len(s) == 1: return 1 long = 0 l = 0 r = 1 while r < len(s): if s[r] not in s[l:r]: r += 1 long = max(long, len(s[l:r])) else: ...
longest-substring-without-repeating-characters
Python3 O(1) space!
BeetCoder
2
253
longest substring without repeating characters
3
0.338
Medium
123
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/905320/Python-3-solution-or-Memory-Usage%3A-14.3-MB-less-than-100.00-of-Python3-online-submissions.
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: maxlength = 0 startIndex = 0 i = 0 letters = [] while i < len(s): if s[i] not in letters: letters.append(s[i]) i = i + 1 else: maxlength ...
longest-substring-without-repeating-characters
Python 3 solution | Memory Usage: 14.3 MB, less than 100.00% of Python3 online submissions.
manojkumarmanusai
2
358
longest substring without repeating characters
3
0.338
Medium
124
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/468273/Easy-to-follow-Python3-solution-(faster-than-99-and-memory-usage-less-than-100)
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: b = '' l = '' for c in s: if c in b: i = b.index(c) b = b[i+1:] + c else: b += c if len(b) > len(l): l = b re...
longest-substring-without-repeating-characters
Easy-to-follow Python3 solution (faster than 99% & memory usage less than 100%)
gizmoy
2
391
longest substring without repeating characters
3
0.338
Medium
125
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2619481/Simple-python3-solution
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: charSet = set() l = 0 res = 0 for r in range(len(s)): while s[r] in charSet: charSet.remove(s[l]) l += 1 charSet.add(s[r]) res = max(res, r - l + 1) ...
longest-substring-without-repeating-characters
Simple python3 solution
__Simamina__
1
174
longest substring without repeating characters
3
0.338
Medium
126
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2591490/Python-solution-with-comments
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: seen = {} #Hashmap to keep track of where we last saw a given character res = 0 #Initial longest substring start = 0 #Window starting point for end,char in enumerate(s): ...
longest-substring-without-repeating-characters
Python solution with comments
harrisonhcue
1
124
longest substring without repeating characters
3
0.338
Medium
127
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2511175/simple-Python3-solution
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: charSet = set() l = 0 res = 0 for r in range(len(s)): while s[r] in charSet: charSet.remove(s[l]) l += 1 charSet.add(s[r]) res = max(res, r - l + 1)...
longest-substring-without-repeating-characters
simple Python3 solution
__Simamina__
1
163
longest substring without repeating characters
3
0.338
Medium
128
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2445855/Python3-or-2-Different-Solutions-or-Optimal-Solution
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: # Method 1: Naive Inefficient Approach l = 0 r = 0 ans = 0 maxans = 0 count = [] while r<=len(s)-1: if s[r] not in count: count.append(s[r]) ans += 1...
longest-substring-without-repeating-characters
Python3 | 2 Different Solutions | Optimal Solution
chawlashivansh
1
187
longest substring without repeating characters
3
0.338
Medium
129
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2306073/Python-Two-Clean-Sliding-Window-Solutions
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: maxLength = left = 0 seen = {} for i in range(len(s)): # If letter is already seen, move left window by 1 to the right until letter is no longer in dictionary while s[i] in seen: ...
longest-substring-without-repeating-characters
Python - Two Clean Sliding Window Solutions
Reinuity
1
141
longest substring without repeating characters
3
0.338
Medium
130
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2306073/Python-Two-Clean-Sliding-Window-Solutions
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: maxLength = left = 0 seen = {} for i in range(len(s)): #If letter is already seen if s[i] in seen: # Set left window to the max between its current index, or the index of the last o...
longest-substring-without-repeating-characters
Python - Two Clean Sliding Window Solutions
Reinuity
1
141
longest substring without repeating characters
3
0.338
Medium
131
https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/2188837/Sliding-Window-Solution-oror-Implementation-of-Approach-by-Aditya-Verma
class Solution: def lengthOfLongestSubstring(self, s: str) -> int: i, j = 0, 0 n = len(s) frequencyArray = [0]*(5 * 10**4) maxSize = 0 while j < n: frequencyArray[ord(s[j]) - ord('a')] += 1 mapSize = (5*10**4) - frequencyArray.count(0) ...
longest-substring-without-repeating-characters
Sliding Window Solution || Implementation of Approach by Aditya Verma
Vaibhav7860
1
151
longest substring without repeating characters
3
0.338
Medium
132
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/949705/Python3-two-pointer-greater9621-runtime-commented
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: # Get the lengths of both lists l1,l2 = len(nums1), len(nums2) # Determine the middle middle = (l1 + l2) / 2 # EDGE CASE: # If we only have 1 value (e.g. [1], []), return nums1[0] if the leng...
median-of-two-sorted-arrays
Python3 two pointer >96,21% runtime [commented]
tomhagen
32
5,100
median of two sorted arrays
4
0.353
Hard
133
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1592457/Python-O(m-%2B-n)-time-O(1)-space
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: m, n = len(nums1), len(nums2) mid = (m + n) // 2 + 1 prev2 = prev1 = None i = j = 0 for _ in range(mid): prev2 = prev1 if j == n or (i != m and nums1[i] <= ...
median-of-two-sorted-arrays
Python O(m + n) time, O(1) space
dereky4
21
2,100
median of two sorted arrays
4
0.353
Hard
134
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1174195/C%2B%2BPython-O(log(m%2Bn))-solution
class Solution: def findkth(self, a: List[int], b: List[int], target) -> int: # print("a: {}, b:{}".format(a, b)) n, m = len(a), len(b) if n <= 0: return b[target - 1] if m <= 0: return a[target - 1] med_a, med_b = n // 2 + 1, m // 2 + 1 ma, mb...
median-of-two-sorted-arrays
C++/Python O(log(m+n)) solution
m0biu5
21
4,800
median of two sorted arrays
4
0.353
Hard
135
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1309837/Elegant-Python-Binary-Search-or-O(log(min(mn)))-O(1)
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: if len(nums2) < len(nums1): nums1, nums2 = nums2, nums1 m, n = len(nums1), len(nums2) left, right = 0, m-1 while True: pointer1 = left + (right-left) // 2 ...
median-of-two-sorted-arrays
Elegant Python Binary Search | O(log(min(m,n))), O(1)
soma28
10
2,100
median of two sorted arrays
4
0.353
Hard
136
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1067613/Python-O(log(m%2Bn))-with-explanation-and-comments
class Solution: def findMedianSortedArrays(self, nums1, nums2) -> float: # odd length -> e.g. length 5, left(2) and right(2) would be the same index # even length -> e.g. length 6, left(2) and right(3) would be different indices m, n = len(nums1), len(nums2) left, right = (m+n-1)//2,...
median-of-two-sorted-arrays
Python O(log(m+n)) with explanation and comments
killerf1
8
1,400
median of two sorted arrays
4
0.353
Hard
137
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2672475/Python-O(-Log(-Min(m-n)-)-)-Solution-with-full-working-explanation
class Solution: # Time: O(log(min(m, n))) and Space: O(n) def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: A, B = nums1, nums2 total = len(nums1) + len(nums2) half = total // 2 if len(A) > len(B): # for our solution we are assuming that B will be bi...
median-of-two-sorted-arrays
Python O( Log( Min(m, n) ) ) Solution with full working explanation
DanishKhanbx
7
3,300
median of two sorted arrays
4
0.353
Hard
138
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2672475/Python-O(-Log(-Min(m-n)-)-)-Solution-with-full-working-explanation
class Solution: # Time: O(NLogN) and Space: O(1) def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: nums1 = nums1 + nums2 nums1 = sorted(nums1) n = len(nums1) if n % 2 == 0: return (nums1[n//2 - 1] + nums1[(n//2)])/2 else: ...
median-of-two-sorted-arrays
Python O( Log( Min(m, n) ) ) Solution with full working explanation
DanishKhanbx
7
3,300
median of two sorted arrays
4
0.353
Hard
139
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/697669/Python-or-simple-or-BInary-search-or-O(log(min(mn)))
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: nums1_len = len(nums1) nums2_len = len(nums2) if (nums1_len > nums2_len): return self.findMedianSortedArrays(nums2,nums1) low = 0 high = nums1...
median-of-two-sorted-arrays
Python | simple | BInary search | O(log(min(m,n)))
ekantbajaj
7
1,800
median of two sorted arrays
4
0.353
Hard
140
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2799909/Python-or-Easy-Solution
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: final = [] i = 0 j = 0 while i<len(nums1) and j<len(nums2): if nums1[i] <= nums2[j]: final.append(nums1[i]) i += 1 else: ...
median-of-two-sorted-arrays
Python | Easy Solution✔
manayathgeorgejames
6
3,700
median of two sorted arrays
4
0.353
Hard
141
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2032243/Python-Tushar-Roy's-Solution-9463
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: if len(nums1) > len(nums2): return self.findMedianSortedArrays(nums2, nums1) all_len = len(nums1) + len(nums2) left_size = (all_len + 1) // 2 lo, hi = 0, len(nums1) - ...
median-of-two-sorted-arrays
✅ Python, Tushar Roy's Solution, 94,63%
AntonBelski
5
666
median of two sorted arrays
4
0.353
Hard
142
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1874742/Python-O(log(m%2Bn))-Modifed-Binary-Search-w-Comments
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: # Initialize total length of array len(A) + len(B), and rounded down half = total // 2 # We will assume A is smaller array of two(swap A and B if needed) # Binary search array A, find its middle i ...
median-of-two-sorted-arrays
Python O(log(m+n)) Modifed Binary Search w/ Comments
hermit1007
4
621
median of two sorted arrays
4
0.353
Hard
143
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1522910/Python%3A-O(log(m%2Bn))-solution-with-diagram-explanation
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: m,n=len(nums1),len(nums2) k=ceil((m+n)/2) lo=max(0,k-n) hi=min(k,m) def calculateBorder(k,x): m_k=nums1[x] if x<m else math.inf n_k=nums2[k...
median-of-two-sorted-arrays
Python: O(log(m+n)) solution with diagram explanation
SeraphNiu
4
1,200
median of two sorted arrays
4
0.353
Hard
144
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2779958/Python-Simple-Python-Solution
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: num=sorted(nums1+nums2) if len(num)%2==0: return (num[(len(num)//2)-1] + num[(len(num)//2)])/2 else: return (num[(len(num)//2)]*2)/2
median-of-two-sorted-arrays
[ Python ] 🐍🐍 Simple Python Solution ✅✅
sourav638
2
96
median of two sorted arrays
4
0.353
Hard
145
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2444913/median-of-two-sorted-arrays-simple-understandable-python3-solution
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: nums1.extend(nums2) nums1.sort() l = len(nums1) mid1 = l // 2 if l % 2 == 1: return nums1[mid1] else: mid2 = (l // 2) - 1 return (nums1[m...
median-of-two-sorted-arrays
median of two sorted arrays - simple understandable python3 solution
whammie
2
555
median of two sorted arrays
4
0.353
Hard
146
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2361650/Easy-solution-using-Python-or-76ms
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: list1=nums1+nums2 list1.sort() length=len(list1) if length%2==1: return float(list1[((length+1)//2)-1]) else: return (list1[length//2-1]+list1[(length//2)])/...
median-of-two-sorted-arrays
Easy solution using Python | 76ms
Hardik-26
2
240
median of two sorted arrays
4
0.353
Hard
147
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2361650/Easy-solution-using-Python-or-76ms
class Solution(object): def findMedianSortedArrays(self, nums1, nums2): list1=nums1+nums2 list1.sort() length=len(list1) if length%2==1: return float(list1[((length+1)//2)-1]) else: return float(list1[length//2-1]+list1[(length//2)])/2
median-of-two-sorted-arrays
Easy solution using Python | 76ms
Hardik-26
2
240
median of two sorted arrays
4
0.353
Hard
148
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2267528/FASTEST-PYTHON-3-LINE-SOLUTION
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: nums1[0:0]=nums2 nums1.sort() return float(nums1[len(nums1)//2]) if len(nums1)%2==1 else (nums1[len(nums1)//2]+nums1[(len(nums1)//2)-1])/2
median-of-two-sorted-arrays
FASTEST PYTHON 3 LINE SOLUTION
afz123
2
484
median of two sorted arrays
4
0.353
Hard
149
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1970305/Python-optimal-solution
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: #last index n=len(nums2) m=len(nums1) for i in range(n): nums1.append(0) lst=n+m-1 # merging from last index while m>0 and n>0:...
median-of-two-sorted-arrays
Python optimal solution
saty035
2
296
median of two sorted arrays
4
0.353
Hard
150
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1961779/Python-easy-to-read-and-understand
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: m, n = len(nums1), len(nums2) i, j = 0, 0 nums = [] while i < m and j < n: if nums1[i] < nums2[j]: nums.append(nums1[i]) i += 1 ...
median-of-two-sorted-arrays
Python easy to read and understand
sanial2001
2
579
median of two sorted arrays
4
0.353
Hard
151
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1805841/Short-Simple-Solution
class Solution(): def findMedianSortedArrays(self, nums1, nums2): combined = nums1 + nums2 combined.sort() length = len(combined) if length % 2 == 0: length = (length / 2) - 1 median = (combined[int(length)] + combined[int(length + 1)]) / 2 ...
median-of-two-sorted-arrays
Short Simple Solution
blacksquid
2
478
median of two sorted arrays
4
0.353
Hard
152
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/714754/Python-O(log(m%2Bn))-Solution-with-comments
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: if len(nums1) > len(nums2): nums1,nums2 = nums2, nums1 #Shorter array is nums1 n1,n2 = len(nums1),len(nums2) leftHalf = (n1+n2+1)//2 #Now the minimum c...
median-of-two-sorted-arrays
[Python] O(log(m+n)) Solution with comments
spongeb0b
2
512
median of two sorted arrays
4
0.353
Hard
153
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/622231/python3-solution-77ms-beats-99
class Solution: def findMedianSortedArrays(self, A: List[int], B: List[int]) -> float: """ Brute Force --> O(m + n) """ # nums3 = sorted(nums1 + nums2) # print(nums3) # if len(nums3) % 2 == 0: # mid = len(nums3)//2 # mid1 = mid-1 # return (nums3[mi...
median-of-two-sorted-arrays
python3 solution - 77ms - beats 99%
ratikdubey
2
147
median of two sorted arrays
4
0.353
Hard
154
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2501326/Simple-Logical-Solution-in-Python
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: nums = nums1 + nums2 nums.sort() med = len(nums)/2 if med.is_integer(): med = int(med) return (nums[med]+nums[med-1])/2 return nums[int(med)]
median-of-two-sorted-arrays
Simple Logical Solution in Python
Real-Supreme
1
181
median of two sorted arrays
4
0.353
Hard
155
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2459620/Simple-python-solution
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: num3=nums1+nums2 num3.sort() l=len(num3) if l%2==1: return num3[l//2] else: return(num3[l//2]+num3[l//2-1])/2
median-of-two-sorted-arrays
Simple python solution
ilancheran
1
234
median of two sorted arrays
4
0.353
Hard
156
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2418916/Simple-Solutionor-Very-Easy-to-Understand
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: for i in range(len(nums2)): nums1.append(nums2[i]) nums1.sort() # sort the merged array length = len(nums1) half = length//2 if(length%2 != 0): return nums...
median-of-two-sorted-arrays
Simple Solution| Very Easy to Understand
AngelaInfantaJerome
1
121
median of two sorted arrays
4
0.353
Hard
157
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2412098/Python-Short-Easy-and-Faster-Solution
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: arr = sorted(nums1 + nums2) mid = len(arr) // 2 if len(arr) % 2: return arr[mid] return (arr[mid-1] + arr[mid]) / 2.0
median-of-two-sorted-arrays
[Python] Short, Easy and Faster Solution
Buntynara
1
223
median of two sorted arrays
4
0.353
Hard
158
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2371583/Very-Easy-solution-with-python
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: mergedList = nums1 + nums2 mergedList.sort() midIndex = int(len(mergedList)/2) if len(mergedList) % 2 == 0: res = (mergedList[midIndex-1] + mergedLi...
median-of-two-sorted-arrays
Very Easy solution with python
amr-khalil
1
177
median of two sorted arrays
4
0.353
Hard
159
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2356530/Python-simple-one-liner
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: return median(sorted(nums1 + nums2))
median-of-two-sorted-arrays
Python simple one-liner
Arrstad
1
132
median of two sorted arrays
4
0.353
Hard
160
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2336302/Simple-or-Python-or-Beats-96.89
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: nums1 = sorted(nums1 + nums2) l = len(nums1) if l % 2: return nums1[l // 2] else: l //= 2 return (nums1[l] + nums1[l - 1]) / ...
median-of-two-sorted-arrays
Simple | Python | Beats 96.89%
fake_death
1
238
median of two sorted arrays
4
0.353
Hard
161
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2308549/93-faster-or-python-or-O(n-logn)-or-simple-solution
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: nums1 = nums1+ nums2 nums1.sort() n = len(nums1) if n % 2 != 0: mid = nums1[n//2] return mid elif n%2 ==0: mid = n//2 avg =...
median-of-two-sorted-arrays
93% faster | python | O(n logn) | simple solution
TuhinBar
1
99
median of two sorted arrays
4
0.353
Hard
162
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2147954/Python-Solution-oror-short-and-simple-code
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: l=nums1[:]+nums2[:] l.sort() r=0 if len(l)%2 !=0: r=l[len(l)//2] else: r=sum(l[len(l)//2 - 1 : len(l)//2 + 1]) / 2 return r
median-of-two-sorted-arrays
Python Solution || short and simple code
T1n1_B0x1
1
350
median of two sorted arrays
4
0.353
Hard
163
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1810446/Simple-Python3-Sol-O(m%2Bn)
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: len1 = len(nums1) len2 = len(nums2) n = len1+len2 i1,i2 = 0,0 d = [] while i2 < len2 and i1 < len1: if nums2[i2] > nums1[i1]: d.append(nums1[i1])...
median-of-two-sorted-arrays
Simple Python3 Sol O(m+n)
shivamm0296
1
318
median of two sorted arrays
4
0.353
Hard
164
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1749839/Python3-or-Simple-or-O(n%2Bm)-or-two-pointers
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: ptr1 = 0 # pointer for nums1 ptr2 = 0 # pointer for nums2 ret = [] # list storing elements ascendingly until median while (ptr1+ptr2 < (len(nums1)+len(nums2)+1)/2): ...
median-of-two-sorted-arrays
Python3 | Simple | O(n+m) | two-pointers
Onlycst
1
634
median of two sorted arrays
4
0.353
Hard
165
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1598799/Self-explanatory-solution-with-almost-constant-space
class Solution: def findKthElement(self, nums1: List[int], nums2: List[int], k: int) -> float: if len(nums1) > len(nums2): nums1, nums2 = nums2, nums1 lowNumSelected = 0 # minimum number of elements can be selected from shorter array highNumSelected = len(nums1) if k > len(nums1)...
median-of-two-sorted-arrays
Self-explanatory solution with almost constant space
vudat1710
1
270
median of two sorted arrays
4
0.353
Hard
166
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1581195/Short-and-readable-python3-84ms-solution
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: i=0 j=0 m2=0 l=len(nums1)+len(nums2) nums1.append(1000001) nums2.append(1000001) for _i in range(l//2+1): if nums1[i]<nums2[j]: m1,m2=m2...
median-of-two-sorted-arrays
Short and readable python3 84ms solution
Veanules
1
467
median of two sorted arrays
4
0.353
Hard
167
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1580354/Two-two-two-pointers-as-if-they-were-merged
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: n1 = len(nums1) n2 = len(nums2) n = n1 + n2 p1 = 0 p2 = n - 1 a1 = 0 a2 = n1 - 1 b1 = 0 b2 = n2 -1 while p2 - p1 ...
median-of-two-sorted-arrays
Two-two-two pointers as if they were merged
hl2425
1
186
median of two sorted arrays
4
0.353
Hard
168
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1530105/Python3-solution-in-time-O(n%2Bm)
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: m,n = len(nums1),len(nums2) if m+n<1: return -1 merge = [] i,j = 0,0 while i<len(nums1) and j<len(nums2): if nums1[i]<=nu...
median-of-two-sorted-arrays
Python3 solution in time O(n+m)
Abhists
1
300
median of two sorted arrays
4
0.353
Hard
169
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/1530105/Python3-solution-in-time-O(n%2Bm)
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: m,n = len(nums1),len(nums2) if m+n<1: return -1 merge = [] merge = sorted(nums1 + nums2) a = len(merge) if a==1: ...
median-of-two-sorted-arrays
Python3 solution in time O(n+m)
Abhists
1
300
median of two sorted arrays
4
0.353
Hard
170
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2848110/python-solution
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: complete = nums1 + nums2 complete.sort() if len(complete)%2 ==0: # return sum(complete)/len(complete) return (complete[int((len(complete) - 1)/2)] + complete[int((len(comple...
median-of-two-sorted-arrays
python solution
HARMEETSINGH0
0
1
median of two sorted arrays
4
0.353
Hard
171
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2841662/Python-oror-Easily-Understandable
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: for j in nums2: nums1.append(j) nums1.sort() if(len(nums1)%2==0): mid = len(nums1)//2 median = (nums1[mid-1] + nums1[mid])/2 else: ...
median-of-two-sorted-arrays
Python || Easily Understandable
user6374IX
0
4
median of two sorted arrays
4
0.353
Hard
172
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2838883/it-works
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: nums1.extend(nums2) n=len(nums1) nums1.sort() if n%2==0 : m=len(nums1)//2 return ((nums1[m-1]+nums1[m])/2) else: m=(len(nums1)//2)+1 re...
median-of-two-sorted-arrays
it works
venkatesh402
0
1
median of two sorted arrays
4
0.353
Hard
173
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2838698/Python-Straight-Forward-Solution
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: ptr1, ptr2 = 0, 0 res = [] while ptr1 < len(nums1) and ptr2 < len(nums2): if nums1[ptr1] < nums2[ptr2]: res.append(nums1[ptr1]) ptr1 += 1 els...
median-of-two-sorted-arrays
Python Straight Forward Solution
paul1202
0
3
median of two sorted arrays
4
0.353
Hard
174
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2838657/Python3-Fast-93ms-solution
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: num = sorted(nums1 + nums2) if len(num) % 2 != 0: return float("%.5f" % num[len(num) // 2]) return float("%.5f" % (num[len(num) // 2] + num[len(num) // 2 - 1])) / 2
median-of-two-sorted-arrays
Python3 Fast 93ms solution
Ki4EH
0
1
median of two sorted arrays
4
0.353
Hard
175
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2838406/2-Python-easy-Solution-with-video-explanation
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: if (len(nums1) > len(nums2)): return self.findMedianSortedArrays(nums2, nums1) start = 0 end = len(nums1) X = len(nums1) Y = len(nums2) while start <= end: ...
median-of-two-sorted-arrays
2 Python easy Solution ✔️ with video explanation
CoderrrMan
0
7
median of two sorted arrays
4
0.353
Hard
176
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2832807/Easy-Python-Solution
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: merged_list = self.merge_sort(nums1, nums2) len_merged_list = len(merged_list) # middle index and middle - 1 index mid = len_merged_list // 2 # when the size is even if le...
median-of-two-sorted-arrays
Easy Python Solution
namashin
0
4
median of two sorted arrays
4
0.353
Hard
177
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2831276/Python3-Solution
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: nums1.extend(nums2) nums = sorted(nums1) # nums = list(set(nums)) m = len(nums) if m%2==0 and m>1: i = m//2 med = (nums[i-1]+nums[i])/2 elif m%2==1 a...
median-of-two-sorted-arrays
Python3 Solution
sina1
0
2
median of two sorted arrays
4
0.353
Hard
178
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2821932/Maybe-I-am-misunderstanding-why-this-problem-is-'hard'-The-solution-seems-pretty-simple....
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: a = nums1 + nums2 a.sort() b = int(len(a)/2) if len(a) % 2 == 1: return a[b] return (a[b] + a[b-1]) / 2
median-of-two-sorted-arrays
Maybe I am misunderstanding why this problem is 'hard'? The solution seems pretty simple....
walter9388
0
18
median of two sorted arrays
4
0.353
Hard
179
https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2816882/dont-know-bro-its-just-simple
class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: sum_list = nums1 + nums2 sum_list.sort() sum_size = len(sum_list) if sum_size % 2 == 0: x = (sum_list[int(sum_size/2)] + sum_list[int((sum_size/2) - 1)]) / 2 else: ...
median-of-two-sorted-arrays
dont know bro its just simple
musaidovs883
0
13
median of two sorted arrays
4
0.353
Hard
180
https://leetcode.com/problems/longest-palindromic-substring/discuss/2156659/Python-Easy-O(1)-Space-approach
class Solution: def longestPalindrome(self, s: str) -> str: n=len(s) def expand_pallindrome(i,j): while 0<=i<=j<n and s[i]==s[j]: i-=1 j+=1 return (i+1, j) res=(0,0) for i in rang...
longest-palindromic-substring
✅ Python Easy O(1) Space approach
constantine786
47
6,600
longest palindromic substring
5
0.324
Medium
181
https://leetcode.com/problems/longest-palindromic-substring/discuss/2156659/Python-Easy-O(1)-Space-approach
class Solution: def longestPalindrome(self, s: str) -> str: n=len(s) def expand_center(i,j): while 0<=i<=j<n and s[i]==s[j]: i-=1 j+=1 return (i+1, j) res=max([expand_cen...
longest-palindromic-substring
✅ Python Easy O(1) Space approach
constantine786
47
6,600
longest palindromic substring
5
0.324
Medium
182
https://leetcode.com/problems/longest-palindromic-substring/discuss/1057629/Python.-Super-simple-and-easy-understanding-solution.-O(n2).
class Solution: def longestPalindrome(self, s: str) -> str: res = "" length = len(s) def helper(left: int, right: int): while left >= 0 and right < length and s[left] == s[right]: left -= 1 right += 1 return s[left + 1 : right] for index in range(len(s)): res = max(helper(index, ind...
longest-palindromic-substring
Python. Super simple & easy-understanding solution. O(n^2).
m-d-f
18
2,300
longest palindromic substring
5
0.324
Medium
183
https://leetcode.com/problems/longest-palindromic-substring/discuss/2157014/Python-2-Approaches-with-Explanation
class Solution: def longestPalindrome(self, s: str) -> str: """ Consider each character in s as the centre of a palindrome. Check for the longest possible odd-length and even-length palindrome; store the longest palindrome """ # res is the starting index of the longest palind...
longest-palindromic-substring
[Python] 2 Approaches with Explanation
zayne-siew
11
905
longest palindromic substring
5
0.324
Medium
184
https://leetcode.com/problems/longest-palindromic-substring/discuss/2157014/Python-2-Approaches-with-Explanation
class Solution: def longestPalindrome(self, s: str) -> str: """ Manacher's Algorithm for longest palindromic substrings (LPS) """ # Transform S into T # For example, S = "abba", T = "^#a#b#b#a#$" # ^ and $ signs are sentinels appended to each end to avoid bounds check...
longest-palindromic-substring
[Python] 2 Approaches with Explanation
zayne-siew
11
905
longest palindromic substring
5
0.324
Medium
185
https://leetcode.com/problems/longest-palindromic-substring/discuss/2156653/PYTHON-oror-DYNAMIC-AND-BRUTE-oror
class Solution: def longestPalindrome(self, s: str) -> str: res=s[0] nn=len(res) n=len(s) for i in range(1,n-1): kk=s[i] z=1 while ((i-z)>=0) and ((i+z)<n) and (s[i-z]==s[i+z]): kk=s[i-z]+kk+s[i-z] z...
longest-palindromic-substring
✔️ PYTHON || DYNAMIC AND BRUTE || ;]
karan_8082
10
734
longest palindromic substring
5
0.324
Medium
186
https://leetcode.com/problems/longest-palindromic-substring/discuss/2156653/PYTHON-oror-DYNAMIC-AND-BRUTE-oror
class Solution: def longestPalindrome(self, s): res = '' dp = [[0]*len(s) for i in range(len(s))] for i in range(len(s)): dp[i][i] = True res = s[i] for i in range(len(s)-1,-1,-1): for j in range(i+1,len(s)): ...
longest-palindromic-substring
✔️ PYTHON || DYNAMIC AND BRUTE || ;]
karan_8082
10
734
longest palindromic substring
5
0.324
Medium
187
https://leetcode.com/problems/longest-palindromic-substring/discuss/368409/Solution-in-Python-3-(-six-lines-)-(-O(n2)-speed-)-(-O(1)-space-)-(With-Explanation)-(beats-~90)
class Solution: def longestPalindrome(self, s: str) -> str: L, M, x = len(s), 0, 0 for i in range(L): for a,b in [(i,i),(i,i+1)]: while a >= 0 and b < L and s[a] == s[b]: a -= 1; b += 1 if b - a - 1 > M: M, x = b - a - 1, a + 1 return s[x:x+M] - Junaid Mansuri (LeetCode ID)@ho...
longest-palindromic-substring
Solution in Python 3 ( six lines ) ( O(n^2) speed ) ( O(1) space ) (With Explanation) (beats ~90%)
junaidmansuri
8
1,600
longest palindromic substring
5
0.324
Medium
188
https://leetcode.com/problems/longest-palindromic-substring/discuss/2718979/Python-Solution-readable-code-with-thorough-explanation.
class Solution: def longestPalindrome(self, s: str) -> str: n = len(s) # Length of the input if n == 1: return s def expandOutwards(start, end): i = start k = end if s[i] != s[k]: return "" while(i-1 >=...
longest-palindromic-substring
Python Solution, readable code with thorough explanation.
BrandonTrastoy
6
1,300
longest palindromic substring
5
0.324
Medium
189
https://leetcode.com/problems/longest-palindromic-substring/discuss/2164588/Python3-solution-with-two-pointers
class Solution: indices = [] maxLength = -float("inf") def isPalindrome(self,low,high,s): while low>=0 and high<len(s) and s[low]==s[high]: low-=1 high+=1 if self.maxLength<high-low+1: self.maxLength = high-low+1 self.in...
longest-palindromic-substring
📌 Python3 solution with two pointers
Dark_wolf_jss
5
112
longest palindromic substring
5
0.324
Medium
190
https://leetcode.com/problems/longest-palindromic-substring/discuss/1767684/Clean-Python-Solution
class Solution: def longestPalindrome(self, s: str) -> str: n, max_len, ans = len(s), 0, '' def expand_around_center(l, r): if r < n and s[l] != s[r]: return '' # Index out of range or elements unequal(for even sized palindromes) thus return empty string while...
longest-palindromic-substring
Clean Python Solution
anCoderr
4
760
longest palindromic substring
5
0.324
Medium
191
https://leetcode.com/problems/longest-palindromic-substring/discuss/2601218/Simple-python-code-with-explanation
class Solution: def longestPalindrome(self, s: str) -> str: #create an empty string res res = "" #store it's length as 0 in resLen resLen = 0 #iterate using indexes in the string for i in range(len(s)): ...
longest-palindromic-substring
Simple python code with explanation
thomanani
3
532
longest palindromic substring
5
0.324
Medium
192
https://leetcode.com/problems/longest-palindromic-substring/discuss/2156940/Longest-Palindromic-Substring
class Solution: def longestPalindrome(self, s: str) -> str: long_s = '' n = len(s) for i in range(n) : left = i right = i while left >= 0 and right < n and s[left] == s[right] : # print("left :...
longest-palindromic-substring
Longest Palindromic Substring
vaibhav0077
3
312
longest palindromic substring
5
0.324
Medium
193
https://leetcode.com/problems/longest-palindromic-substring/discuss/1504461/Python3-Beats-95.79
class Solution: def longestPalindrome(self, s: str) -> str: longest = "" core_start = 0 while core_start in range(len(s)): # identifying the "core" core_end = core_start while core_end < len(s) - 1: if s[core_end + 1] == s[core_end]: ...
longest-palindromic-substring
[Python3] Beats 95.79%
Wallace_W
3
1,700
longest palindromic substring
5
0.324
Medium
194
https://leetcode.com/problems/longest-palindromic-substring/discuss/2337365/EFFICIENT-APPROCH-using-python3
class Solution: def longestPalindrome(self, s: str) -> str: def func(i,j): while(i>=0 and j<len(s) and s[i]==s[j]): i-=1 j+=1 return(s[i+1:j]) r="" for i in range(len(s)): test=func(i,i) if len(r)<len(test): ...
longest-palindromic-substring
EFFICIENT APPROCH using python3
V_Bhavani_Prasad
2
88
longest palindromic substring
5
0.324
Medium
195
https://leetcode.com/problems/longest-palindromic-substring/discuss/2311861/Easy-Python3-Two-Pointer-Solution-w-Explanation
class Solution: def longestPalindrome(self, s: str) -> str: res = '' for i in range(len(s)*2-1): # i refers to the "central" (or "mid point" or "pivot") of the palindrome # Uses index 0 as central, uses the middle of index 0 and 1 as central and so on # idx 0 1 2 3 4 ... ...
longest-palindromic-substring
Easy Python3 Two Pointer Solution w/ Explanation
geom1try
2
204
longest palindromic substring
5
0.324
Medium
196
https://leetcode.com/problems/longest-palindromic-substring/discuss/1993958/Python3-Runtime%3A-897ms-81.93-memory%3A-13.9mb-99.78
class Solution: # O(n^2) || space O(n) def longestPalindrome(self, string: str) -> str: if not string or len(string) < 1: return '' start, end = 0, 0 for i in range(len(string)): odd = self.expandFromMiddleHelper(string, i, i) even = self.exp...
longest-palindromic-substring
Python3 Runtime: 897ms 81.93% memory: 13.9mb 99.78%
arshergon
2
361
longest palindromic substring
5
0.324
Medium
197
https://leetcode.com/problems/longest-palindromic-substring/discuss/1993958/Python3-Runtime%3A-897ms-81.93-memory%3A-13.9mb-99.78
class Solution: # O(n^2) || space O(n) def longestPalindrome(self, string: str) -> str: result = "" for i in range(len(string)): left, right = i, i while left >= 0 and right < len(string) and string[left] == string[right]: ...
longest-palindromic-substring
Python3 Runtime: 897ms 81.93% memory: 13.9mb 99.78%
arshergon
2
361
longest palindromic substring
5
0.324
Medium
198
https://leetcode.com/problems/longest-palindromic-substring/discuss/2614056/Clean-python-solution-expand-from-the-center
class Solution: def longestPalindrome(self, s: str) -> str: res = "" def helper(left, right): while left >= 0 and right < len(s) and s[left] == s[right]: left -= 1 right += 1 return s[left+1 : right] for i in range(len(s)): res = max(helper(i, i), helper(i, i+1), res, key=len) return res
longest-palindromic-substring
Clean python solution expand from the center
miko_ab
1
180
longest palindromic substring
5
0.324
Medium
199