You are given a string sentence made of words separated by single spaces. Each word is made of English letters.
Return the sentence with every word of 5 or more letters reversed. Words with fewer than 5 letters stay as they are, and the words stay in their original order.
Each letter keeps its case, so a capital letter at the start of a reversed word ends up at its end.
Examples
Example 1
Input: sentence = "Short words stay while longer ones flip"
Output: "trohS sdrow stay elihw regnol ones flip"
Explanation: "Short", "words", "while" and "longer" have 5 or more letters, so each is reversed.
"stay", "ones" and "flip" have 4 letters, so they stay as they are.
Example 2
Input: sentence = "a cat sat"
Output: "a cat sat"
Explanation: No word has 5 or more letters, so nothing changes.
Example 3
Input: sentence = "Hello Code World"
Output: "olleH Code dlroW"
Explanation: "Hello" and "World" have exactly 5 letters, so they are reversed. "Code" has 4 letters.
Constraints
1≤sentence.length≤105
sentence consists of English letters (lowercase and uppercase) and single spaces
sentence does not start or end with a space
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up