Print characters having odd frequencies in order of occurrence
Last Updated :
28 Oct, 2023
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 = “geeksforgeeks”
Output: for
‘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++
#include <bits/stdc++.h>
using namespace std;
#define SIZE 26
void printChar(string str, int n)
{
int freq[SIZE];
memset (freq, 0, sizeof (freq));
for ( int i = 0; i < n; i++)
freq[str[i] - 'a' ]++;
for ( int i = 0; i < n; i++) {
if (freq[str[i] - 'a' ] % 2 == 1) {
cout << str[i];
}
}
}
int main()
{
string str = "geeksforgeeks" ;
int n = str.length();
printChar(str, n);
return 0;
}
|
Java
class GFG {
public static void printChar(String str, int n)
{
int [] freq = new int [ 26 ];
for ( int i = 0 ; i < n; i++)
freq[str.charAt(i) - 'a' ]++;
for ( int i = 0 ; i < n; i++) {
if (freq[str.charAt(i) - 'a' ] % 2 == 1 ) {
System.out.print(str.charAt(i));
}
}
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
int n = str.length();
printChar(str, n);
}
}
|
Python3
import sys
import math
def printChar(str_, n):
freq = [ 0 ] * 26
for i in range (n):
freq[ ord (str_[i]) - ord ( 'a' )] + = 1
for i in range (n):
if (freq[ ord (str_[i]) -
ord ( 'a' )]) % 2 = = 1 :
print ( "{}" . format (str_[i]), end = "")
if __name__ = = '__main__' :
str_ = "geeksforgeeks"
n = len (str_)
printChar(str_, n)
|
C#
using System;
class GFG {
public static void printChar(String str, int n)
{
int [] freq = new int [26];
for ( int i = 0; i < n; i++)
freq[str[i] - 'a' ]++;
for ( int i = 0; i < n; i++) {
if (freq[str[i] - 'a' ] % 2 == 1) {
Console.Write(str[i]);
}
}
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
int n = str.Length;
printChar(str, n);
}
}
|
Javascript
<script>
let SIZE = 26;
function printChar(str, n)
{
let freq = [];
for (let i = 0; i < SIZE; i++){
freq.push(0);
}
for (let i = 0; i < n; i++)
freq[str.charCodeAt(i) - 97]++;
for (let i = 0; i < n; i++) {
if (freq[str.charCodeAt(i) - 97] % 2 == 1) {
document.write(str[i]);
}
}
}
let str = "geeksforgeeks" ;
let n = str.length;
printChar(str, n);
</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;
bool checkString(string s)
{
unordered_map< char , int > frequency;
for ( char c : s) {
frequency++;
}
for ( const auto & pair : frequency) {
if (pair.second % 2 == 0) {
return false ;
}
}
return true ;
}
int main()
{
string s = "gggfffaaa" ;
if (checkString(s)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.util.*;
public class CheckString {
public static boolean checkString(String s) {
Map<Character, Integer> frequency = new HashMap<>();
for ( char c : s.toCharArray()) {
frequency.put(c, frequency.getOrDefault(c, 0 ) + 1 );
}
for ( char c : frequency.keySet()) {
if (frequency.get(c) % 2 == 0 ) {
return false ;
}
}
return true ;
}
public static void main(String[] args) {
String s = "gggfffaaa" ;
if (checkString(s)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
from collections import Counter
def checkString(s):
frequency = Counter(s)
for i in frequency:
if (frequency[i] % 2 = = 0 ):
return False
return True
s = "gggfffaaa"
if (checkString(s)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class Program {
static bool CheckString( string s)
{
Dictionary< char , int > frequency
= new Dictionary< char , int >();
foreach ( char c in s)
{
if (frequency.ContainsKey(c)) {
frequency++;
}
else {
frequency = 1;
}
}
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
function checkString(s) {
let frequency = {};
for (let i = 0; i < s.length; i++) {
if (frequency[s[i]]) {
frequency[s[i]]++;
} else {
frequency[s[i]] = 1;
}
}
for (let i in frequency) {
if (frequency[i] % 2 === 0){
return false ;
}
}
return true ;
}
let s = "gggfffaaa" ;
if (checkString(s)){
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Output:
Yes
Share your thoughts in the comments
Please Login to comment...