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

Given a 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Store the frquency of each character in an array of 26, for hashing purpose.
2. Now traverse the hash array and check if frequency of each characters at an index i is equal to (i + 1) or not.
3. If yes, then print “Yes”, Else print “No”.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach ` ` `  `#include "bits/stdc++.h" ` `using` `namespace` `std; ` ` `  `bool` `checkValidString(string str) ` `{ ` ` `  `    ``// Initialise frequency array ` `    ``int` `freq = { 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 Code ` `int` `main() ` `{ ` ` `  `    ``// Given string str ` `    ``string str = ``"abbcccdddd"``; ` ` `  `    ``if` `(checkValidString(str)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program for the above approach ` `class` `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 Code ` `public` `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 `

## C#

 `// C# program for the above approach ` `using` `System; ` `class` `GFG{ ` ` `  `static` `bool` `checkValidString(String str) ` `{ ` `     `  `    ``// Initialise frequency array ` `    ``int` `[]freq = ``new` `int``; ` ` `  `    ``// 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 Code ` `public` `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 `

Output:

```Yes
```

Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(26)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : sapnasingh4991