Return the shortest word in a sentence. Words are separated by single spaces. When two or more words share the shortest length, return the first (leftmost) one. Return null when the input is null or blank.
Examples:
Input: sentence = 'the quick brown fox'
Output: 'the'
Explanation: the and fox both have length 3, the appears first
Input: sentence = 'a bb ccc'
Output: 'a'
Explanation: a is the shortest word
Input: sentence = 'hello'
Output: 'hello'
Explanation: A single word is the shortest by default
Input: sentence = 'cat dog'
Output: 'cat'
Explanation: Both have length 3, cat appears first
Input: sentence = null
Output: null
Explanation: Null input returns null
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Note
You can test your code by connecting to Salesforce, but to save your progress and earn points, you'll need to create an account. Your solutions and achievements will be tracked automatically once you're logged in.