Check if frequency of each character is equal to its position in English Alphabet - GeeksforGeeks

13 May, 2021
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”.
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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include "bits/stdc++.h"using namespace std; bool checkValidString(string str){     // Initialise frequency array    int freq[26] = { 0 };     // Traverse the string    for (int i = 0; str[i]; i++) {         // Update the frequency        freq[str[i] - 'a']++;    }     // Check for valid string    for (int i = 0; i < 26; i++) {         // If frequency is non-zero        if (freq[i] != 0) {             // If freq is not equals            // to (i+1), then return            // false            if (freq[i] != i + 1) {                return false;            }        }    }     // Return true;    return true;} // Driver Codeint main(){     // Given string str    string str = "abbcccdddd";     if (checkValidString(str))        cout << "Yes";    else        cout << "No";     return 0;}
// Java program for the above approachclass GFG{ static boolean checkValidString(String str){         // Initialise frequency array    int freq[] = new int[26];     // Traverse the String    for(int i = 0; i < str.length(); i++)    {               // Update the frequency       freq[str.charAt(i) - 'a']++;    }     // Check for valid String    for(int i = 0; i < 26; i++)    {               // If frequency is non-zero       if (freq[i] != 0)       {                       // If freq is not equals           // to (i+1), then return           // false           if (freq[i] != i + 1)           {               return false;           }       }    }         // Return true;    return true;} // Driver Codepublic static void main(String[] args){     // Given String str    String str = "abbcccdddd";     if (checkValidString(str))    {        System.out.print("Yes");    }    else    {        System.out.print("No");    }}} // This code is contributed by sapnasingh4991
# Python3 program for the# above approachdef checkValidString(str):     # Initialise frequency array    freq = [0 for i in range(26)]     # Traverse the string    for i in range(len(str)):         # Update the frequency        freq[ord(str[i]) - ord('a')] += 1     # Check for valid string    for i in range(26):         # If frequency is non-zero        if(freq[i] != 0):             # If freq is not equals            # to (i+1), then return            # false            if(freq[i] != i + 1):                return False    # Return true    return True # Driver Code # Given string strstr = "abbcccdddd" if(checkValidString(str)):    print("Yes")else:    print("No") # This code is contributed by avanitrachhadiya2155
// C# program for the above approachusing System;class GFG{ static bool checkValidString(String str){         // Initialise frequency array    int []freq = new int[26];     // Traverse the String    for(int i = 0; i < str.Length; i++)    {                 // Update the frequency        freq[str[i] - 'a']++;    }     // Check for valid String    for(int i = 0; i < 26; i++)    {                 // If frequency is non-zero        if (freq[i] != 0)        {                             // If freq is not equals            // to (i+1), then return            // false            if (freq[i] != i + 1)            {                return false;            }        }    }         // Return true;    return true;} // Driver Codepublic static void Main(String[] args){     // Given String str    String str = "abbcccdddd";     if (checkValidString(str))    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }}} // This code is contributed by sapnasingh4991
<script> // Javascript program for the above approachfunction checkValidString(str){         // Initialise frequency array    var freq = new Array(26).fill(0);         // Traverse the String    for(var i = 0; i < str.length; i++)    {                 // Update the frequency        freq[str[i].charCodeAt(0) -                "a".charCodeAt(0)]++;    }         // Check for valid String    for(var i = 0; i < 26; i++)    {                 // If frequency is non-zero        if (freq[i] !== 0)        {                         // If freq is not equals            // to (i+1), then return            // false            if (freq[i] !== i + 1)            {                return false;            }        }    }         // Return true;    return true;} // Driver Code // Given String strvar str = "abbcccdddd"; if (checkValidString(str)){    document.write("Yes");}else{    document.write("No");} // This code is contributed by rdtank </script>
Yes
 
Time Complexity: O(N), where N is the length of the string. Auxiliary Space: O(26)
sapnasingh4991
avanitrachhadiya2155
rdtank
ASCII
frequency-counting
Arrays
Hash
School Programming
Strings
Arrays
Hash
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Reversal algorithm for array rotation
Building Heap from Array
Program to find sum of elements in a given array
Internal Working of HashMap in Java
Hashing | Set 1 (Introduction)
Hashing | Set 3 (Open Addressing)
Count pairs with given sum
Hashing | Set 2 (Separate Chaining)