209. Minimum Size Subarray Sum
def minSubArrayLen(self, s: int, nums: List[int]) -> int:
minSize = len(nums)+1
currSum = 0
startWindow = 0
for endWindow in range(len(nums)):
currSum += nums[endWindow]
while currSum>=s:
minSize = min(minSize,endWindow-startWindow+1)
currSum -= nums[startWindow]
startWindow+=1
return minSize%(len(nums)+1)
3. Longest Substring Without Repeating Characters
def lengthOfLongestSubstring(self, s: str) -> int:
startWindow = 0
d = collections.Counter()
maxSize = 0
for endWindow,val in enumerate(s):
d[val] += 1
while d[val] > 1:
d[s[startWindow]] -= 1
startWindow += 1
maxSize = max(maxSize,endWindow-startWindow+1)
return maxSize424. Longest Repeating Character Replacement
def characterReplacement(self, s: str, k: int) -> int:
startWindow = 0
maxSize = 0
maxChar = 0
d = collections.Counter()
for endWindow in range(len(s)):
d[s[endWindow]]+=1
maxChar = max(maxChar,d[s[endWindow]])
if endWindow-startWindow+1-maxChar > k:
d[s[startWindow]]-=1
startWindow+=1
maxSize = max(maxSize,endWindow-startWindow+1)
return maxSizedef totalFruit(self, tree: List[int]) -> int:
maxSize = 0
startWindow = 0
d = collections.Counter()
for endWindow,val in enumerate(tree):
d[val] += 1
while len(d)>2:
d[tree[startWindow]]-=1
if d[tree[startWindow]]==0:
d.pop(tree[startWindow])
startWindow+=1
maxSize = max(maxSize,endWindow-startWindow+1)
return maxSize1004. Max Consecutive Ones III
def longestOnes(self, A: List[int], K: int) -> int:
startWindow = 0
count = 0
maxSize = 0
for endWindow in range(len(A)):
if A[endWindow] == 0: count += 1
if count > K:
if A[startWindow] == 0: count -= 1
startWindow += 1
maxSize = max(maxSize,endWindow-startWindow+1)
return maxSize1208. Get Equal Substrings Within Budget
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
startWindow = 0
currCost = 0
for endWindow in range(len(s)):
currCost += abs(ord(s[endWindow])-ord(t[endWindow]))
if currCost>maxCost:
currCost -= abs(ord(s[startWindow])-ord(t[startWindow]))
startWindow+=1
return endWindow-startWindow+11358. Number of Substrings Containing All Three Characters
def numberOfSubstrings(self, s: str) -> int:
startWindow = 0
count = 0
d = {'a':0,'b':0,'c':0}
for endWindow in range(len(s)):
d[s[endWindow]]+=1
while all(d.values()):
count += len(s)-endWindow
d[s[startWindow]] -= 1
startWindow += 1
return count