917-Reverse-Only-Letters
https://leetcode.com/problems/reverse-only-letters
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reverseOnlyLetters(self, S: str) -> str:
S = list(S)
i = 0
j = len(S) - 1
while i < j:
while i < j and not S[i].isalpha():
i += 1
while …
Read More
918-Maximum-Sum-Circular-Subarray
https://leetcode.com/problems/maximum-sum-circular-subarray
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxSubarraySumCircular(self, A: List[int]) -> int:
totalSum = 0
currMaxSum = 0
currMinSum = 0
maxSum = -math.inf
minSum = math.inf
for a in A:
totalSum += a
currMaxSum = max(currMaxSum + a …
Read More
92-Reverse-Linked-List-Ii
https://leetcode.com/problems/reverse-linked-list-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
if left == 1:
return self.reverseN(head, right)
head.next = self.reverseBetween(head.next, left - 1, right …
Read More
921-Minimum-Add-To-Make-Parentheses-Valid
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minAddToMakeValid(self, s: str) -> int:
l = 0
r = 0
for c in s:
if c == '(':
l += 1
else:
if l == 0:
r += 1
else:
l -= 1
return l …
Read More
922-Sort-Array-By-Parity-Ii
https://leetcode.com/problems/sort-array-by-parity-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sortArrayByParityII(self, A: List[int]) -> List[int]:
n = len(A)
i = 0
j = 1
while i < n:
while i < n and A[i] % 2 == 0:
i += 2
while …
Read More
925-Long-Pressed-Name
https://leetcode.com/problems/long-pressed-name
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isLongPressedName(self, name: str, typed: str) -> bool:
i = 0
for j, t in enumerate(typed):
if i < len(name) and name[i] == t:
i += 1
elif j == 0 …
Read More
926-Flip-String-To-Monotone-Increasing
https://leetcode.com/problems/flip-string-to-monotone-increasing
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minFlipsMonoIncr(self, S: str) -> int:
dp = [0] * 2
for i, c in enumerate(S):
dp[0], dp[1] = dp[0] + (c == '1'), min(dp[0], dp[1]) + (c …
Read More
927-Three-Equal-Parts
https://leetcode.com/problems/three-equal-parts
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def threeEqualParts(self, A: List[int]) -> List[int]:
ones = sum(a == 1 for a in A)
if ones == 0:
return [0, len(A) - 1]
if ones % 3 != 0:
return …
Read More
929-Unique-Email-Addresses
https://leetcode.com/problems/unique-email-addresses
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numUniqueEmails(self, emails: List[str]) -> int:
seen = set()
for email in emails:
local, domain = email.split('@')
local = local.split('+')[0].replace('.', '')
seen.add(local + '@' + domain)
return len(seen …
Read More
93-Restore-Ip-Addresses
https://leetcode.com/problems/restore-ip-addresses
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def restoreIpAddresses(self, s: str) -> List[str]:
ans = []
def dfs(start: int, path: List[int]) -> None:
if len(path) == 4 and start == len(s):
ans.append(path[0] + '.' + path …
Read More