Generate Two Output Strings Depending upon Occurrence of Character in Input String - Python
The task of generating two output strings based on the occurrence of characters in an input string in Python involves classifying characters based on their frequency. We need to create one string that contains characters that appear only once in the input string and another string for characters that appear more than once. For example, given the string s = "geeksforgeeks", the first output string should contain characters that appear once, while the second output string will contain characters that appear more than once.
Using collections.Counter
Counter
from the collections
module efficiently count the frequency of characters in a string. It then separates the characters into two lists . One for characters occurring once and another for those occurring more than once. Finally, both lists are sorted alphabetically and joined into strings for output.
from collections import Counter
s = "hello"
d = Counter(s) # create counter object
# Separate characters into those with count 1 and greater than 1
a = [char for char, count in d.items() if count == 1]
b = [char for char, count in d.items() if count > 1]
print(''.join(sorted(a)))
print(''.join(sorted(b)))
Output
eho l
Explanation:
- Two lists are created where
a
contains characters with count of 1 andb
contains characters with count greater than 1. - Both lists are sorted alphabetically using
sorted()
and then joined into strings with''.join()
.
Table of Content
Using for loop
This method counts character frequencies in a string by going through it just once, storing the counts in a dictionary. After counting, it separates the characters into two lists. The lists are then sorted and combined into strings. This approach is efficient as it avoids repeatedly counting characters and ensures faster processing by using a single pass through the string.
s = "hello"
d = {}
# Single pass over input string to count frequencies
for char in s:
d[char] = d.get(char, 0) + 1
# Separate characters into two lists
a = [char for char, count in d.items() if count == 1]
b = [char for char, count in d.items() if count > 1]
print(''.join(sorted(a)))
print(''.join(sorted(b)))
Output
eho l
Explanation:
- split characters into two lists, one for those occurring once and another for those occurring more than once.
- sort both lists alphabetically and join them into strings for display.
Using defaultdict
This approach uses defaultdict
to count the frequency of characters in the input string, where each character is initialized with a default count of 0. The characters are then divided into two groups. Both groups are sorted alphabetically and returned as strings.
Example:
from collections import defaultdict
s = "hello"
count = defaultdict(int) # Creates a defaultdict
for char in s:
count[char] += 1
a = [char for char, freq in count.items() if freq == 1]
b = [char for char, freq in count.items() if freq > 1]
print(''.join(sorted(a)))
print(''.join(sorted(b)))
Output
eho l
Explanation:
- for char in s loops through each character in the input string s and increments its count in the count dictionary. If the character doesn't exist, it's initialized to 0 and incremented by 1.
- Two lists are created where a contains characters with count of 1 and
b
contains characters with count greater than 1 and Both lists are sorted alphabetically usingsorted()
and then joined into strings with''.join()
.
Using itertools.groupby()
itertools.groupby from itertools groups consecutive occurrences of the same element. This method requires sorting the string first, making it less efficient compared to single-pass methods like Counter and defaultdict.
from itertools import groupby
s = "hello"
sorted_s = sorted(s) # Sorts the input string `s` alphabetically
a = [] # initialize empty list to store characters that appear once
b = [] # initialize empty list to store characters that appear more than once
for char, group in groupby(sorted_s):
if len(list(group)) == 1:
a.append(char)
else:
b.append(char)
print(''.join(sorted(a)))
print(''.join(sorted(b)))
Output
eho l
Explanation:
- for char, group in groupby(sorted_s) iterates over the sorted string grouping consecutive occurrences of each character.
- if len(list(group)) == 1 adds characters that appear once to a otherwise adds characters that appear more than once to b.