Print characters having odd frequencies in order of occurrence
Given a string str containing only lowercase characters. The task is to print the characters having an odd frequency in the order of their occurrence.
Note: Repeated elements with odd frequency are printed as many times they occur in order of their occurrences.
Examples:
Input: str = “w3wiki”
Output: for
Character Frequency ‘g’ 2 ‘e’ 4 ‘k’ 2 ‘s’ 2 ‘f’ 1 ‘o’ 1 ‘r’ 1 ‘f’, ‘o’ and ‘r’ are the only characters with odd frequencies.
Input: str = “elephant”
Output: lphant
Approach: Create a frequency array to store the frequency of each of the characters of the given string str. Traverse the string str again and check whether the frequency of that character is odd. If yes, then print the character.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define SIZE 26 // Function to print the odd frequency characters // in the order of their occurrence void printChar(string str, int n) { // To store the frequency of each of // the character of the string int freq[SIZE]; // Initialize all elements of freq[] to 0 memset (freq, 0, sizeof (freq)); // Update the frequency of each character for ( int i = 0; i < n; i++) freq[str[i] - 'a' ]++; // Traverse str character by character for ( int i = 0; i < n; i++) { // If frequency of current character is odd if (freq[str[i] - 'a' ] % 2 == 1) { cout << str[i]; } } } // Driver code int main() { string str = "w3wiki" ; int n = str.length(); printChar(str, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to print the odd frequency characters // in the order of their occurrence public static void printChar(String str, int n) { // To store the frequency of each of // the character of the string int [] freq = new int [ 26 ]; // Update the frequency of each character for ( int i = 0 ; i < n; i++) freq[str.charAt(i) - 'a' ]++; // Traverse str character by character for ( int i = 0 ; i < n; i++) { // If frequency of current character is odd if (freq[str.charAt(i) - 'a' ] % 2 == 1 ) { System.out.print(str.charAt(i)); } } } // Driver code public static void main(String[] args) { String str = "w3wiki" ; int n = str.length(); printChar(str, n); } } // This code is contributed by Naman_Garg. |
Python3
# Python3 implementation of the approach import sys import math # Function to print the odd frequency characters # in the order of their occurrence def printChar(str_, n): # To store the frequency of each of # the character of the string and # Initialize all elements of freq[] to 0 freq = [ 0 ] * 26 # Update the frequency of each character for i in range (n): freq[ ord (str_[i]) - ord ( 'a' )] + = 1 # Traverse str character by character for i in range (n): # If frequency of current character is odd if (freq[ ord (str_[i]) - ord ( 'a' )]) % 2 = = 1 : print ( "{}" . format (str_[i]), end = "") # Driver code if __name__ = = '__main__' : str_ = "w3wiki" n = len (str_) printChar(str_, n) # This code is contributed by Vikash Kumar 37 |
C#
// C# implementation of the approach using System; class GFG { // Function to print the odd frequency characters // in the order of their occurrence public static void printChar(String str, int n) { // To store the frequency of each of // the character of the string int [] freq = new int [26]; // Update the frequency of each character for ( int i = 0; i < n; i++) freq[str[i] - 'a' ]++; // Traverse str character by character for ( int i = 0; i < n; i++) { // If frequency of current character is odd if (freq[str[i] - 'a' ] % 2 == 1) { Console.Write(str[i]); } } } // Driver code public static void Main(String[] args) { String str = "w3wiki" ; int n = str.Length; printChar(str, n); } } // This code has been contributed by 29AjayKumar |
Javascript
<script> // javascript implementation of the approach let SIZE = 26; // Function to print the odd frequency characters // in the order of their occurrence function printChar(str, n) { // To store the frequency of each of // the character of the string let freq = []; // Initialize all elements of freq[] to 0 for (let i = 0; i < SIZE; i++){ freq.push(0); } // Update the frequency of each character for (let i = 0; i < n; i++) freq[str.charCodeAt(i) - 97]++; // Traverse str character by character for (let i = 0; i < n; i++) { // If frequency of current character is odd if (freq[str.charCodeAt(i) - 97] % 2 == 1) { document.write(str[i]); } } } let str = "w3wiki" ; let n = str.length; printChar(str, n); // This code is contributed by rohitsingh07052. </script> |
Output:
for
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2: Using built-in python functions.
Approach:
We will scan the string and count the occurrence of all characters using built in Counter() function after that we traverse the counter list and check if the occurrences are odd or not if there is any even frequency character then we immediately print No.
Note: This method is applicable for all type of characters
Below is the implementation of the above approach:
C++
#include <iostream> #include <string> #include <unordered_map> using namespace std; // Function to check if all elements occur an odd number of // times bool checkString(string s) { // Counting the frequency of all characters unordered_map< char , int > frequency; // Traversing the string to calculate frequencies for ( char c : s) { frequency++; } // Checking if any element has even count for ( const auto & pair : frequency) { if (pair.second % 2 == 0) { return false ; } } return true ; } // Driver code int main() { string s = "gggfffaaa" ; if (checkString(s)) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; } |
Java
import java.util.*; public class CheckString { // Function to check if all elements occur odd times public static boolean checkString(String s) { // Counting the frequency of all characters Map<Character, Integer> frequency = new HashMap<>(); // Traversing the string to calculate frequencies for ( char c : s.toCharArray()) { frequency.put(c, frequency.getOrDefault(c, 0 ) + 1 ); } // Checking if any element has even count for ( char c : frequency.keySet()) { if (frequency.get(c) % 2 == 0 ) { return false ; } } return true ; } // Driver code public static void main(String[] args) { String s = "gggfffaaa" ; if (checkString(s)) { System.out.println( "Yes" ); } else { System.out.println( "No" ); } } } |
Python3
# importing Counter function from collections import Counter # function to check if all # elements occur odd times def checkString(s): # Counting the frequency of all # character using Counter function frequency = Counter(s) # Traversing frequency for i in frequency: # Checking if any element # has even count if (frequency[i] % 2 = = 0 ): return False return True # Driver code s = "gggfffaaa" if (checkString(s)): print ( "Yes" ) else : print ( "No" ) # This code is contributed by vikkycirus |
C#
using System; using System.Collections.Generic; class Program { // Function to check if all elements occur an odd number // of times static bool CheckString( string s) { // Counting the frequency of all characters Dictionary< char , int > frequency = new Dictionary< char , int >(); // Traversing the string to calculate frequencies foreach ( char c in s) { if (frequency.ContainsKey(c)) { frequency++; } else { frequency = 1; } } // Checking if any element has even count foreach ( var pair in frequency) { if (pair.Value % 2 == 0) { return false ; } } return true ; } static void Main( string [] args) { string s = "gggfffaaa" ; if (CheckString(s)) { Console.WriteLine( "Yes" ); } else { Console.WriteLine( "No" ); } } } |
Javascript
// Javascript program for the above approach // function to check if all // elements occur odd times function checkString(s) { // Counting the frequency of all // character using Counter function let frequency = {}; for (let i = 0; i < s.length; i++) { if (frequency[s[i]]) { frequency[s[i]]++; } else { frequency[s[i]] = 1; } } // Traversing frequency for (let i in frequency) { // Checking if any element // has even count if (frequency[i] % 2 === 0){ return false ; } } return true ; } // Driver code let s = "gggfffaaa" ; if (checkString(s)){ console.log( "Yes" ); } else { console.log( "No" ); } // contributed by adityasharmadev01 |
Output:
Yes