# Largest index for each distinct character in given string with frequency K

Given a string S consisting of lower case English letters and an integer K, the task is to find, for each distinct character in S, the largest index having this character exactly K times. If no such characters exist, print -1. Print the result in a lexicographical ordering.

Note: Consider 0-based indexing in S.

Examples:

Input: S = “cbaabaacbcd”, K = 2
Output: { {a 4}, {b 7}, {c 8} }
Explanation:
For ‘a’, the largest index having 2 a’s is “cbaab”.
For ‘b’, the largest index having 2 b’s is “cbaabaac”.
For ‘c’, the largest index having 2 c’s is “cbaabaacb”.
For ‘d’, the is no index up to which we have 2 d’s

Input: P = “acbacbacbaba”, K = 3
Output: { {a 8}, {b 9}, {c 11} }

Approach: The idea is to first find all the distinct characters in string S. Then for each lower case English character, check whether it is present in S or not and run a for loop from the beginning of S and maintain the count of that character occurred till now. When the count becomes equal to K update the index answer accordingly. Finally, append this character and its corresponding index in the vector result.

Below is the implementation of the above approach.

 `// C++ implementation of the approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find largest index for each ` `// distinct character occuring exactly K times. ` `void` `maxSubstring(string& S, ``int` `K, ``int` `N) ` `{ ` ` `  `    ``// finding all characters present in S ` `    ``int` `freq; ` `    ``memset``(freq, 0, ``sizeof` `freq); ` ` `  `    ``// Finding all distinct characters in S ` `    ``for` `(``int` `i = 0; i < N; ++i) { ` `        ``freq[S[i] - ``'a'``] = 1; ` `    ``} ` ` `  `    ``// vector to store result for each character ` `    ``vector > answer; ` ` `  `    ``// loop through each lower case English character ` `    ``for` `(``int` `i = 0; i < 26; ++i) { ` ` `  `        ``// if current character is absent in s ` `        ``if` `(freq[i] == 0) ` `            ``continue``; ` ` `  `        ``// getting current character ` `        ``char` `ch = i + 97; ` ` `  `        ``// finding count of character ch in S ` `        ``int` `count = 0; ` ` `  `        ``// to store max Index encountred so far ` `        ``int` `index = -1; ` ` `  `        ``for` `(``int` `j = 0; j < N; ++j) { ` `            ``if` `(S[j] == ch) ` `                ``count++; ` ` `  `            ``if` `(count == K) ` `                ``index = j; ` `        ``} ` ` `  `        ``answer.push_back({ ch, index }); ` `    ``} ` ` `  `    ``int` `flag = 0; ` ` `  `    ``// printing required result ` `    ``for` `(``int` `i = 0; i < (``int``)answer.size(); ++i) { ` ` `  `        ``if` `(answer[i].second > -1) { ` `            ``flag = 1; ` `            ``cout << answer[i].first << ``" "` `                 ``<< answer[i].second << endl; ` `        ``} ` `    ``} ` ` `  `    ``// If no such character exists, print -1 ` `    ``if` `(flag == 0) ` `        ``cout << ``"-1"` `<< endl; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string S = ``"cbaabaacbcd"``; ` `    ``int` `K = 5; ` `    ``int` `N = S.length(); ` ` `  `    ``maxSubstring(S, K, N); ` ` `  `    ``return` `0; ` `} `

Output:

```a 4
b 7
c 8
```

Time Complexity: O(26 * N)

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.