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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Generate two output strings depending upon occurrence of character in input string.
- Python | First character occurrence from rear String
- Python | Split string on Kth Occurrence of Character
- Python program to find occurrence to each character in given string
- Index of character depending on frequency count in string
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Find the Nth occurrence of a character in the given String
- Program to count occurrence of a given character in a string
- Minimize the length of string by removing occurrence of only one character
- Count occurrence of a given character in a string using Stream API in Java
- Python | Substitute character with its occurrence
- Python | Replace multiple occurrence of character by single
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Maximum occurring character in an input string | Set-2
- Return maximum occurring character in an input string
- Count of strings that can be formed from another string using each character at-most once
- Pairs of strings which on concatenating contains each character of "string"
- Python | Get the string after occurrence of given substring
- Python - Replace duplicate Occurrence in String
- Python | Ways to find nth occurrence of substring in a string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.