Contrast compare

the living tribunal

Moderator
Staff member
moderator
Python:
from typing import List

# Function to find words in str1 that are not in str2
def unique_words(str1: str, str2: str) -> List[str]:
    words1 = set(str1.split())
    words2 = set(str2.split())
    unique = words1 - words2
    return list(unique)

# Example usage
string1 = "The quick brown fox jumps over the lazy dog"
string2 = "The quick brown cat jumps over the lazy dog"
unique_words_list = unique_words(string1, string2)
print(unique_words_list)  # Output: ['fox']
 

the living tribunal

Moderator
Staff member
moderator
Python:
from typing import List

# Function to find common words in str1 and str2 in order
def common_words_in_order(str1: str, str2: str) -> List[str]:
    words1 = str1.split()
    words2 = set(str2.split())
    common = [word for word in words1 if word in words2]
    return common

# Example usage
string1 = "The quick brown fox jumps over the lazy dog"
string2 = "The quick brown cat jumps over the lazy dog"
common_words_list = common_words_in_order(string1, string2)
print(common_words_list)  # Output: ['The', 'quick', 'brown', 'jumps', 'over', 'the', 'lazy', 'dog']
 
Top