Given a string ‘str’, the task is to remove all the characters from the string that have even frequencies.
Examples:
Input: str = "aabbbddeeecc" Output: bbbeee The characters a, d, c have even frequencies So, they are removed from the string. Input: str = "zzzxxweeerr" Output: zzzweee
Approach:
- Create a map and store the frequency of each character from the string to the same map.
- Then, traverse the string and find out which characters have even frequencies with the help of the map.
- Ignore all those characters which have even frequencies and store the rest in a new string.
- Finally, display the new string.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that removes the // characters which have even // frequencies in the string void solve(string s) { // create a map to store the // frequency of each character unordered_map<char, int> m; for (int i = 0; i < s.length(); i++) { m[s[i]]++; } // to store the new string string new_string = ""; // remove the characters which // have even frequencies for (int i = 0; i < s.length(); i++) { // if the character has // even frequency then skip if (m[s[i]] % 2 == 0) continue; // else concatenate the // character to the new string new_string += s[i]; } // display the modified string cout << new_string << endl; } // Driver code int main() { string s = "aabbbddeeecc"; // remove the characters which // have even frequencies solve(s); return 0; } |
chevron_right
filter_none
Java
// Java implementation of the approach import java.util.*; class GFG { // Function that removes the // characters which have even // frequencies in the string static void solve(String s) { // create a map to store the // frequency of each character HashMap<Character, Integer> m = new HashMap<>(); for (int i = 0; i < s.length(); i++) { if(m.containsKey(s.charAt(i))) m.put(s.charAt(i), m.get(s.charAt(i)) + 1); else m.put(s.charAt(i), 1); } // to store the new string String new_string = ""; // remove the characters which // have even frequencies for (int i = 0; i < s.length(); i++) { // if the character has // even frequency then skip if (m.get(s.charAt(i)) % 2 == 0) continue; // else concatenate the // character to the new string new_string = new_string + s.charAt(i); } // display the modified string System.out.println(new_string); } // Driver code public static void main(String []args) { String s = "aabbbddeeecc"; // remove the characters which // have even frequencies solve(s); } } // This code is contributed by ihritik |
chevron_right
filter_none
Python3
# Python3 implementation of # above approach # Function that removes the # characters which have even # frequencies in the string def solve(s): # create a map to store the # frequency of each character m = dict() for i in range(len(s)): if s[i] in m: m[s[i]] = m[s[i]]+1 else: m[s[i]] = 1 # to store the new string new_string = "" # remove the characters which # have even frequencies for i in range(len(s)): # if the character has # even frequency then skip if m[s[i]]%2 == 0: continue # else concatenate the # character to the new string new_string = new_string+s[i] # display the modified string print(new_string) #Driver code if __name__=='__main__': s = "aabbbddeeecc" # remove the characters which # have even frequencies solve(s) # this code is contributed by # Shashank_Sharma |
chevron_right
filter_none
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { // Function that removes the // characters which have even // frequencies in the string static void solve(String s) { // create a map to store the // frequency of each character Dictionary<char, int> m = new Dictionary<char, int>(); for (int i = 0; i < s.Length; i++) { if(m.ContainsKey(s[i])) { var val = m[s[i]]; m.Remove(s[i]); m.Add(s[i], val + 1); } else m.Add(s[i], 1); } // to store the new string String new_string = ""; // remove the characters which // have even frequencies for (int i = 0; i < s.Length; i++) { // if the character has // even frequency then skip if (m[s[i]] % 2 == 0) continue; // else concatenate the // character to the new string new_string = new_string + s[i]; } // display the modified string Console.WriteLine(new_string); } // Driver code public static void Main(String []args) { String s = "aabbbddeeecc"; // remove the characters which // have even frequencies solve(s); } } // This code has been contributed by 29AjayKumar |
chevron_right
filter_none
Output:
bbbeee
Time Complexity: O(N)
Auxiliary Space: O(N)
Recommended Posts:
- Remove odd frequency characters from the string
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Program to check if all characters have even frequency
- Minimum characters to be replaced to make frequency of all characters same
- Remove characters from the first string which are present in the second string
- Check if a string has all characters with same frequency with one variation allowed
- String with frequency of characters in Lucas Sequence
- Check whether frequency of characters in a string makes Fibonacci Sequence
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Number of Positions to partition the string such that atleast m characters with same frequency are present in each substring
- Maximum repeated frequency of characters in a given string
- Sort a string according to the frequency of characters
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Count of Binary Strings of length N such that frequency of 1's exceeds frequency of 0's
- Remove all characters other than alphabets from string
- Remove all non-alphabetical characters of a String in Java
- How to remove all non-alphanumeric characters from a string in Java
- Remove characters from string that appears strictly less than K times
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.


