711. Capitalize First and Last Character of Each Word

You are given a string s consisting of one or more words separated by single spaces. Your task is to capitalize the first and last character of each word in the string.

If a word contains only one character, capitalize that character.

Return the transformed string after applying the operation to all words.

Example 1:

Input: s = "take u forward is awesome"

Output: "TakE U ForwarD IS AwesomE"

Explanation: Each word's first and last characters are capitalized.

Example 2:

Input: s = "Take u Forward is Awesome"

Output: "TakE U ForwarD IS AwesomE"

Explanation: Already capitalized characters remain, others are capitalized.

Now Your Turn!

Pick the correct output for the given input

Input: s = "x y z"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 105
  • The string contains only lowercase and uppercase English letters and single spaces between words
  • There are no leading or trailing spaces, and no multiple spaces between words
0
class Solution {
public:
string capitalizeFirstLast(string s) {
// Your code goes here
}
};
 
Test Case

Input:

S