Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Common Words in Two Strings in Python
Finding common words between two strings is a frequent programming task. This involves splitting strings into words, handling case-insensitivity, and finding intersections between word sets.
For example, if we have s0 = "i love python coding" and s1 = "coding in python is easy", the common words are python and coding, so the count is 2.
Algorithm Steps
To find common words between two strings, follow these steps:
- Convert both strings to lowercase for case-insensitive comparison
- Split each string into a list of words using
split() - Convert word lists to sets and find their intersection
- Return the count of common words
Using Set Intersection
The most efficient approach uses Python sets to find common elements ?
def find_common_words(s0, s1):
s0 = s0.lower()
s1 = s1.lower()
s0_words = s0.split()
s1_words = s1.split()
return len(set(s0_words) & set(s1_words))
# Test the function
s0 = "i love python coding"
s1 = "coding in python is easy"
result = find_common_words(s0, s1)
print(f"Number of common words: {result}")
# Show the actual common words
common_words = set(s0.lower().split()) & set(s1.lower().split())
print(f"Common words: {list(common_words)}")
Number of common words: 2 Common words: ['python', 'coding']
Using Class-Based Approach
Here's the object-oriented version of the solution ?
class WordComparator:
def count_common_words(self, s0, s1):
s0 = s0.lower()
s1 = s1.lower()
s0_words = s0.split()
s1_words = s1.split()
return len(set(s0_words) & set(s1_words))
def get_common_words(self, s0, s1):
s0 = s0.lower()
s1 = s1.lower()
s0_words = set(s0.split())
s1_words = set(s1.split())
return list(s0_words & s1_words)
# Example usage
comparator = WordComparator()
s0 = "i love python coding"
s1 = "coding in python is easy"
count = comparator.count_common_words(s0, s1)
words = comparator.get_common_words(s0, s1)
print(f"Count: {count}")
print(f"Words: {words}")
Count: 2 Words: ['python', 'coding']
Handling Multiple Test Cases
Let's test with different string combinations ?
def find_common_words_count(s0, s1):
s0_words = set(s0.lower().split())
s1_words = set(s1.lower().split())
return len(s0_words & s1_words)
# Test cases
test_cases = [
("i love python coding", "coding in python is easy"),
("Hello World", "world hello"),
("Python Java C++", "JavaScript Python Ruby"),
("no common", "different words")
]
for s0, s1 in test_cases:
count = find_common_words_count(s0, s1)
common = set(s0.lower().split()) & set(s1.lower().split())
print(f"'{s0}' & '{s1}' ? {count} common words: {list(common)}")
'i love python coding' & 'coding in python is easy' ? 2 common words: ['python', 'coding'] 'Hello World' & 'world hello' ? 2 common words: ['world', 'hello'] 'Python Java C++' & 'JavaScript Python Ruby' ? 1 common words: ['python'] 'no common' & 'different words' ? 0 common words: []
Key Points
-
Case Insensitive: Use
lower()to handle different cases - Set Operations: Sets provide efficient intersection operations
-
Split Method:
split()without arguments handles multiple spaces automatically -
Intersection Operator:
&finds common elements between sets
Conclusion
Finding common words between strings involves converting to lowercase, splitting into words, and using set intersection. The & operator efficiently finds common elements between two sets of words.
