# 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

**Approach:**

- Maintain the frequency of each and every character in an array.
- Find the character with the maximum frequency.
- 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 <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimum length ` `int` `minimumLength(string s) ` `{ ` ` ` `int` `maxOcc = 0, n = s.length(); ` ` ` `int` `arr[26] = {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; ` `} ` |

*chevron_right*

*filter_none*

## 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... ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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` `[26]; ` ` ` ` ` `// 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 ` |

*chevron_right*

*filter_none*

**Output:**

5

## Recommended Posts:

- Minimize ASCII values sum after removing all occurrences of one character
- Program to count occurrence of a given character in a string
- Count occurrence of a given character in a string using Stream API in Java
- Character replacement after removing duplicates from a string
- Generate two output strings depending upon occurrence of character in input string.
- Lexicographically smallest string formed by removing at most one character
- Generate two output strings depending upon occurrence of character in input string in Python
- Minimum sum of squares of character counts in a given string after removing k characters
- Check if the frequency of any character is more than half the length of the string
- Rearrange given string to maximize the occurrence of string t
- Replace every character of string by character whose ASCII value is K times more than it
- Find a string such that every character is lexicographically greater than its immediate next character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Map every character of one string to another such that all occurrences are mapped to the same character

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.