Generate two output strings depending upon occurrence of character in input string in Python

Given an input string str[], generate two output strings. One of which consists of those character which occurs only once in input string and second which consists of multi-time occurring characters. Output strings must be sorted.

Examples:

Input : str = "geeksforgeeks"
Output : String with characters occurring once:
"for".
String with characters occurring multiple times:
"egks"

Input : str = "geekspractice"
Output : String with characters occurring once:
"agikprst"
String with characters occurring multiple times:
"ce"



We have existing solution for this problem please refer Generate two output strings depending upon occurrence of character in input string link. We can solve this problem quickly in python using Counter(iterable) method. Approach is simple,

  1. Convert string into dictionary having characters as keys and their frequencies as value using counter() method.
  2. Now separate out list of characters having frequency 1 and having frequency more than 1.
  3. Sort characters in both lists to get output strings.
filter_none

edit
close

play_arrow

link
brightness_4
code

# Function Generate two output strings depending upon 
# occurrence of character in input string
  
from collections import Counter
  
def generateStrings(input):
      
     # convert string into dictionary
     # having characters as keys and frequency as value
     freqDict = Counter(input)
  
     # separate out characters having frequency 1 and more than 1
     freq1 = [ key for (key,count) in freqDict.items() if count==1]
     freqMore1 = [ key for (key,count) in freqDict.items() if count>1]
  
     # sort lists and concatenate characters
     # with out space to print resultant strings
     freq1.sort()
     freqMore1.sort()
  
     # print output strings
     print ('String with characters occurring once:')
     print (''.join(freq1))
     print ('String with characters occurring multiple times:')
     print (''.join(freqMore1))
  
# Driver program
if __name__ == "__main__":
    input = "geeksforgeeks"
    generateStrings(input)

chevron_right


Output:

String with characters occurring once:
for
String with characters occurring multiple times:
egks


My Personal Notes arrow_drop_up

Experienced Software Engineer with a demonstrated history of working in the information technology and services industry Skilled in Big Data technologies, Data Structures, Algorithm, Software Development, App Development, Databases, Python, Java, C, and WordPress

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.