This problem looks busy at first, but the accepted solution is built around one steady invariant. For 824. Goat Latin, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are toGoatLatinToken, aMul, toGoatLatin.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- Substring checks are convenient but not free, so they are part of the real complexity story.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.
03
04We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.)
05
06The rules of Goat Latin are as follows:
07
08If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word.
09For example, the word 'apple' becomes 'applema'.
10
11If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add "ma".
12For example, the word "goat" becomes "oatgma".
13
14Add one letter 'a' to the end of each word per its word index in the sentence, starting with 1.
15For example, the first word gets "a" added to the end, the second word gets "aa" added to the end and so on.
16Return the final sentence representing the conversion from S to Goat Latin.
17
18
19
20Example 1:
21
22Input: "I speak Goat Latin"
23Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
24Example 2:
25
26Input: "The quick brown fox jumped over the lazy dog"
27Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
28
29
30Notes:
31
32S contains only uppercase, lowercase and spaces. Exactly one space between each word.
331 <= S.length <= 150.
34**/
35
36//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Goat Latin.
37//Memory Usage: 9.7 MB, less than 73.58% of C++ online submissions for Goat Latin.
38
39class Solution {
40public:
41 string toGoatLatinToken(string token, int order, string delimeter){
42 if(!(token[0]=='a' || token[0]=='e' || token[0]=='i' || token[0]=='o' || token[0]=='u' || token[0]=='A' || token[0]=='E' || token[0]=='I' || token[0]=='O' || token[0]=='U')){
43 token += token[0];
44 token.erase(token.begin());
45 }
46 string aMul(order, 'a');
47 return token + "ma" + aMul + delimeter;
48 }
49
50 string toGoatLatin(string S) {
51 string ans;
52 size_t pos = 0;
53 string token;
54 int order = 1;
55 string delimeter = " ";
56 while ((pos = S.find(delimeter)) != string::npos) {
57 token = S.substr(0, pos);
58 ans += toGoatLatinToken(token, order, delimeter);
59 order++;
60 S.erase(0, pos + delimeter.size());
61 }
62 ans += toGoatLatinToken(S, order, "");
63 return ans;
64 }
65};
66
67/**
68Approach #1: String [Accepted]
69Intuition
70
71We apply the steps given in the problem in a straightforward manner. The difficulty lies in the implementation.
72
73Algorithm
74
75For 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).
76
77Afterwards, we add "ma", the desired number of "a"'s, and a space character.
78**/
79
80/**
81Complexity Analysis
82
83Time Complexity: O(N^2), where N is the length of S.
84This represents the complexity of rotating the word and adding extra "a" characters.
85
86Space Complexity: O(N^2), the space added to the answer by adding extra "a" characters.
87**/
Cost