Join Regular Classroom : Visit ClassroomTech

Strings Based Coding Problems | Super ASCII String | Codewindow.in

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’).

Example 1:

Input:
2
bba
scca

Output:
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

Solution: In C

#include<stdio.h>
#include<string.h>

int main()
{
    int test, flag=0,length=0;
    int j,count[26]={0,};
    char arr[400];
    scanf("%d",&test);
    if(test>0 && test<=100)
    {
        while(test)
        {
            scanf("%s",arr);
            length=strlen(arr);
            if(length<=400)
            {
                for(j=0;arr[j]!='\0';j++)
                {
                    if(arr[j]<97 || arr[j]>122)
                    {
                        flag++;
                        break;
                    }
                    count[(arr[j]-97)]++;
                }
                for(j=0;j<26;j++)
                {
                    if(count[j]!=0)
                    {
                        if(count[j]!=j+1)
                        {
                            flag++;
                            break;
                        }
                    }
                }
                if(flag)
                    printf("No\n");
                else
                    printf("Yes\n");
            }
            for(j=0;j<26;j++)
            {
                count[j]=0;
            }
            flag=0;
            test--;
        }
    }
    return 0;
}

Solution: In Java

import java.io.*;

class Main
{
    static int count[]=new int[27];
    public static void main(String args[])throws IOException
    {
        try
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int no=Integer.parseInt(br.readLine());
            if(no>=1 && no<=100)
            {
                while(no!=0)
                {
                    String str=br.readLine();
                    int len=str.length();
                    if(strcheck(str,len))
                    {
                        for(int i=0;i<len;i++)
                        {
                            int value=((int)str.charAt(i))-96;
                            count[value]+=1;
                        }
                        if(check())
                        {
                            System.out.println("Yes");
                        }
                        else
                        {
                            System.out.println("No");
                        }
                        no=no-1;
                    }
                    else
                    {
                        System.out.println("Wrong String-->1<=|S|<=400, S will contains only lower case alphabets ('a'-'z').\nre-enter");
                    }
                }
            }
            else
            {
                System.out.println("Test case Exceeded");
            }
        }
        catch(Exception e)
        {
        }
    }
    public static boolean check()
    {
        for(int i=1;i<=26;i++)
        {
            if(count[i]!=i && count[i]!=0)
            {
                return false;
            }
        }
        return true;
    }
    public static boolean strcheck(String str,int len)
    {
        if(len<=1 || len >=400)
        {
            return false;
        }
        else
        {
            for(char c : str.toCharArray()) {
                if(Character.isLetter(c) && Character.isUpperCase(c)) {
                    return false;
                }
            }
        }
        return true;
    }
}

Solution: In Python 3

n=int(input())
i=0
list=list()
while i<n:
    list.append(input())
    i=i+1
i=0
while i<n:
    s=list[i]
    j=0
    flag=1
    while j<s.__len__() and flag==1:
        if s.count(s[j])!=(ord(s[j])-96):
            flag=0;
        j=j+1
    if(flag==1):
        print("Yes")
    else:
        print("No")
    i=i+1

Also Checkout

Recent Posts
Categories
Pages