My issue
include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
int n;
cin>>n;
string s;
cin>>s;
string check=“”;
for(int i=1;i<n-1;i++){
if(s[i]==s[i+1]|| s[i]==s[i-1]){
if(i-1==0){
check+=s[0];
}
else if(i+1==n-1){
check+=s[n-1];
}
check+=s[i];
}
}
vectorcount(26);
int m=check.size();
for(int i=0;i<m;i++){
count[check[i]-‘A’]++;
}
int result=0;
for(int i=0;i<26;i++){
if(count[i]%2==0){
result+=(count[i]/2);
}
else{
result+=((count[i]+1)/2);
}
}
cout<<result+(n-m)<<endl;
}
return 0;
}
help me to solve this problem
My code
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s;
cin>>s;
string check="";
for(int i=1;i<n-1;i++){
if(s[i]==s[i+1]|| s[i]==s[i-1]){
if(i-1==0){
check+=s[0];
}
else if(i+1==n-1){
check+=s[n-1];
}
check+=s[i];
}
}
vector<int>count(26);
int m=check.size();
for(int i=0;i<m;i++){
count[check[i]-'A']++;
}
int result=0;
for(int i=0;i<26;i++){
if(count[i]%2==0){
result+=(count[i]/2);
}
else{
result+=((count[i]+1)/2);
}
}
cout<<result+(n-m)<<endl;
}
return 0;
}
Problem Link: Rock Paper Scissors Practice Coding Problem
strong text