Rearrange a string
Vishal Yadav
Posted on January 23, 2022
Solution
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str="ACCBA10D2EW30";
int sum=0;
vector<int>f(26,0);
for(int i=0;i<str.size();i++)
{
if(str[i]>='0'&&str[i]<='9')
sum+=str[i]-'0';
else
f[str[i]-'A']++;
}
//cout<<str[3]-'A';
string ans;
for(int i=0;i<26;i++)
{
while(f[i]>0)
{
ans+=i+'A';
f[i]--;
}
}
ans+=to_string(sum);
cout<<ans;
return 0;
}
π πͺ π
π©
Vishal Yadav
Posted on January 23, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.