Rearrange a string (GFG)

 Rearrange a string 

Given a string containing uppercase alphabets and integer digits (from 0 to 9), the task is to print the alphabets in the order followed by the sum of digits.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains a string with uppercase alphabets and integer digits.

Output:
Print alphabets in the order followed by the sum of digits.

Constraints:
1<=T<=10^5
1<=length of string<=10^5

Example:
Input:

2
AC2BEW3
ACCBA10D2EW30

Output:
ABCEW5
AABCCDEW6


SOLUTION:

t=int(input())

for i in range(t):

    s=input()

    val=0

    for i in s:

        if i.isdigit():

            s=s.replace(i,"")

            val=val+int(i)

    res=sorted(s)

    res+=str(val)

    for i in res:

        print(i,end="")

    print()

    

Comments

Popular posts from this blog

Sort in specific order (GFG)

Chef and Remissness Problem Code: REMISS (CodeChef)

Short Notes Of computer Network