VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-string-ignoring-alternate-occurrences-character/

⇱ Print the string by ignoring alternate occurrences of any character - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print the string by ignoring alternate occurrences of any character

Last Updated : 15 Sep, 2023

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:


Output
Geks fore
It sa longdy ear

Time complexity : O(n) 
Auxiliary Space : O(n)

Comment
Article Tags:
Article Tags: