Check if frequency of each character is equal to its position in English Alphabet
Given string str of lowercase alphabets, the task is to check if the frequency of each distinct characters in the string equals to its position in the English Alphabet. If valid, then print “Yes”, else print “No”.
Examples:
Input: str = “abbcccdddd”
Output: Yes
Explanation:
Since frequency of each distinct character is equals to its position in English Alphabet, i.e.
F(a) = 1,
F(b) = 2,
F(c) = 3, and
F(d) = 4
Hence the output is Yes.
Input: str = “geeksforgeeks”
Output: No
Approach:
- Store the frequency of each character in an array of 26, for hashing purpose.
- Now traverse the hash array and check if the frequency of each character at an index i is equal to (i + 1) or not.
- If yes, then print “Yes”, Else print “No”.
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
using namespace std;
bool checkValidString(string str)
{
int freq[26] = { 0 };
for ( int i = 0; str[i]; i++) {
freq[str[i] - 'a' ]++;
}
for ( int i = 0; i < 26; i++) {
if (freq[i] != 0) {
if (freq[i] != i + 1) {
return false ;
}
}
}
return true ;
}
int main()
{
string str = "abbcccdddd" ;
if (checkValidString(str))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG{
static boolean checkValidString(String str)
{
int freq[] = new int [ 26 ];
for ( int i = 0 ; i < str.length(); i++)
{
freq[str.charAt(i) - 'a' ]++;
}
for ( int i = 0 ; i < 26 ; i++)
{
if (freq[i] != 0 )
{
if (freq[i] != i + 1 )
{
return false ;
}
}
}
return true ;
}
public static void main(String[] args)
{
String str = "abbcccdddd" ;
if (checkValidString(str))
{
System.out.print( "Yes" );
}
else
{
System.out.print( "No" );
}
}
}
|
Python3
def checkValidString( str ):
freq = [ 0 for i in range ( 26 )]
for i in range ( len ( str )):
freq[ ord ( str [i]) - ord ( 'a' )] + = 1
for i in range ( 26 ):
if (freq[i] ! = 0 ):
if (freq[i] ! = i + 1 ):
return False
return True
str = "abbcccdddd"
if (checkValidString( str )):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool checkValidString(String str)
{
int []freq = new int [26];
for ( int i = 0; i < str.Length; i++)
{
freq[str[i] - 'a' ]++;
}
for ( int i = 0; i < 26; i++)
{
if (freq[i] != 0)
{
if (freq[i] != i + 1)
{
return false ;
}
}
}
return true ;
}
public static void Main(String[] args)
{
String str = "abbcccdddd" ;
if (checkValidString(str))
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
function checkValidString(str)
{
var freq = new Array(26).fill(0);
for ( var i = 0; i < str.length; i++)
{
freq[str[i].charCodeAt(0) -
"a" .charCodeAt(0)]++;
}
for ( var i = 0; i < 26; i++)
{
if (freq[i] !== 0)
{
if (freq[i] !== i + 1)
{
return false ;
}
}
}
return true ;
}
var str = "abbcccdddd" ;
if (checkValidString(str))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(26)
Method: Using Map
Algorithm :
- Create a HashMap map with character and integer as key and value pairs respectively.
- Loop through each character of the string str and count the frequency.
- Loop through each character in the string str again and check if its frequency is equal to its position in the English alphabet.
- If any character fails to satisfy the condition, set the boolean variable isEqual to false and break out of the loop.
- Print the result based on the value of isEqual.
C++
#include <iostream>
#include <unordered_map>
using namespace std;
bool check(string str) {
unordered_map< char , int > map;
for ( char c : str) {
if (map.find(c) != map.end()) {
map++;
} else {
map = 1;
}
}
bool isEqual = true ;
for ( char c : str) {
int frequency = map;
int position = c - 'a' + 1;
if (frequency != position) {
isEqual = false ;
break ;
}
}
return isEqual;
}
int main() {
string str = "abbcccdddd" ;
bool ans = check(str);
if (ans) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args)
{
String str = "abbcccdddd" ;
boolean ans = check(str);
if (ans) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
public static boolean check(String str)
{
Map<Character, Integer> map = new HashMap<>();
for ( char c : str.toCharArray()) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1 );
}
else {
map.put(c, 1 );
}
}
boolean isEqual = true ;
for ( char c : str.toCharArray())
{
int frequency = map.get(c);
int position = c - 'a' + 1 ;
if (frequency != position)
{
isEqual = false ;
break ;
}
}
return isEqual;
}
}
|
Python3
def check(s):
char_count = {}
for char in s:
if char in char_count:
char_count[char] + = 1
else :
char_count[char] = 1
is_equal = True
for char in s:
frequency = char_count[char]
position = ord (char) - ord ( 'a' ) + 1
if frequency ! = position:
is_equal = False
break
return is_equal
if __name__ = = "__main__" :
input_str = "abbcccdddd"
result = check(input_str)
if result:
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static void Main( string [] args)
{
string str = "abbcccdddd" ;
bool ans = Check(str);
if (ans)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
public static bool Check( string str)
{
Dictionary< char , int > map = new Dictionary< char , int >();
foreach ( char c in str)
{
if (map.ContainsKey(c))
{
map++;
}
else
{
map = 1;
}
}
bool isEqual = true ;
foreach ( char c in str)
{
int frequency = map;
int position = c - 'a' + 1;
if (frequency != position)
{
isEqual = false ;
break ;
}
}
return isEqual;
}
}
|
Javascript
function check(str) {
const map = new Map();
for (const c of str) {
if (map.has(c)) {
map.set(c, map.get(c) + 1);
} else {
map.set(c, 1);
}
}
let isEqual = true ;
for (const c of str) {
const frequency = map.get(c);
const position = c.charCodeAt(0) - 'a' .charCodeAt(0) + 1;
if (frequency !== position) {
isEqual = false ;
break ;
}
}
return isEqual;
}
const str = "abbcccdddd" ;
const ans = check(str);
if (ans) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(1)
Note: The size of the map never exceeds 26 here as the lower case English alphabets are only 26, so the Auxiliary space is 1 (constant).
Last Updated :
20 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...