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
Approach : We follow total two steps to generate the both output strings.
Step 1: Create a count array and count occurrences of characters in the given input string.
Step 2: Check count array for each position ‘i’ which leads to three possible conditions :
a) If count value is 1, append character in first output string.
b) If count value is greater than 1, append character in second output string.
c) If count value is 0 do nothing.
Time Complexity for above approach is O(n).
Auxiliary Space required is O(1).
String with characters occurring once: cdltv String with characters occurring multiple times: eo
This article is contributed by Shivam Pradhan (anuj_charm). 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Generate two output strings depending upon occurrence of character in input string in Python
- Index of character depending on frequency count in string
- Find the Nth occurrence of a character in the given String
- Remove the first and last occurrence of a given Character from a String
- Program to count occurrence of a given character in a string
- Count occurrence of a given character in a string using Stream API in Java
- Minimize the length of string by removing occurrence of only one character
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- 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
- Pairs of strings which on concatenating contains each character of "string"
- Count of strings that can be formed from another string using each character at-most once
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Generate string with Hamming Distance as half of the hamming distance between strings A and B
- Concatenate the strings in an order which maximises the occurrence of subsequence "ab"
- Rearrange given string to maximize the occurrence of string t
- Generate all binary strings without consecutive 1's
- Generate all binary strings from given pattern
- Modify the string such that every character gets replaced with the next character in the keyboard