1190. Reverse Substrings Between Each Pair of Parentheses

class Solution: def reverseParentheses(self, s: str) -> str: s_stack=[] l,r=0,0 while r<len(s): if s[r]=='(‘: if r!=l: s_stack.append(s[l:r]) s_stack.append(s[r]) l=r+1 elif s[r]==’)’: cur=s[l:r] ss=” while ss!='(‘: ss=s_stack.pop() if ss!='(‘: cur=ss+cur else: cur=cur[::-1] s_stack.append(cur) l=r+1 r+=1 if s[-1]!=’)’: s_stack.append(s[l:r]) rep=”” for str1 in s_stack: rep+=str1 return rep ”’ def ss: if not s: return s l,r=0,len(s)-1 whileContinueContinue reading “1190. Reverse Substrings Between Each Pair of Parentheses”

1208. Get Equal Substrings Within Budget

class Solution: def equalSubstring(self, s: str, t: str, maxCost: int) -> int: rep=[] for k in range(len(s)): gap=self.getGapOfStr(s[k],t[k]) rep.append(gap) res=0 print(rep) l=0 total=0 r=0 while l<len(rep): if r<len(rep) and total+rep[r]<=maxCost: while r<len(rep) and total+rep[r]<=maxCost: total+=rep[r] r+=1 res=max(res,r-l) total-=rep[l] l+=1 return res def getGapOfStr(self,str1,str2): strs=’abcdefghijklmnopqrstuvwxyz’ dict_s={} for k in range(len(strs)): dict_s[strs[k]]=k return abs(dict_s[str1]-dict_s[str2])

1220. Count Vowels Permutation

class Solution: def countVowelPermutation(self, n: int) -> int: ”’ Each vowel ‘a’ may only be followed by an ‘e’. Each vowel ‘e’ may only be followed by an ‘a’ or an ‘i’. Each vowel ‘i’ may not be followed by another ‘i’. Each vowel ‘o’ may only be followed by an ‘i’ or a ‘u’.ContinueContinue reading “1220. Count Vowels Permutation”

1219. Path with Maximum Gold

class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: rep=0 for row in range(len(grid)): for col in range(len(grid[0])): if grid[row][col]!=0: rep=max(rep,self.getrcMaximumGold(row,col,grid)) return rep def getrcMaximumGold(self,row,col,grid): if grid[row][col]==0: return 0 if grid[row][col]==’X’: return 0 rep=grid[row][col] grid[row][col]=’X’ cur=0 if row-1>=0: cur=max(self.getrcMaximumGold(row-1,col,grid),cur) if col-1>=0: cur=max(self.getrcMaximumGold(row,col-1,grid),cur) if row+1<len(grid): cur=max(self.getrcMaximumGold(row+1,col,grid),cur) if col+1<len(grid[0]): cur=max(self.getrcMaximumGold(row,col+1,grid),cur) grid[row][col]=rep return rep+cur

1218. Longest Arithmetic Subsequence of Given Difference

1218. Longest Arithmetic Subsequence of Given Difference Difficulty: 中等 Given an integer array arr and an integer <font face=”monospace” style=”display: inline;”>difference</font>, return the length of the longest subsequence in arr which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals difference. Example 1: Input: arr = [1,2,3,4], difference = 1 Output: 4ContinueContinue reading “1218. Longest Arithmetic Subsequence of Given Difference”