Eliminar caracteres de frecuencia impar de la string

Dada la string str de tamaño N , la tarea es eliminar todos los caracteres de la string que tienen frecuencias impares.

Ejemplos: 

Entrada: str = “geeksforgeeks” 
Salida: geeksgeeks 
Los caracteres f, o, r tienen frecuencias impares 
, por lo que se eliminan de la string.

Entrada: str = “zzzxxweeerr” 
Salida: xxrr 

Acercarse: 

  • Cree un mapa y almacene la frecuencia de cada carácter de la string en el mismo mapa.
  • Luego, recorra la string y descubra qué caracteres tienen frecuencias impares con la ayuda del mapa.
  • Ignore todos aquellos caracteres que tengan frecuencias impares y almacene el resto en una nueva string.
  • Finalmente, muestre la nueva string.

A continuación se muestra la implementación del enfoque anterior: 

C++

// C++ program to remove the characters
// having odd frequencies in the string
#include <bits/stdc++.h>
using namespace std;
 
// Function to remove the characters which
// have odd frequencies in the string
string removeOddFrequencyCharacters(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 odd frequencies
    for (int i = 0; i < s.length(); i++) {
 
        // If the character has
        // odd frequency then skip
        if (m[s[i]] & 1)
            continue;
 
        // Else concatenate the
        // character to the new string
        new_string += s[i];
    }
 
    // Return the modified string
    return new_string;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
 
    // Remove the characters which
    // have odd frequencies
    str = removeOddFrequencyCharacters(str);
    cout << str << "\n";
 
    return 0;
}

Java

// Java program to remove the characters
// having odd frequencies in the string
import java.util.*;
 
class GFG
{
      
    // Function to remove the characters which
    // have odd frequencies in the string
    static String removeOddFrequencyCharacters(String s)
    {
        // Create a map to store the
        // frequency of each character
        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);
        }
      
        // To store the new string
        String new_string = "";
      
        // Remove the characters which
        // have odd frequencies
        for (int i = 0; i < s.length(); i++) {
      
            // If the character has
            // odd frequency then skip
            if ((m.get(s.charAt(i))& 1)==1)
                continue;
      
            // Else concatenate the
            // character to the new string
            new_string += s.charAt(i);
        }
      
        // Return the modified string
        return new_string;
    }
      
    // Driver code
    public static void main(String []args)
    {
        String str = "geeksforgeeks";
      
        // Remove the characters which
        // have odd frequencies
        str = removeOddFrequencyCharacters(str);
        System.out.print(str);
    }
}
 
// This is contributed by chitranayal

Python3

# Python3 program to remove the characters
# having odd frequencies in the string
 
# Function to remove the characters which
# have odd frequencies in the string
def removeOddFrequencyCharacters(s):
     
    # Create a map to store the
    # frequency of each character
    m = dict()
    for i in s:
        m[i] = m.get(i, 0) + 1
 
    # To store the new string
    new_s = ""
 
    # Remove the characters which
    # have odd frequencies
    for i in s:
 
        # If the character has
        # odd frequency then skip
        if (m[i] & 1):
            continue
 
        # Else concatenate the
        # character to the new string
        new_s += i
 
    # Return the modified string
    return new_s
 
# Driver code
if __name__ == '__main__':
    str = "geeksforgeeks"
 
    # Remove the characters which
    # have odd frequencies
    str = removeOddFrequencyCharacters(str)
    print(str)
 
# This code is contributed by mohit kumar 29

C#

// C# program to remove the characters
// having odd frequencies in the string
using System;
using System.Collections.Generic;
 
class GFG{
       
// Function to remove the characters which
// have odd frequencies in the string
static string removeOddFrequencyCharacters(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++)
    {
        char p = s[i];
         
        if (m.ContainsKey(p))
        {
            m[p]++;
        }
        else
        {
            m[p] = 1;
        }
    }
   
    // To store the new string
    string new_string = "";
   
    // Remove the characters which
    // have odd frequencies
    for(int i = 0; i < s.Length; i++)
    {
         
        // If the character has
        // odd frequency then skip
        if ((m[s[i]] & 1) == 1)
            continue;
   
        // Else concatenate the
        // character to the new string
        new_string += s[i];
    }
   
    // Return the modified string
    return new_string;
}
   
// Driver code
public static void Main(string []args)
{
    string str = "geeksforgeeks";
   
    // Remove the characters which
    // have odd frequencies
    str = removeOddFrequencyCharacters(str);
     
    Console.Write(str);
}
}
 
// This code is contributed by rutvik_56

Javascript

<script>
 
// Javascript program to remove the characters
// having odd frequencies in the string
 
    // Function to remove the characters which
    // have odd frequencies in the string
    function removeOddFrequencyCharacters(s)
    {
        // Create a map to store the
        // frequency of each character
        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);
        }
       
        // To store the new string
        let new_string = "";
       
        // Remove the characters which
        // have odd frequencies
        for (let i = 0; i < s.length; i++) {
       
            // If the character has
            // odd frequency then skip
            if ((m.get(s[i])& 1)==1)
                continue;
       
            // Else concatenate the
            // character to the new string
            new_string += s[i];
        }
       
        // Return the modified string
        return new_string;
    }
 
// Driver code
     
      let str = "geeksforgeeks";
       
        // Remove the characters which
        // have odd frequencies
        str = removeOddFrequencyCharacters(str);
        document.write(str);
                                                                                 
</script>
Producción: 

geeksgeeks

 

Publicación traducida automáticamente

Artículo escrito por muskan_garg y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *