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 occurrence 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 occurrence 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 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++
#include <bits/stdc++.h>
using namespace std;
void findUngroupedElement(string s,
int k)
{
int n = s.length();
int b[26];
for ( int i = 0; i < 26; i++)
b[i] = 0;
for ( int i = 0; i < n; i++) {
char p = s.at(i);
b[p - 'a' ] += 1;
}
int sum = 0;
for ( int i = 0; i < 26; i++) {
if (b[i] > k)
sum += b[i] - k;
}
cout << sum << endl;
}
int main()
{
string s = "stayinghomesaveslife" ;
int k = 1;
findUngroupedElement(s, k);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findUngroupedElement(String s,
int k)
{
int n = s.length();
int []b = new int [ 26 ];
for ( int i = 0 ; i < 26 ; i++)
b[i] = 0 ;
for ( int i = 0 ; i < n; i++)
{
char p = s.charAt(i);
b[p - 'a' ] += 1 ;
}
int sum = 0 ;
for ( int i = 0 ; i < 26 ; i++)
{
if (b[i] > k)
sum += b[i] - k;
}
System.out.println(sum);
}
public static void main(String srgs[])
{
String s = "stayinghomesaveslife" ;
int k = 1 ;
findUngroupedElement(s, k);
}
}
|
Python3
def findUngroupedElement(s, k):
n = len (s);
b = [ 0 ] * 26
for i in range (n):
p = s[i]
b[ ord (p) - ord ( 'a' )] + = 1
sum = 0 ;
for i in range ( 26 ):
if (b[i] > k):
sum + = b[i] - k
print ( sum )
s = "stayinghomesaveslife"
k = 1
findUngroupedElement(s, k)
|
C#
using System;
class GFG{
static void findUngroupedElement(String s,
int k)
{
int n = s.Length;
int []b = new int [26];
for ( int i = 0; i < 26; i++)
b[i] = 0;
for ( int i = 0; i < n; i++)
{
char p = s[i];
b[p - 'a' ] += 1;
}
int sum = 0;
for ( int i = 0; i < 26; i++)
{
if (b[i] > k)
sum += b[i] - k;
}
Console.WriteLine(sum);
}
public static void Main(String []srgs)
{
String s = "stayinghomesaveslife" ;
int k = 1;
findUngroupedElement(s, k);
}
}
|
Javascript
<script>
function findUngroupedElement(s, k)
{
let n = s.length;
let b = Array.from({length: 26}, (_, i) => 0);
for (let i = 0; i < 26; i++)
b[i] = 0;
for (let i = 0; i < n; i++)
{
let p = s[i];
b[p.charCodeAt() - 'a' .charCodeAt()] += 1;
}
let sum = 0;
for (let i = 0; i < 26; i++)
{
if (b[i] > k)
sum += b[i] - k;
}
document.write(sum);
}
let s = "stayinghomesaveslife" ;
let k = 1;
findUngroupedElement(s, k);
</script>
|
Time Complexity: O(N)
Auxiliary Space complexity: O(26) which is equivalent to O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!