Super ASCII String Checker | TCS CodeVita
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:
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
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:
Python3
import string #for accessing alphabetsdicti = {}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 dictionarys = 'bba't = True #t is initialized as truefor 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 falseif t: print("Yes") #printing yes if t remains true after checking all alphabetselse: print("No") #code written by jayaselva |
C
// 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
// 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
# 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#
// 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)


