📜  从字符串删除奇数频率字符

📅  最后修改于: 2021-05-31 22:18:06             🧑  作者: Mango

给定大小为N的字符串str ,任务是从字符串中删除所有具有奇数频率的字符。

例子:

方法:

  • 创建一个映射,并存储从字符串到同一映射的每个字符的频率。
  • 然后,遍历字符串,并借助地图找出哪些字符具有奇数频率。
  • 忽略所有具有奇数频率的字符,并将其余字符存储在新字符串。
  • 最后,显示新字符串。

下面是上述方法的实现:

C++
// C++ program to remove the characters
// having odd frequencies in the string
#include 
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 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 m = new HashMap();
        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 m = new Dictionary();
     
    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


输出:
geeksgeeks


想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”