Problem : Super ASCII String Checker
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.
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').
Sample Input and Output
SNo. | Input | Output |
---|---|---|
1 | 2 bba scca | Yes No |
Explanation:
In case 1, viz. String "bba" -
The count of character 'b' is 2. Ascii value of 'b' is also 2.
The count of character 'a' is 1. Ascii value of 'a' is also 1.
Hence string "bba" is super ascii.
In case 1, viz. String "bba" -
The count of character 'b' is 2. Ascii value of 'b' is also 2.
The count of character 'a' is 1. Ascii value of 'a' is also 1.
Hence string "bba" is super ascii.
Solution:
#include<iostream>
using namespace std;
int c[26];
int main(){
int t,len,flag=1;;
string s;
cin>>t;
while(t--){
cin>>s;
len=s.length();
for(int i=0;i<len;i++)
c[int(s[i])-int('a')]++;
for(int i=0;i<26;i++){
if(!(c[i]==0 || c[i]==i+1))
flag=0;
}
if(flag==1)
cout<<"Yes\n";
else
cout<<"No\n";
for(int i=0;i<26;i++)
c[i]=0;
flag=1;
}
return 0;
}
can please give solution in C
ReplyDelete#include
ReplyDeleteusing namespace std;
int main() {
int t;
cin>> t;
START:
while(t--){
string s;
cin>> s;
int ar[27]={0};
for(int i=0;i<s.length();i++)ar[s.at(i)-'a'+1]++;
for(int i=0;i<s.length();i++){
if(ar[s.at(i)-'a'+1]!=s.at(i)-'a'+1){
cout<< "No"<<endl;
goto START;
}
}
cout<<"Yes"<< endl;
}
return 0;
}
c[int(s[i])-int('a')]++;
ReplyDeletefor(int i=0;i<26;i++){
if(!(c[i]==0 || c[i]==i+1))
flag=0;
PLEASE TELL ME THE WORKING OF THIS CODE PRESENT HERE
int(s[i])-int('a') gives 1 less than ascii value of a character. For example if s[i] is equal to b then it evaluates to int('b')-int('a') = 1
Deleteso using this property we are storing count of a character with ascii value x at index x-1
The condition !(c[i]==0 || c[i]==i+1) can be simplified to (c[i]!=0 && c[i]!=i+1) using de morgan's law. c[i] stores count of a character with ascii value i+1. S0 we check whether it is equal to i+1.If not we set the flag to zero
#include
ReplyDeleteint main() {
char s[30];
int i,num[30]={0},isascii,n;
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
i=0;
isascii=1;
while(s[i]!='\0')
{
if((s[i]>='a')&&(s[i]<='z'))
num[s[i]-'a']++;
s[i]='\0';
i++;
}
for(i=0;i<26;i++)
{
if((num[i]>0)&&(num[i]!=(i+1)))
isascii=0;
num[i]=0;
}
if(isascii)
printf("yes\n");
else
printf("no");
}
return 0;
}