Approach #1: String [Accepted]


We apply the steps given in the problem in a straightforward manner. The difficulty lies in the implementation.


For each word in the sentence split, if it is a vowel we consider the word, otherwise we consider the rotation of the word (either word[1:] + word[:1] in Python, otherwise word.substring(1) + word.substring(0, 1) in Java).

Afterwards, we add "ma", the desired number of "a"'s, and a space character.

Complexity Analysis

  • Time Complexity: , where is the length of S. This represents the complexity of rotating the word and adding extra "a" characters.

  • Space Complexity: , the space added to the answer by adding extra "a" characters.