leetcode1208_2-5每日题:尽可能使字符串相等

class Solution {
    
    
public:
    int equalSubstring(string s, string t, int maxCost) {
    
    
        int s_n = s.length();
        if(!s_n) return 0;
        queue<pair<int,int>> q;
        int now = 0,answer = 0;
        for(int i=0;i<s_n;i++){
    
    
            q.emplace(i,abs(t[i]-s[i]));
            now += abs(t[i]-s[i]);
            if(now<=maxCost) answer = q.size()>answer?q.size():answer;
            else {
    
    
                while(now>maxCost){
    
    
                    auto [x,y] = q.front();
                    now -= y;
                    q.pop();
                }
            }
        }
        return answer;
    }
};

放入队列顺序判断字符串即可。

猜你喜欢

转载自blog.csdn.net/SJTU_liangge/article/details/113705084