Given a lower alphabetic string **“S”** of size **N**, and an integer **K**; the task is to find the count of characters that will remain ungrouped, after dividing the given string into K groups of distinct characters.

**Examples:**

Input:S = “stayinghomesaveslife”, K = 1

Output:6

Explanation:

In the string S the elements whose occurence is more than one time are ‘e’ -> 3 times, ‘s’ -> 3 times, ‘a’ -> 2 times, ‘i’ -> 2 times and rest all elements occurs 1 time each.

There is only one group to be formed as K = 1 so only one copy of these elements can be present in the group and rest all elements cannot be in the group,

So the elements which are left out of the group are 2-times ‘e’, 2-times ‘s’, 1-time ‘a’ and 1-time ‘i’.

Total elements left out are 6.

Input:S = “stayinghomesaveslife”, K = 2

Output:2

Explanation:

In the string S the elements whose occurence is more than one time are ‘e’ -> 3 times, ‘s’ -> 3 times, ‘a’ -> 2 times, ‘i’ -> 2 times and rest all elements occurs 1 time each.

Since 2 groups can be formed, one group contains 1 copy of all the elements. The second group will contain 1 copy of the extra elements i.e. ‘e’, ‘s’, ‘a’ and ‘i’. The elements that will be left out are 1-time ‘e’ and 1-time ‘s’.

Total elements that will be left out are 2.

**Approach:** The idea is to use frequency-counting.

- Create a Hashing data structure, to store the frequency of characters ‘a’-‘z’.
- Find the initial frequency of each character in the given string and store it in the hashing data structure.
- Since a group can contain only 1 occurrence of a character. Therefore, decrement K from each character’s occurrence in the hashing data structure.
- Now add the the remaining frequencies of the characters in the hashing data structure. This will be the required number of characters that will remain ungrouped.

Below is the implementation of the above approach:

## C++

`// C++ code to implement the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `findUngroupedElement(string s, ` ` ` `int` `k) ` `{ ` ` ` ` ` `int` `n = s.length(); ` ` ` ` ` `// create array where ` ` ` `// index represents alphabets ` ` ` `int` `b[26]; ` ` ` ` ` `for` `(` `int` `i = 0; i < 26; i++) ` ` ` `b[i] = 0; ` ` ` ` ` `// fill count of every ` ` ` `// alphabet to corresponding ` ` ` `// array index ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `char` `p = s.at(i); ` ` ` `b[p - ` `'a'` `] += 1; ` ` ` `} ` ` ` ` ` `int` `sum = 0; ` ` ` ` ` `// count for every element ` ` ` `// how much is exceeding ` ` ` `// from no. of groups then ` ` ` `// sum them ` ` ` `for` `(` `int` `i = 0; i < 26; i++) { ` ` ` `if` `(b[i] > k) ` ` ` `sum += b[i] - k; ` ` ` `} ` ` ` ` ` `// print answer ` ` ` `cout << sum << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string s = ` `"stayinghomesaveslife"` `; ` ` ` `int` `k = 1; ` ` ` ` ` `findUngroupedElement(s, k); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code to implement the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `static` `void` `findUngroupedElement(String s, ` ` ` `int` `k) ` `{ ` ` ` `int` `n = s.length(); ` ` ` ` ` `// Create array where ` ` ` `// index represents alphabets ` ` ` `int` `[]b = ` `new` `int` `[` `26` `]; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `26` `; i++) ` ` ` `b[i] = ` `0` `; ` ` ` ` ` `// Fill count of every ` ` ` `// alphabet to corresponding ` ` ` `// array index ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `char` `p = s.charAt(i); ` ` ` `b[p - ` `'a'` `] += ` `1` `; ` ` ` `} ` ` ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Count for every element ` ` ` `// how much is exceeding ` ` ` `// from no. of groups then ` ` ` `// sum them ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `26` `; i++) ` ` ` `{ ` ` ` `if` `(b[i] > k) ` ` ` `sum += b[i] - k; ` ` ` `} ` ` ` ` ` `// Print answer ` ` ` `System.out.println(sum); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String srgs[]) ` `{ ` ` ` `String s = ` `"stayinghomesaveslife"` `; ` ` ` `int` `k = ` `1` `; ` ` ` ` ` `findUngroupedElement(s, k); ` `} ` `} ` ` ` `// This code is contributed by ANKITKUMAR34 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to implement the above approach ` `def` `findUngroupedElement(s, k): ` ` ` ` ` `n ` `=` `len` `(s); ` ` ` ` ` `# Create array where ` ` ` `# index represents alphabets ` ` ` `b ` `=` `[` `0` `] ` `*` `26` ` ` ` ` `# Fill count of every ` ` ` `# alphabet to corresponding ` ` ` `# array index ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `p ` `=` `s[i] ` ` ` `b[` `ord` `(p) ` `-` `ord` `(` `'a'` `)] ` `+` `=` `1` ` ` ` ` `sum` `=` `0` `; ` ` ` ` ` `# Count for every element ` ` ` `# how much is exceeding ` ` ` `# from no. of groups then ` ` ` `# sum them ` ` ` `for` `i ` `in` `range` `(` `26` `): ` ` ` `if` `(b[i] > k): ` ` ` `sum` `+` `=` `b[i] ` `-` `k ` ` ` ` ` `# Print answer ` ` ` `print` `(` `sum` `) ` ` ` `# Driver code ` `s ` `=` `"stayinghomesaveslife"` `k ` `=` `1` ` ` `findUngroupedElement(s, k) ` ` ` `# This code is contributed by ANKITKUMAR34 ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity:** O(N)

**Auxillary Space complexity:** O(26) which is equivalent to O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- String with k distinct characters and no same characters adjacent
- Check whether count of distinct characters in a string is Prime or not
- Count of Distinct strings possible by inserting K characters in the original string
- String Range Queries to count number of distinct characters with updates
- Count ways to partition a string such that both parts have equal distinct characters
- Sort groups of numbers and characters separately in a String
- Replace minimal number of characters to make all characters pair wise distinct
- Find distinct characters in distinct substrings of a string
- Count of substrings having all distinct characters
- Count distinct substrings that contain some characters at most k times
- Count of substrings of length K with exactly K distinct characters
- Count number of substrings with exactly k distinct characters
- Find the String having each substring with exactly K distinct characters
- Convert given string so that it holds only distinct characters
- Count of distinct characters in a substring by given range for Q queries
- Contiguous subsegments of a string having distinct subsequent characters
- Print all distinct characters of a string in order (3 Methods)
- Count the number of strings in an array whose distinct characters are less than equal to M
- Sort an array of strings based on count of distinct characters
- Find the number of strings formed using distinct characters of a given string

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.