Skip to content
Related Articles

Related Articles

Super ASCII String Checker | TCS CodeVita
  • Difficulty Level : Hard
  • Last Updated : 03 Feb, 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: Yes
Explanation:
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: No
Explanation:
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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <iostream>
#include <string.h>
 
using namespace std;
 
// Function to check whether the
// string is super ASCII or not
void 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)
        {
            cout << "No";
            return;
        }
    }
 
    // Else print "Yes"
    cout << "Yes";
}
 
// Driver Code
int main()
{
     
    // Given string S
    char s[] = "bba";
 
    // Function Call
    checkSuperASCII(s);
     
    return 0;
}
 
// This code is contributed by khushboogoyal499

chevron_right


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program for the above approach
#include <stdio.h>
#include <string.h>
 
// Function to check whether the
// string is super ASCII or not
void 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 Code
int main()
{
    // Given string S
    char s[] = "bba";
 
    // Function Call
    checkSuperASCII(s);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to check whether the
// string is super ASCII or not
public 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 Code
public static void main(String args[])
{
     
    // Given string S
    String s = "bba";
     
    // Function Call
    checkSuperASCII(s);
}
}
 
// This code is contributed by Md Shahbaz Alam

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function to check whether the
# string is super ASCII or not
def 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 Code
if __name__ == '__main__':
     
    # Given string S
    s = "bba"
 
    # Function Call
    checkSuperASCII(s)
 
# This code is contributed by SURENDRA_GANGWAR

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using 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.

chevron_right


Output: 

Yes

 

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :