In the Byteland country a string "S" is said to super ascii string if and only if count of each character in the string is equal to its ascii value.
In the Byteland country ascii code of 'a' is 1, 'b' is 2 ...'z' is 26.
Your task is to find out whether the given string is a super ascii string or not.
Input Format:
First line contains number of test cases T, followed by T lines, each containing a string "S".
Output Format:
For each test case print "Yes" if the String "S" is super ascii, else print "No"
Constraints:
1<=T<=100
1<=|S|<=400, S will contains only lower case alphabets ('a'-'z').
1
Expert's answer
2014-08-14T09:38:30-0400
#include <iostream> #include <string> using namespace std; int main() { int T; cin >> T; while (T-- > 0) { string S; cin >> S; int cnt['z' - 'a' + 1] = {}; // an array with the number of occurrences of each letter for (int i = 0; i < S.length(); ++i) { // calculate the number od occurreces of each letter ++cnt[S[i] - 'a']; // S[i] - 'a' is the ascii code of the letter S[i] } bool is_super = true; for (int i = 0; i <= 'z' - 'a'; ++i) { if (cnt[i] != i + 1 && cnt[i] > 0) is_super = false; // the array is indexed from 0 but the ascii codes are starting from 1, so we check for cnt[i] != i + 1 } if (is_super == true) cout << "Yes" << endl; else cout << "No" << endl; } }
Comments
Dear visitor, please use panel for submitting new questions
In the Byteland country a string "S" is said to super ascii string if and only if count of each character in the string is equal to its ascii value.
Leave a comment