Got this question in online test
Anonymous User
199

Operations on characters You are given a string S of size N. Assume that count is equal to 0. Your task is the remove all the N elements of string S by performing the following operation N times:
• In a single operation, select an alphabetically smallest character in S, for example, 'c'. Remove c from S and add its index to count. If multiple characters such as c exist, then select c that has the smallest index. Print the value of count.

Read sample examples for better understanding.
Note: Consider 1-based indexing.
Solve the problem forT test cases.

Input format • The first line of the input contains an integer b' denoting the number of cases. • The first line of each test case contains an integer N denoting the size of S
• The second line of each teat case contains a string S.
ex:
1
5
abcab

Output : 8
Explaiantion:
There are 2 'a' we choose the first occurence and add it to count.
This a is removed and string becomes bcab
Remove a from bcab and add 3 to count
Remove b from bcb and add 1 to count
reove b from cb and add 2 to count
remove c from c and add 1 to count

Comments (2)