require 'set'
# Removes duplicate words from a free-text string containing Unicode characters.
# Preserves word order and the case of the first occurrence.
#
# @param input [String] The input string containing text and punctuation.
# @return [String] Space-separated unique words.
def remove_duplicate_words(input)
return '' if input.nil? || input.strip.empty?
# \p{L}+ matches sequences of any Unicode letter (Latin, Japanese, Greek, Cyrillic, etc.)
# \p{N}+ matches digits if needed: /[\p{L}\p{N}_]+/
words = input.scan(/[\p{L}\p{N}_]+/)
# Set for O(1) case-insensitive duplicate tracking
seen_words = Set.new
# Filter words sequentially while keeping the first occurrence's original case
unique_words = words.select do |word|
# Set#add? returns nil if the lowercased word was already present
seen_words.add?(word.downcase)
end
# Join unique words with a single space
unique_words.join(' ')
end
# Main
input = "Hello! こんにちは, ,hello こんにちは Bună ziua; Γεια σας Bună ziua *HELLO* Γεια σας"
result = remove_duplicate_words(input)
puts result
# run:
#
# Hello こんにちは Bună ziua Γεια σας
#