Super ASCII String Checker | TCS CodeVita
Last Updated :
08 Feb, 2024
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++
#include <iostream>
using namespace std;
void checkSuperASCII(string s)
{
int b[26] = {0};
for ( int i = 0; i < s.size(); i++)
{
int index = s[i] - 97 + 1;
b[index - 1]++;
}
for ( int i = 0; i < s.size(); i++)
{
int index = s[i] - 97 + 1;
if (b[index - 1] != index)
{
cout << "No" ;
return ;
}
}
cout << "Yes" ;
}
int main()
{
string s = "bba" ;
checkSuperASCII(s);
return 0;
}
|
C
#include <stdio.h>
#include <string.h>
void checkSuperASCII( char s[])
{
int b[26] = { 0 };
for ( int i = 0; i < strlen (s); i++) {
int index = ( int )s[i] - 97 + 1;
b[index - 1]++;
}
for ( int i = 0; i < strlen (s); i++) {
int index = ( int )s[i] - 97 + 1;
if (b[index - 1] != index) {
printf ( "No" );
return ;
}
}
printf ( "Yes" );
}
int main()
{
char s[] = "bba" ;
checkSuperASCII(s);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
public static void checkSuperASCII(String s)
{
int b[] = new int [ 26 ];
for ( int i = 0 ; i < s.length(); i++)
{
int index = ( int )s.charAt(i) - 97 + 1 ;
b[index - 1 ]++;
}
for ( int i = 0 ; i < s.length(); i++)
{
int index = ( int )s.charAt(i) - 97 + 1 ;
if (b[index - 1 ] != index)
{
System.out.println( "No" );
return ;
}
}
System.out.println( "Yes" );
}
public static void main(String args[])
{
String s = "bba" ;
checkSuperASCII(s);
}
}
|
Python3
import string
dicti = {}
a = []
for i in string.ascii_lowercase:
a.append(i)
for i in string.ascii_lowercase:
for j in range ( 1 , 27 ):
if (a.index(i) + 1 ) = = j:
dicti[i] = j
break
s = 'bba'
t = True
for i in s:
if s.count(i) ! = dicti[i]:
t = False
if t:
print ( "Yes" )
else :
print ( "No" )
|
Python3
def checkSuperASCII(s):
b = [ 0 for i in range ( 26 )]
for i in range ( len (s)):
index = ord (s[i]) - 97 + 1 ;
b[index - 1 ] + = 1
for i in range ( len (s)):
index = ord (s[i]) - 97 + 1
if (b[index - 1 ] ! = index):
print ( "No" )
return
print ( "Yes" )
if __name__ = = '__main__' :
s = "bba"
checkSuperASCII(s)
|
C#
using System;
class GFG {
static void checkSuperASCII( string s)
{
int [] b = new int [26];
for ( int i = 0; i < s.Length; i++)
{
int index = ( int )s[i] - 97 + 1;
b[index - 1]++;
}
for ( int i = 0; i < s.Length; i++)
{
int index = ( int )s[i] - 97 + 1;
if (b[index - 1] != index)
{
Console.WriteLine( "No" );
return ;
}
}
Console.WriteLine( "Yes" );
}
static void Main()
{
string s = "bba" ;
checkSuperASCII(s);
}
}
|
Javascript
function checkSuperASCII(s)
{
let b = new Array(26).fill(0);
for (let i = 0; i < s.length; i++)
{
let index = s[i].charCodeAt(0) - 97 + 1;
b[index - 1] = b[index-1] + 1;
}
for (let i = 0; i < s.length; i++)
{
let index = s[i].charCodeAt(0) - 97 + 1;
if (b[index - 1] != index)
{
console.log( "No" );
return ;
}
}
console.log( "Yes" );
}
let s = "bba" ;
checkSuperASCII(s);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...