
Problem
Solution
Submissions
Largest Word in a String
Certification: Basic Level
Accuracy: 54.55%
Submissions: 11
Points: 10
Write a C++ program that finds the largest word in a given string. If there are multiple words with the same maximum length, return the first one.
Example 1
- Input: string = "I love programming"
- Output: "programming"
- Explanation:
- Step 1: Split the input string into words ["I", "love", "programming"].
- Step 2: Find the word with the maximum length.
- Step 3: Return the longest word "programming" (11 characters).
Example 2
- Input: string = "C++ is fun"
- Output: "C++"
- Explanation:
- Step 1: Split the input string into words ["C++", "is", "fun"].
- Step 2: Find the word with the maximum length.
- Step 3: Return the first longest word "C++" (3 characters), since both "C++" and "fun" have the same length.
Constraints
- 1 ≤ len(string) ≤ 10^6
- String contains printable ASCII characters
- Time Complexity: O(n), where n is the length of the string
- 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 string into words based on spaces.
- Track the length of each word and keep the longest one.
- Handle edge cases where the string is empty or contains only spaces.