nikunjsoni

1208

Apr 17th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int equalSubstring(string s, string t, int maxCost) {
  4.         int ans = 0, left, right;
  5.        
  6.         for(left=0, right=0; right<s.length(); right++){
  7.             maxCost -= abs(s[right]-t[right]);
  8.             while(maxCost < 0) {
  9.                 maxCost += abs(s[left]-t[left]);
  10.                 left++;
  11.             }
  12.             ans = max(ans, right-left+1);
  13.         }
  14.         return ans;  
  15.     }
  16. };
Advertisement
Add Comment
Please, Sign In to add comment