cpp

Rearrange a string

carnato

Vishal Yadav

Posted on January 23, 2022

Rearrange a string

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;
}
Enter fullscreen mode Exit fullscreen mode
πŸ’– πŸ’ͺ πŸ™… 🚩
carnato
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.

Related