Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- unordered_map<char,int> f;
- vector<pair<int,char>> need;
- bool check(int wind, string s){
- unordered_map<char,int> d;
- queue<char> q;
- for(char c : s){
- if(q.size()==wind){
- d[q.front()]--;
- q.pop();
- }
- d[c]++;
- q.push(c);
- //checar aqui
- int n = s.size();
- bool chec = true;
- for(auto p : need){
- chec &= d[p.second] >= p.first;
- }
- if(chec) return true;
- }
- return false;
- }
- int balancedString(string &s) {
- f['Q']=f['W']=f['E']=f['R'];
- for(char c : s)
- f[c]++;
- if(f['Q']==f['W'] and f['Q']==f['W'] and f['Q']==f['R']) return 0;
- int n = s.size();
- for(auto p : f)
- need.push_back({p.second-n/4,p.first});
- int l=0,r=s.size();
- for(auto p : need) if(p.first>0)l+=p.first;
- while(l<r){
- int mid = (l+r)/2;
- if(check(mid, s)) r=mid;
- else l = mid+1;
- }
- return l;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement