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).
# Python3 program to print two strings
# made of character occurring once
# and multiple times
MAX_CHAR = 256
# function to print two strings
# generated from single string one
# with characters occurring onces
# other with character occurring
# multiple of times
# initialize hashtable with zero
countChar = [0 for i in range(MAX_CHAR)]
# perform hashing for input string
n = len(string)
for i in range(n):
countChar[ord(string[i]) – ord(‘a’)] += 1
# generate string (str1) consisting
# char occurring once and string
# (str2) consisting char occurring
# multiple times
str1 = “”
str2 = “”
for i in range(MAX_CHAR):
if (countChar[i] > 1):
str2 = str2 + chr(i + ord(‘a’))
elif (countChar[i] == 1):
str1 = str1 + chr(i + ord(‘a’))
# print both strings
print(“String with characters occurring once:”,
print(“String with characters occurring”,
“multiple times:”, “\n”, str2)
# Driver Code
string = “lovetocode”
# This code is contributed by
# Mohit kumar 29
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.
- Return maximum occurring character in an input string
- Print all the duplicates in the input string
- Remove characters from the first string which are present in the second string
- Remove duplicates from a given string
- A Program to check if strings are rotations of each other or not
- Print reverse of a string using recursion
- Write a program to print all permutations of a given string
- Divide a string in N equal parts
- Given a string, find its first non-repeating character
- Write a program to reverse an array or string
- Reverse words in a given string
- Find the smallest window in a string containing all characters of another string
- Check whether two strings are anagram of each other
- Print all interleavings of given two strings
- Check whether a given string is an interleaving of two other given strings