Remove odd frequency characters from the string
Given string str of size N, the task is to remove all the characters from the string that have odd frequencies.
Examples:
Input: str = “geeksforgeeks”
Output: geeksgeeks
The characters f, o, r have odd frequencies
So, they are removed from the string.
Input: str = “zzzxxweeerr”
Output: xxrr
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 odd frequencies with the help of the map.
- Ignore all those characters which have odd 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;
string removeOddFrequencyCharacters(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]] & 1)
continue ;
new_string += s[i];
}
return new_string;
}
int main()
{
string str = "geeksforgeeks" ;
str = removeOddFrequencyCharacters(str);
cout << str << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String removeOddFrequencyCharacters(String s)
{
HashMap<Character, Integer> m = new HashMap<Character,Integer>();
for ( int i = 0 ; i < s.length(); i++) {
char p = s.charAt(i);
Integer count = m.get(p);
if ( count == null )
{
count= 0 ;
m.put(p, 1 );
}
else
m.put(p,count + 1 );
}
String new_string = "" ;
for ( int i = 0 ; i < s.length(); i++) {
if ((m.get(s.charAt(i))& 1 )== 1 )
continue ;
new_string += s.charAt(i);
}
return new_string;
}
public static void main(String []args)
{
String str = "geeksforgeeks" ;
str = removeOddFrequencyCharacters(str);
System.out.print(str);
}
}
|
Python3
def removeOddFrequencyCharacters(s):
m = dict ()
for i in s:
m[i] = m.get(i, 0 ) + 1
new_s = ""
for i in s:
if (m[i] & 1 ):
continue
new_s + = i
return new_s
if __name__ = = '__main__' :
str = "geeksforgeeks"
str = removeOddFrequencyCharacters( str )
print ( str )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static string removeOddFrequencyCharacters( string s)
{
Dictionary< char ,
int > m = new Dictionary< char ,
int >();
for ( int i = 0; i < s.Length; i++)
{
char p = s[i];
if (m.ContainsKey(p))
{
m[p]++;
}
else
{
m[p] = 1;
}
}
string new_string = "" ;
for ( int i = 0; i < s.Length; i++)
{
if ((m[s[i]] & 1) == 1)
continue ;
new_string += s[i];
}
return new_string;
}
public static void Main( string []args)
{
string str = "geeksforgeeks" ;
str = removeOddFrequencyCharacters(str);
Console.Write(str);
}
}
|
Javascript
<script>
function removeOddFrequencyCharacters(s)
{
let m = new Map();
for (let i = 0; i < s.length; i++) {
let p = s[i];
let count = m.get(p);
if ( count == null )
{
count=0;
m.set(p,1);
}
else
m.set(p,count + 1);
}
let new_string = "" ;
for (let i = 0; i < s.length; i++) {
if ((m.get(s[i])& 1)==1)
continue ;
new_string += s[i];
}
return new_string;
}
let str = "geeksforgeeks" ;
str = removeOddFrequencyCharacters(str);
document.write(str);
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n)
Approach: Bit Manipulation
Steps for the above approach:
- Create a bit vector of size 26 (assuming the string contains only lowercase alphabets) to store the parity of each character’s frequency.
- Traverse the string and toggle the corresponding bit in the bit vector for each character.
- Iterate over the string again and append the characters to a new result string based on the bit value in the bit vector.
- Return the result string as the output.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <string>
using namespace std;
string removeCharactersWithOddFrequencies(string str) {
int bitVector = 0;
for ( char character : str) {
int index = character - 'a' ;
bitVector ^= (1 << index);
}
string result = "" ;
for ( char character : str) {
int index = character - 'a' ;
if (!(bitVector & (1 << index))) {
result += character;
}
}
return result;
}
int main() {
string input_str = "geeksforgeeks" ;
string output_str = removeCharactersWithOddFrequencies(input_str);
cout << output_str << endl;
return 0;
}
|
Java
public class RemoveCharactersWithOddFrequencies {
static String
removeCharactersWithOddFrequencies(String str)
{
int bitVector = 0 ;
for ( char character : str.toCharArray()) {
int index = character - 'a' ;
bitVector ^= ( 1 << index);
}
StringBuilder result = new StringBuilder();
for ( char character : str.toCharArray()) {
int index = character - 'a' ;
if ((bitVector & ( 1 << index))
== 0 ) {
result.append(character);
}
}
return result.toString();
}
public static void main(String[] args)
{
String input_str = "geeksforgeeks" ;
String output_str
= removeCharactersWithOddFrequencies(input_str);
System.out.println(output_str);
}
}
|
Python
def removeCharactersWithOddFrequencies( str ):
bitVector = 0
for character in str :
index = ord (character) - ord ( 'a' )
bitVector ^ = ( 1 << index)
result = ""
for character in str :
index = ord (character) - ord ( 'a' )
if not (bitVector & ( 1 << index)):
result + = character
return result
input_str = "geeksforgeeks"
output_str = removeCharactersWithOddFrequencies(input_str)
print (output_str)
|
C#
using System;
namespace RemoveCharactersWithOddFrequencies
{
class Program
{
static string RemoveCharactersWithOddFrequencies( string str)
{
int bitVector = 0;
foreach ( char character in str)
{
int index = character - 'a' ;
bitVector ^= (1 << index);
}
string result = "" ;
foreach ( char character in str)
{
int index = character - 'a' ;
if ((bitVector & (1 << index)) == 0)
{
result += character;
}
}
return result;
}
static void Main( string [] args)
{
string input_str = "geeksforgeeks" ;
string output_str = RemoveCharactersWithOddFrequencies(input_str);
Console.WriteLine(output_str);
Console.ReadKey();
}
}
}
|
Javascript
function removeCharactersWithOddFrequencies(str) {
let bitVector = 0;
for (let i = 0; i < str.length; i++) {
let character = str[i];
let index = character.charCodeAt(0) - 'a' .charCodeAt(0);
bitVector ^= (1 << index);
}
let result = "" ;
for (let i = 0; i < str.length; i++) {
let character = str[i];
let index = character.charCodeAt(0) - 'a' .charCodeAt(0);
if (!(bitVector & (1 << index))) {
result += character;
}
}
return result;
}
let input_str = "geeksforgeeks" ;
let output_str = removeCharactersWithOddFrequencies(input_str);
console.log(output_str);
|
Output:
geeksgeeks
Time Complexity: O(N), where N is the length of the input string.
Auxiliary Space: O(1)
Last Updated :
04 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...