Jbears

Untitled

Apr 29th, 2022
1,466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. class Solution:
  2.     def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int:
  3.         '''
  4.        a valid k is one where
  5.        after removing k caracters from s using the first k indexes in removable
  6.        you get a string which is still a subseq of s
  7.        https://leetcode.com/problems/maximum-number-of-removable-characters/
  8.        '''
  9.        
  10.         def isSubseq(p,s):
  11.            
  12.             i=0
  13.             j=0
  14.             while i < len(p) and j < len(s):
  15.                 if p[i]!=s[j]:
  16.                     j+=1
  17.                 else:
  18.                     # if they match
  19.                     i+=1
  20.                     j+=1
  21.             return i==len(p)
  22.        
  23.         def isValid(k,s,p):
  24.             aset=set(removable[:k])
  25.             sfiltered=''
  26.             for i,c in enumerate(s):
  27.                 if i not in aset:
  28.                     sfiltered+=c
  29.             return isSubseq(p,sfiltered)
  30.        
  31.         start=0
  32.         end=len(removable)
  33.         # 3+0 //2 = 1
  34.  
  35.         while start < end:
  36.             # print(start,end)
  37.             if end-start==1:
  38.                 if isValid(end,s,p):
  39.                     return end
  40.                 if isValid(start,s,p):
  41.                     return start
  42.                 break
  43.             mid=(start+end)//2
  44.            
  45.             if isValid(mid,s,p):
  46.                 start=mid
  47.             else:
  48.                 end=mid-1
  49.        
  50.        
  51.         return end
  52.        
  53.        
  54.        
  55.                    
  56.                    
  57.                
  58.                
  59.                
  60.                
  61.                
  62.        
  63.                
  64.            
  65.            
  66.                    
  67.                    
  68.                    
  69.            
  70.            
  71.        
  72.        
  73.        
Advertisement
Add Comment
Please, Sign In to add comment