
Problem
Solution
Submissions
Longest Word Finder
Certification: Basic Level
Accuracy: 66.67%
Submissions: 3
Points: 5
Write a C# program to implement the FindLongestWord(string sentence) function, which finds and returns the longest word in a given sentence. If multiple words have the same maximum length, return the first one encountered. Spaces separate words, and punctuation marks attached to words should be ignored.
Example 1
- Input: sentence = "The quick brown fox jumps over the lazy dog"
- Output: "quick"
- Explanation: The words in the sentence are: "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"
- The longest words are "quick", "brown", "jumps" (all with 5 characters)
- "quick" is the first one to appear, so it is returned
Example 2
- Input: sentence = "Programming is fun and challenging!"
- Output: "challenging"
- Explanation: The words in the sentence are: "Programming", "is", "fun", "and", "challenging!"
- After removing punctuation, the longest word is "challenging" with 11 characters
- "Programming" has 11 characters, but "challenging" has 11 characters excluding '!'
Constraints
- 1 ≤ sentence.length ≤ 10^4
- The sentence contains English letters (lower-case and upper-case), digits, spaces, and basic punctuation
- Time Complexity: O(n)
- Space Complexity: O(n)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Split the sentence into words
- Iterate through each word and track the longest one
- Consider removing punctuation from words before comparing lengths
- Handle edge cases like empty strings or sentences with only one word