Remove even frequency characters from the string
Last Updated :
21 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void solve(string s)
{
unordered_map< char , int > m;
for ( int i = 0; i < s.length(); i++) {
m[s[i]]++;
}
string new_string = "" ;
for ( int i = 0; i < s.length(); i++) {
if (m[s[i]] % 2 == 0)
continue ;
new_string += s[i];
}
cout << new_string << endl;
}
int main()
{
string s = "aabbbddeeecc" ;
solve(s);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void solve(String s)
{
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 );
}
String new_string = "" ;
for ( int i = 0 ; i < s.length(); i++)
{
if (m.get(s.charAt(i)) % 2 == 0 )
continue ;
new_string = new_string + s.charAt(i);
}
System.out.println(new_string);
}
public static void main(String []args)
{
String s = "aabbbddeeecc" ;
solve(s);
}
}
|
Python3
def solve(s):
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
new_string = ""
for i in range ( len (s)):
if m[s[i]] % 2 = = 0 :
continue
new_string = new_string + s[i]
print (new_string)
if __name__ = = '__main__' :
s = "aabbbddeeecc"
solve(s)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void solve(String s)
{
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);
}
String new_string = "" ;
for ( int i = 0; i < s.Length; i++)
{
if (m[s[i]] % 2 == 0)
continue ;
new_string = new_string + s[i];
}
Console.WriteLine(new_string);
}
public static void Main(String []args)
{
String s = "aabbbddeeecc" ;
solve(s);
}
}
|
Javascript
<script>
function solve(s)
{
let m = new Map();
for (let i = 0; i < s.length; i++)
{
if (m.has(s[i]))
m.set(s[i],
m.get(s[i]) + 1);
else
m.set(s[i], 1);
}
let new_string = "" ;
for (let i = 0; i < s.length; i++)
{
if (m.get(s[i]) % 2 == 0)
continue ;
new_string = new_string + s[i];
}
document.write(new_string);
}
let s = "aabbbddeeecc" ;
solve(s);
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(N)
Method #2: Using Built-in Python Functions
- Calculate the frequency of all characters using Counter() function.
- 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:
C++
#include <iostream>
#include <unordered_map>
using namespace std;
void removeEven(string s)
{
unordered_map< char , int > freq;
for ( int i = 0; i < s.length(); i++) {
char ch = s[i];
freq[ch]++;
}
string newString = "" ;
for ( int i = 0; i < s.length(); i++) {
char ch = s[i];
if (freq[ch] % 2 != 0) {
newString += ch;
}
}
cout << newString << endl;
}
int main() {
string s = "aabbbddeeecc" ;
removeEven(s);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
public class Main {
static void removeEven(String s) {
Map<Character, Integer> freq = new HashMap<>();
for ( int i = 0 ; i < s.length(); i++) {
char ch = s.charAt(i);
freq.put(ch, freq.getOrDefault(ch, 0 ) + 1 );
}
StringBuilder newString = new StringBuilder();
for ( int i = 0 ; i < s.length(); i++) {
char ch = s.charAt(i);
if (freq.get(ch) % 2 != 0 ) {
newString.append(ch);
}
}
System.out.println(newString.toString());
}
public static void main(String[] args) {
String s = "aabbbddeeecc" ;
removeEven(s);
}
}
|
Python3
from collections import Counter
def removeEven(s):
m = Counter(s)
new_string = ""
for i in range ( len (s)):
if (m[s[i]] % 2 ! = 0 ):
new_string = new_string + s[i]
print (new_string)
if __name__ = = '__main__' :
s = "aabbbddeeecc"
removeEven(s)
|
Javascript
function removeEven(s)
{
let m = new Map();
let new_string = "" ;
for (let i = 0; i < s.length; i++) {
if (m.has(s[i])) {
m.set(s[i], m.get(s[i]) + 1);
} else {
m.set(s[i], 1);
}
}
for (let i = 0; i < s.length; i++)
{
if (m.get(s[i]) % 2 != 0) {
new_string = new_string + s[i];
}
}
console.log(new_string);
}
let s = "aabbbddeeecc" ;
removeEven(s);
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
static void RemoveEven( string s)
{
Dictionary< char , int > freq
= new Dictionary< char , int >();
for ( int i = 0; i < s.Length; i++) {
char ch = s[i];
if (freq.ContainsKey(ch)) {
freq[ch]++;
}
else {
freq[ch] = 1;
}
}
string newString = "" ;
for ( int i = 0; i < s.Length; i++) {
char ch = s[i];
if (freq[ch] % 2 != 0) {
newString += ch;
}
}
Console.WriteLine(newString);
}
public static void Main( string [] args)
{
string s = "aabbbddeeecc" ;
RemoveEven(s);
}
}
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...