Super Ascii
Problem Decription:
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').
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.
Program:
#include <stdio.h> int 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; }
Output:
2
bba
scca
yes
no
You can also run it on the online IDE: https://ide.geeksforgeeks.org/q64aeIcLxM
Your feedback and comments are welcomed! If you have an doubt can contact me or comment below! Cheers!
Related Link: Date Time 2018
Related Link: Date Time 2018
package Tcs;
ReplyDeleteimport java.util.Scanner;
public class SuperAscii {
public static String check(String s)
{
String ans="Yes";
int count[]=new int[256];
for(int i=0;i0)
{
String s=sc.next()+sc.nextLine();
String ans=check(s);
sb.append(ans+"\n");
}
System.out.print(sb);
}
}
send code in python
ReplyDeleteSend a code in python language
ReplyDelete#include
ReplyDelete#include
int main()
{
char a[100],temp=0,p=0;
int i,j,c=1,r,k=0,x[100],s;
// printf("the ascii of a is 097\n");
// printf("enter the string :");
scanf("%s",&a);
s=strlen(a);
for(i=0;i<s;i++)
{ c=1;
for(j=i+1;j<s;j++)
{
if(a[i]==a[j] && p!=a[i])
{ temp=a[i];
// printf("%c",a[i]);
c++;
r=a[i]-96;
}
}
p=temp;
if(c==1)
{
r=a[i]-96;
}
if(r==c)
{
k=k+c;
// printf("%d\t",c);
}
}
// printf("%d",k);
if(s==k)
{
printf("\tsuper ");
}
main();
}