My issue
im trying to find each occurrences of character . I m taking maximum occurrence if it has unity then im taking 0 and if not then the remaining scattered its values im adding it up as n - high_occurred value. why my approach is wrong
My code
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;cin>>t;
while(t--){
map<char,int>m;
string s; cin>>s;
vector<int>v;
int c = 1,maxc = 1;
for(int i=0;i<s.size();i++){
if (s[i]==s[i+1]){
c++;
}else{
m[s[i]] = max(m[s[i]],c);
c =1;
}
}for(auto [a,b]:m){
if (count(s.begin(),s.end(),a)==b) {
v.push_back(0);
}else{
v.push_back(count(s.begin(),s.end(),a) - b);
}
}
cout<<*min_element(v.begin(),v.end())<<endl;
}
}
Problem Link: Swap and Unite Practice Coding Problem - CodeChef