Tuesday, 7 January 2025

Day 79: Python Program to Find Common Characters in Two Strings


 from collections import Counter

def find_non_common_letters(str1, str2):

    count1 = Counter(str1)

    count2 = Counter(str2)

    non_common_letters = (count1 - count2) + (count2 - count1)

    result = []

    for char, count in non_common_letters.items():

        result.extend([char] * count)  

    return result

str1 = input("Enter the first string: ")

str2 = input("Enter the second string: ")

non_common_letters = find_non_common_letters(str1, str2)

print("Letters that are not common in both strings:", non_common_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 that counts the occurrences of elements in an iterable, such as a string.
Function find_non_common_letters:

def find_non_common_letters(str1, str2): This defines a function that takes two strings, str1 and str2, as input and returns a list of letters that are not common between the two strings.

Counting Characters:
count1 = Counter(str1): This creates a Counter object for str1, which counts how many times each character appears in the string str1.
For example, for str1 = "aab", count1 would be: {'a': 2, 'b': 1}.
count2 = Counter(str2): Similarly, this creates a Counter object for str2, counting character occurrences in str2.

Finding Non-Common Letters:
non_common_letters = (count1 - count2) + (count2 - count1):
count1 - count2: This finds the letters that appear in str1 but not in str2, keeping the count of occurrences.
count2 - count1: This finds the letters that appear in str2 but not in str1, again keeping the count of occurrences.
The + operator combines these two parts, effectively getting the total set of non-common letters with their counts.

Building the Result List:
result = []: Initializes an empty list, result, to store the non-common letters.
for char, count in non_common_letters.items(): This loops through each character (char) and its count (count) from non_common_letters.
result.extend([char] * count): This line adds the character char to the list result multiple times (according to its count). For example, if 'a': 3, the character 'a' will appear 3 times in result.

Returning the Result:
return result: This returns the final list result, which contains all the letters that are not common between str1 and str2, each appearing the appropriate number of times.

Taking User Input:
str1 = input("Enter the first string: "): Takes the first string input from the user.
str2 = input("Enter the second string: "): Takes the second string input from the user.

Calling the Function:
non_common_letters = find_non_common_letters(str1, str2): This calls the find_non_common_letters function with the two user-provided strings and stores the result in non_common_letters.

Printing the Result:
print("Letters that are not common in both strings:", non_common_letters): This prints the list of non-common letters.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (78) AI (35) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (179) C (77) C# (12) C++ (82) Course (67) Coursera (231) Cybersecurity (24) data management (11) Data Science (129) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (61) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) Python (951) Python Coding Challenge (398) Python Quiz (47) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses