Super ASCII String Checker | TCS CodeVita

07 Sep, 2021
In the Byteland country, a string S is said to super ASCII string if and only if the 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. The task is to find out whether the given string is a super ASCII string or not. If true, then print “Yes” otherwise print “No”.
Examples:
Input: S = “bba” Output: YesExplanation:The count of character ‘b’ is 2 and the ASCII value of ‘b’ is also 2.The count of character ‘a’ is 1. and the ASCII value of ‘a’ is also 1. Hence, string “bba” is super ASCII String.
Input: S = “ssba”Output: NoExplanation:The count of character ‘s’ is 2 and the ASCII value of ‘s’ is 19.The count of character ‘b’ is 1. and the ASCII value of ‘b’ is 2.Hence, string “ssba” is not a super ASCII String.
Approach: The ASCII value of a character ‘ch‘ in Byteland can be calculated by the following formula:
The ASCII value of ch = integer equivalent of ch – integer equivalent of ‘a'(97) + 1
Now, using this formula, the frequency count of each character in the string can be compared with its ASCII value. Follow the below steps to solve the problem:
Initialize an array to store the frequency count of each character of the string.
Traverse the string S and increment the frequency count of each character by 1.
Again, traverse the string S and check if any character has non-zero frequency and is not equal to its ASCII value then print “No”.
After the above steps if there doesn’t any such character in the above step then print “Yes”.
Below is the implementation of the above approach:
Python3
C
Java
Python3
C#
import string #for accessing alphabets dicti = {}a = [] #creating a list with the alphabetsfor i in string.ascii_lowercase:    a.append(i) #creating a dictionary for the alphabets and correpondind ascii codefor i in string.ascii_lowercase:    for j in range (1,27):        if (a.index(i)+1) == j: #if the number is equal to the position of the alphabet            dicti[i] = j        #in the list, then the number will be ascii code for the            break               #aplhabet in the dictionary s = 'bba't = True #t is initialized as true for i in s:    if s.count(i) != dicti[i]: #if any of the alphabet count is not equal to its        t = False              #corresponding ascii code in the dictionary, t will be false if t:    print("Yes")            #printing yes if t remains true after checking all alphabetselse:    print("No")     #code written by jayaselva
// C program for the above approach#include <stdio.h>#include <string.h> // Function to check whether the// string is super ASCII or notvoid checkSuperASCII(char s[]){    // Stores the frequency count    // of characters 'a' to 'z'    int b[26] = { 0 };     // Traverse the string    for (int i = 0; i < strlen(s); i++) {         // AscASCIIii value of the        // current character        int index = (int)s[i] - 97 + 1;         // Count frequency of each        // character in the string        b[index - 1]++;    }     // Traverse the string    for (int i = 0; i < strlen(s); i++) {         // ASCII value of the current        // character        int index = (int)s[i] - 97 + 1;         // Check if the frequency of        // each character in string        // is same as ASCII code or not        if (b[index - 1] != index) {            printf("No");            return;        }    }     // Else print "Yes"    printf("Yes");} // Driver Codeint main(){    // Given string S    char s[] = "bba";     // Function Call    checkSuperASCII(s);    return 0;}
// Java program for the above approachimport java.io.*;import java.util.*; class GFG{     // Function to check whether the// string is super ASCII or notpublic static void checkSuperASCII(String s){         // Stores the frequency count    // of characters 'a' to 'z'    int b[] = new int[26];     // Traverse the string    for(int i = 0; i < s.length(); i++)    {                 // AscASCIIii value of the        // current character        int index = (int)s.charAt(i) - 97 + 1;                 // Count frequency of each        // character in the string        b[index - 1]++;    }     // Traverse the string    for(int i = 0; i < s.length(); i++)    {                 // ASCII value of the current        // character        int index = (int)s.charAt(i) - 97 + 1;                 // Check if the frequency of        // each character in string        // is same as ASCII code or not        if (b[index - 1] != index)        {            System.out.println("No");            return;        }    }         // Else print "Yes"    System.out.println("Yes");} // Driver Codepublic static void main(String args[]){         // Given string S    String s = "bba";         // Function Call    checkSuperASCII(s);}} // This code is contributed by Md Shahbaz Alam
# Python3 program for the above approach # Function to check whether the# string is super ASCII or notdef checkSuperASCII(s):         # Stores the frequency count    # of characters 'a' to 'z'    b = [0 for i in range(26)]     # Traverse the string    for i in range(len(s)):                 # AscASCIIii value of the        # current character        index = ord(s[i]) - 97 + 1;         # Count frequency of each        # character in the string        b[index - 1] += 1     # Traverse the string    for i in range(len(s)):                 # ASCII value of the current        # character        index = ord(s[i]) - 97 + 1         # Check if the frequency of        # each character in string        # is same as ASCII code or not        if (b[index - 1] != index):            print("No")            return     # Else print "Yes"    print("Yes") # Driver Codeif __name__ == '__main__':         # Given string S    s = "bba"     # Function Call    checkSuperASCII(s) # This code is contributed by SURENDRA_GANGWAR
// C# program for the above approachusing System;class GFG {         // Function to check whether the    // string is super ASCII or not    static void checkSuperASCII(string s)    {                  // Stores the frequency count        // of characters 'a' to 'z'        int[] b = new int[26];              // Traverse the string        for(int i = 0; i < s.Length; i++)        {                          // AscASCIIii value of the            // current character            int index = (int)s[i] - 97 + 1;                          // Count frequency of each            // character in the string            b[index - 1]++;        }              // Traverse the string        for(int i = 0; i < s.Length; i++)        {                          // ASCII value of the current            // character            int index = (int)s[i] - 97 + 1;                          // Check if the frequency of            // each character in string            // is same as ASCII code or not            if (b[index - 1] != index)            {                Console.WriteLine("No");                return;            }        }                  // Else print "Yes"        Console.WriteLine("Yes");    }   // Driver code  static void Main() {         // Given string S    string s = "bba";          // Function Call    checkSuperASCII(s);  }} // This code is contributed by divyeshrabadiya07.
Yes
 
Time Complexity: O(N)Auxiliary Space: O(1)
SURENDRA_GANGWAR
shahbazalam75508
khushboogoyal499
divyeshrabadiya07
jayaselvaravi
interview-preparation
TCS
TCS-coding-questions
TCS-interview-experience
Strings
TCS
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different Methods to Reverse a String in C++
KMP Algorithm for Pattern Searching
Python program to check if a string is palindrome or not
Length of the longest substring without repeating characters
Longest Palindromic Substring | Set 1
Convert string to char array in C++
stringstream in C++ and its Applications
Iterate over characters of a string in C++
Program to add two binary strings
Check for Balanced Brackets in an expression (well-formedness) using Stack