Print the string by ignoring alternate occurrences of any character
Given a string of both uppercase and lowercase alphabets, the task is to print the string with alternate occurrences of any character dropped(including space and consider upper and lowercase as same).
Examples:
Input : It is a long day Dear.
Output : It sa longdy ear.
Print first I and then ignore next i.
Similarly print first space then
ignore next space.
Input : Geeks for geeks
Output : Geks fore
Asked in: Microsoft
As we have to print characters in an alternate manner, so start traversing the string and perform following two steps:-
- Increment the count of occurrence of current character in a hash table.
- Check if the count becomes odd, then print the current character, else not.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void printStringAlternate(string str)
{
unordered_map< char , int > occ;
for ( int i = 0; i < str.length(); i++) {
char temp = tolower (str[i]);
occ[temp]++;
if (occ[temp] & 1)
cout << str[i];
}
cout << endl;
}
int main()
{
string str = "Geeks for geeks" ;
string str2 = "It is a long day Dear" ;
printStringAlternate(str);
printStringAlternate(str2);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void printStringAlternate(String str)
{
int [] occ = new int [ 122 ];
String s = str.toLowerCase();
for ( int i = 0 ; i < str.length(); i++)
{
char temp = s.charAt(i);
occ[temp]++;
if (occ[temp]% 2 != 0 )
System.out.print(str.charAt(i));
}
System.out.println();
}
public static void main (String[] args)
{
String str1 = "Geeks for geeks" ;
String str2 = "It is a long day Dear" ;
printStringAlternate(str1);
printStringAlternate(str2);
}
}
|
Python3
def printStringAlternate(string):
occ = {}
for i in range ( 0 , len (string)):
temp = string[i].lower()
occ[temp] = occ.get(temp, 0 ) + 1
if occ[temp] & 1 :
print (string[i], end = "")
print ()
if __name__ = = "__main__" :
string = "Geeks for geeks"
string2 = "It is a long day Dear"
printStringAlternate(string)
printStringAlternate(string2)
|
C#
using System;
public class GFG {
static void printStringAlternate(String str)
{
int [] occ = new int [122];
string s = str.ToLower();
for ( int i = 0; i < str.Length; i++)
{
char temp = s[i];
occ[temp]++;
if (occ[temp] % 2 != 0)
Console.Write(str[i]);
}
Console.WriteLine();
}
public static void Main ()
{
string str1 = "Geeks for geeks" ;
string str2 = "It is a long day Dear" ;
printStringAlternate(str1);
printStringAlternate(str2);
}
}
|
PHP
<?php
function printStringAlternate( $str )
{
$occ = array ();
$s = strtolower ( $str );
for ( $i = 0; $i < strlen ( $str ); $i ++)
{
$temp = $s [ $i ];
$occ [( $temp )]++;
if ( $occ [ $temp ] % 2 != 0)
echo ( $str [ $i ]);
}
echo "\n" ;
}
$str1 = "Geeks for geeks" ;
$str2 = "It is a long day Dear" ;
printStringAlternate( $str1 );
printStringAlternate( $str2 );
?>
|
Javascript
<script>
function printStringAlternate(str)
{
let occ = new Array(122);
occ.fill(0);
let s = str.toLowerCase();
for (let i = 0; i < str.length; i++)
{
let temp = s[i];
occ[temp.charCodeAt()]++;
if (occ[temp.charCodeAt()] % 2 != 0)
document.write(str[i]);
}
document.write( "</br>" );
}
let str1 = "Geeks for geeks" ;
let str2 = "It is a long day Dear" ;
printStringAlternate(str1);
printStringAlternate(str2);
</script>
|
Output
Geks fore
It sa longdy ear
Time complexity : O(n)
Auxiliary Space : O(n)
Last Updated :
15 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...