from collections import Counter
def find_unique_letters(str1, str2):
count1 = Counter(str1)
count2 = Counter(str2)
unique_letters = count1 - count2
result = []
for char, count in unique_letters.items():
result.extend([char] * count)
return result
str1 = input("Enter the first string: ")
str2 = input("Enter the second string: ")
unique_letters = find_unique_letters(str1, str2)
print("Letters in the first string but not in the second string:", unique_letters)
#source code --> clcoding.com
Code Explanation:
Importing Counter:
from collections import Counter: This imports the Counter class from Python's collections module. A Counter is a special dictionary used to count the occurrences of elements in an iterable (such as a string, list, etc.).
Defining the Function find_unique_letters:
def find_unique_letters(str1, str2): This defines a function that takes two strings str1 and str2 as input. The function will return a list of letters that are present in str1 but not in str2.
Counting Character Frequencies:
count1 = Counter(str1): This creates a Counter object for the first string str1, which counts how many times each character appears in str1. For example, if str1 = "aab", count1 would be:
{'a': 2, 'b': 1}
count2 = Counter(str2): Similarly, this creates a Counter object for str2, counting the character frequencies in str2.
Finding Unique Letters in str1 (Not in str2):
unique_letters = count1 - count2: This subtracts the Counter of str2 from the Counter of str1. The result is a new Counter object that contains only the characters that are present in str1 but not in str2. This subtraction operation works as follows:
It keeps the characters from str1 whose count is greater than in str2 (or if they don't appear in str2 at all).
For example, if str1 = "aab" and str2 = "abb", the result of count1 - count2 would be:
{'a': 1} # Since 'a' appears once more in str1 than in str2
Building the Result List:
result = []: Initializes an empty list result to store the characters that are found in str1 but not in str2.
for char, count in unique_letters.items(): This loops through each character (char) and its count (count) in the unique_letters Counter object.
result.extend([char] * count): For each character, it appends that character to the result list a number of times equal to its count. For example, if 'a': 1, then 'a' will be added once to the result list.
Returning the Result:
return result: After building the list of unique letters, the function returns the result list, which contains the letters that are in str1 but not in str2.
User Input:
str1 = input("Enter the first string: "): This takes the first string input from the user.
str2 = input("Enter the second string: "): This takes the second string input from the user.
Calling the Function:
unique_letters = find_unique_letters(str1, str2): This calls the find_unique_letters function with the user-provided strings str1 and str2 and stores the result in unique_letters.
Printing the Result:
print("Letters in the first string but not in the second string:", unique_letters): This prints the list of letters that are found in str1 but not in str2.
0 Comments:
Post a Comment