Answer to Question #44933 in C++ for jananee

Question #44933
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;
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
14.08.14, 15:25

Dear visitor, please use panel for submitting new questions

sarika
14.08.14, 08:06

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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS