# Minimize the length of string by removing occurrence of only one character

Given a string str containing only lowercase alphabets, the task is to minimize the length of the string after performing the following operation:

• Delete all occurrences of any one character from this string.

Examples:

```Input: str = "abccderccwq"
Output: 7
character 'c' will be deleted since it has maximum occurence.

Input: str = "dddded"
Output: 1
character 'd' will be deleted
```

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

Approach:

1. Maintain the frequency of each and every character in an array.
2. Find the character with the maximum frequency.
3. Finally, subtract the length of the original string with the frequency of that character to get the desired answer.

Below is the implementation of the above approach:

## C++

 `// C++ program to minimize the length of string by  ` `// removing occurrence of only one character ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find the minimum length ` `int` `minimumLength(string s) ` `{ ` `    ``int` `maxOcc = 0, n = s.length(); ` `    ``int` `arr = {0}; ` ` `  `    ``// Count the frequency of each alphabet ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``arr[s[i] - ``'a'``]++; ` ` `  `    ``// Find the alphabets with maximum frequency ` `    ``for` `(``int` `i = 0; i < 26; i++) ` `        ``if` `(arr[i] > maxOcc) ` `            ``maxOcc = arr[i]; ` ` `  `    ``// Subtract the frequency of character ` `    ``// from length of string ` `    ``return` `(n - maxOcc); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``string str = ``"afddewqd"``; ` `    ``cout << minimumLength(str); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to minimize the length of string  ` `// by removing occurrence of only one character ` `import` `java.io.*; ` ` `  `class` `GFG { ` `    `  ` `  `// Function to find the minimum length  ` `static` `int` `minimumLength(String s)  ` `{  ` `    ``int` `maxOcc = ``0``, n = s.length();  ` `    ``int` `arr[] = ``new` `int``[``26``];  ` ` `  `    ``// Count the frequency of each alphabet  ` `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``arr[s.charAt(i) - ``'a'``]++;  ` ` `  `    ``// Find the alphabets with maximum frequency  ` `    ``for` `(``int` `i = ``0``; i < ``26``; i++)  ` `        ``if` `(arr[i] > maxOcc)  ` `            ``maxOcc = arr[i];  ` ` `  `    ``// Subtract the frequency of character  ` `    ``// from length of string  ` `    ``return` `(n - maxOcc);  ` `}  ` ` `  `// Driver Code  ` ` `  `    ``public` `static` `void` `main (String[] args) { ` `    ``String str = ``"afddewqd"``;  ` `    ``System.out.println( minimumLength(str)); ` `    ``} ` `} ` ` `  `//This code is contributed by chandan_jnu... `

## Python 3

 `# Python 3 program to minimize the length of string  ` `# by removing occurrence of only one character ` ` `  `# Function to find the minimum length ` `def` `minimumLength(s) : ` ` `  `    ``maxOcc ``=` `0` `    ``n ``=` `len``(s) ` `    ``arr ``=` `[``0``]``*``26` ` `  `    ``# Count the frequency of each alphabet  ` `    ``for` `i ``in` `range``(n) : ` `        ``arr[``ord``(s[i]) ``-``ord``(``'a'``)] ``+``=` `1` ` `  `    ``# Find the alphabets with maximum frequency ` `    ``for` `i ``in` `range``(``26``) : ` `        ``if` `arr[i] > maxOcc : ` `            ``maxOcc ``=` `arr[i] ` ` `  `    ``# Subtract the frequency of character  ` `    ``# from length of string  ` `    ``return` `n ``-` `maxOcc ` ` `  ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``str` `=` `"afddewqd"` `    ``print``(minimumLength(``str``)) ` ` `  ` `  `# This code is contributed by ANKITRAI1 `

## C#

 `// C# program to minimize the ` `// length of string by removing ` `// occurrence of only one character ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `    ``// Function to find the minimum length  ` `    ``static` `int` `minimumLength(String s)  ` `    ``{  ` `        ``int` `maxOcc = 0, n = s.Length;  ` `        ``int` `[]arr = ``new` `int``;  ` ` `  `        ``// Count the frequency of each alphabet  ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `            ``arr[s[i] - ``'a'``]++;  ` ` `  `        ``// Find the alphabets with  ` `        ``// maximum frequency  ` `        ``for` `(``int` `i = 0; i < 26; i++)  ` `            ``if` `(arr[i] > maxOcc)  ` `                ``maxOcc = arr[i];  ` ` `  `        ``// Subtract the frequency of character  ` `        ``// from length of string  ` `        ``return` `(n - maxOcc);  ` `    ``}  ` ` `  `    ``// Driver Code  ` `    ``public` `static` `void` `Main (String[] args)  ` `    ``{ ` `        ``String str = ``"afddewqd"``;  ` `        ``Console.WriteLine( minimumLength(str)); ` `    ``} ` `} ` ` `  `// This code is contributed by ` `// PrinciRaj1992 `

Output:

```5
```

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.