DEV Community

Carnato
Carnato

Posted on

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

Discussion (0)