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.
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,
- Convert string into dictionary having characters as keys and their frequencies as value using counter() method.
- Now separate out list of characters having frequency 1 and having frequency more than 1.
- Sort characters in both lists to get output strings.
String with characters occurring once: for String with characters occurring multiple times: egks
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live