Algorithm
In order to write a string, Atilla needs to first learn all letters that are contained in the string.
Atilla needs to write a message which can be represented as a string s. He asks you what is the minimum alphabet size required so that one can write this message.
The alphabet of size x (1≤x≤26) contains only the first x Latin letters. For example an alphabet of size 4 contains only the characters a, b, c and d.
The first line contains a single integer t (1≤t≤1000) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤100) — the length of the string.
The second line of each test case contains a string s of length n, consisting of lowercase Latin letters.
For each test case, output a single integer — the minimum alphabet size required to so that Atilla can write his message s.
1 23 19 6 26
For the first test case, Atilla needs to know only the character a, so the alphabet of size 1 which only contains a is enough.
For the second test case, Atilla needs to know the characters d, o, w, n. The smallest alphabet size that contains all of them is 23 (such alphabet can be represented as the string abcdefghijklmnopqrstuvw).
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s;
cin>>s;
int ans = 0;
for(int i = 0; i < n; i++){
ans = max(ans, s[i] - 'a' + 1);
}
cout<<ans<<endl;
}
}
Copy The Code &
Try With Live Editor
Input
1
a
4
down
10
codeforces
3
bcf
5
zzzzz
Output
23
19
6
26
Demonstration
Codeforcess Solution 1760-B B. Atilla's Favorite Problem ,C++, Java, Js and Python,1760-B,Codeforcess Solution