import re
def split_words(text: str) -> list:
"""
Splits free text into words using a simple, portable regex.
Regex explanation:
[^A-Za-z]+ → any sequence of NON-ASCII letters
This is not full Unicode, but it is:
- fully supported by Python's re
- safe and predictable
"""
text = text.strip()
# Split on any sequence of non-letter characters
return re.split(r"[^A-Za-z]+", text)
def remove_duplicate_words(text: str) -> str:
"""
Removes duplicate words while preserving:
- original order
- original casing of first occurrence
- case-insensitive comparison
Uses Python's built-in set for O(1) average lookup time.
"""
words = split_words(text)
seen = set()
unique = []
for word in words:
if not word:
continue
key = word.lower() # case-insensitive key
if key not in seen:
seen.add(key)
unique.append(word) # preserve original casing
# Reassemble into a space-separated string
return " ".join(unique)
# ------------------------------------------------------------
# Program entry point
# ------------------------------------------------------------
input_text = (
"Hello, hello! This is a test. A TEST, hello universe... "
"UNIVERSE! Hello; *** Is Anybody There?"
)
output = remove_duplicate_words(input_text)
print(output)
"""
run:
Hello This is a test universe Anybody There
"""