Challenge: Find the number of distinct words in a sentence
Your task is to write a program that takes a sentence as input and returns the number of distinct words in it. For this challenge, words are defined as consecutive sequences of letters, digits, and underscores ('_').
Input:
- A string containing a sentence. The length of the sentence will not exceed 1000 characters.
Output:
- An integer indicating the number of distinct words in the sentence.
Examples:
Input: "The quick brown fox jumps over the lazy dog"
Output: 8
Explanation: The sentence contains 9 words, 8 of which are distinct: "The", "quick", "brown", "fox", "jumps", "over", "lazy", and "dog"
Input: "To be, or not to be, that is the question"
Output: 8
Explanation: The sentence contains 8 distinct words: "To", "be", "or", "not", "that", "is", "the", and "question".
Input: "Hello, World!"
Output: 2
Explanation: The sentence contains 2 distinct words: "Hello" and "World".
Input: "hello-world2"
Output: 2
Explanation: Two distinct words, "hello" and "world2" separated by hyphen.
Input: "Hello, World! Hello!"
Output: 2
Explanation: Only "Hello" and "World" are two distinct words here.
Scoring: This is a code golf challenge, so the goal is to minimize the size of your code while still producing correct output for all test cases. In case of a tie, the earliest submission wins. Good luck!
1
(1 word),hello-world2
(2 words),code_._golf.
(2 words) -- assuming I'm interpreting the question correctly. \$\endgroup\$Hello, World! Hello!
, to catch solutions that try to split on spaces. \$\endgroup\$