72-Edit-Distance
https://leetcode.com/problems/edit-distance
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m = len(word1)
n = len(word2)
# dp[i][j] := min # Of operations to convert word1[0..i) to word2[0..j …
Read More
720-Longest-Word-In-Dictionary
https://leetcode.com/problems/longest-word-in-dictionary
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def longestWord(self, words: List[str]) -> str:
root = {}
for word in words:
node = root
for c in word:
if c not in node:
node[c] = {}
node = node[c]
node …
Read More
722-Remove-Comments
https://leetcode.com/problems/remove-comments
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def removeComments(self, source: List[str]) -> List[str]:
ans = []
commenting = False
modified = ''
for line in source:
i = 0
while i < len(line):
if i + 1 == len(line):
if not …
Read More
724-Find-Pivot-Index
https://leetcode.com/problems/find-pivot-index
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def pivotIndex(self, nums: List[int]) -> int:
summ = sum(nums)
prefix = 0
for i, num in enumerate(nums):
if prefix == summ - prefix - num:
return i
prefix += num
return -1 …
Read More
725-Split-Linked-List-In-Parts
https://leetcode.com/problems/split-linked-list-in-parts
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def splitListToParts(self, root: ListNode, k: int) -> List[ListNode]:
ans = [[] for _ in range(k)]
length = 0
curr = root
while curr:
length += 1
curr = curr.next
subLength = length // k …
Read More
726-Number-Of-Atoms
https://leetcode.com/problems/number-of-atoms
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countOfAtoms(self, formula: str) -> str:
def parse() -> dict:
ans = defaultdict(int)
nonlocal i
while i < n:
if formula[i] == '(':
i += 1
for elem, freq in parse().items():
ans …
Read More
728-Self-Dividing-Numbers
https://leetcode.com/problems/self-dividing-numbers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def selfDividingNumbers(self, left: int, right: int) -> List[int]:
return [num for num in range(left, right + 1) if all(n != 0 and num % n == 0 for n in …
Read More
73-Set-Matrix-Zeroes
https://leetcode.com/problems/set-matrix-zeroes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
m = len(matrix)
n = len(matrix[0])
shouldFillFirstRow = 0 in matrix[0]
shouldFillFirstCol = 0 in list(zip(*matrix))[0]
# Store the …
Read More
730-Count-Different-Palindromic-Subsequences
https://leetcode.com/problems/count-different-palindromic-subsequences
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countPalindromicSubsequences(self, s: str) -> int:
def count(l: int, r: int) -> int:
if l > r:
return 0
if l == r:
return 1
key = l * len(s) + r
if …
Read More
733-Flood-Fill
https://leetcode.com/problems/flood-fill
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def floodFill(self, image: List[List[int]],
sr: int, sc: int, newColor: int) -> List[List[int]]:
startColor = image[sr][sc]
seen = set()
def dfs(i: int, j: int) -> None …
Read More