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

📅  最后修改于: 2021-10-27 08:08:35             🧑  作者: Mango

给定一个字符串“str”,任务是从字符串中删除所有频率为偶数的字符。

例子:

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

方法:

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

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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 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;
}


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 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


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


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 m = new Dictionary();
         
        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


Javascript


Python3
# Python3 implementation of
# above approach
from collections import Counter
 
# Function that removes the
# characters which have even
# frequencies in the string
def removeEven(s):
 
    # Calculate the frequency using Counter function
    # to store the new string
    m = Counter(s)
    new_string = ""
 
    # Remove the characters which
    # have even frequencies
    for i in range(len(s)):
        if(m[s[i]] % 2 != 0):
           
            # 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
    removeEven(s)
 
# this code is contributed by vikkycirus


输出:

bbbeee

时间复杂度: O(N)
辅助空间: O(N)

方法 #2:使用内置Python函数

  • 使用Counter()函数计算所有字符的频率。
  • 然后,遍历字符串并借助映射找出哪些字符具有偶数频率。
  • 忽略所有具有偶数频率的字符并将其余字符存储在一个新字符串。
  • 最后,显示新字符串。

下面是实现:

蟒蛇3

# Python3 implementation of
# above approach
from collections import Counter
 
# Function that removes the
# characters which have even
# frequencies in the string
def removeEven(s):
 
    # Calculate the frequency using Counter function
    # to store the new string
    m = Counter(s)
    new_string = ""
 
    # Remove the characters which
    # have even frequencies
    for i in range(len(s)):
        if(m[s[i]] % 2 != 0):
           
            # 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
    removeEven(s)
 
# this code is contributed by vikkycirus

输出:

bbbeee

时间复杂度: O(N)

辅助空间: O(N)