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 that character that occurs only once in the input string and the second 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 an 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.
The 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.
Implementation:
Python3
from collections import Counter
def generateStrings( input ):
freqDict = Counter( input )
freq1 = [ key for (key,count) in freqDict.items() if count = = 1 ]
freqMore1 = [ key for (key,count) in freqDict.items() if count> 1 ]
freq1.sort()
freqMore1.sort()
print ( 'String with characters occurring once:' )
print (''.join(freq1))
print ( 'String with characters occurring multiple times:' )
print (''.join(freqMore1))
if __name__ = = "__main__" :
input = "geeksforgeeks"
generateStrings( input )
|
Output
String with characters occurring once:
for
String with characters occurring multiple times:
egks
Time complexity: O(NlogN)
Auxiliary space: O(N)
Method 2: The given program generates two output strings depending upon the occurrence of characters in the input string. Here is an alternative implementation of the same functionality:
Steps:
- Define the function generateStrings that takes an input string as a parameter.
- Create two empty lists freq1 and freqMore1 to store characters occurring once and multiple times respectively.
- Create an empty dictionary char_count to keep track of the count of each character in the input string.
- Loop through each character in the input string and check if it is already in the dictionary.
- If the character is not in the dictionary, add it with count 1 and append it to freq1.
- If the character is already in the dictionary, increment its count and append it to freqMore1.
- Sort the lists freq1 and freqMore1 in ascending order.
- Concatenate the characters in the lists without spaces.
- Print the output strings.
- In the driver program, define an input string and call the generateStrings function with this string as a parameter.
- The function will print the two output strings.
Example:
Python3
def generateStrings(input_string):
freq1 = []
freqMore1 = []
char_count = {}
for char in input_string:
if char not in char_count:
char_count[char] = 1
freq1.append(char)
else :
char_count[char] + = 1
freqMore1.append(char)
freq1.sort()
freqMore1.sort()
print ( 'String with characters occurring once:' )
print (''.join(freq1))
print ( 'String with characters occurring multiple times:' )
print (''.join(freqMore1))
if __name__ = = "__main__" :
input_string = "geeksforgeeks"
generateStrings(input_string)
|
Output
String with characters occurring once:
efgkors
String with characters occurring multiple times:
eeegks
Time Complexity: O(NlogN), where n is the length of the input string.
Auxiliary Space: O(N) , where n is the length of the input string.
Last Updated :
22 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...