824. Goat Latin LeetCode Solution

In this guide, you will get 824. Goat Latin LeetCode Solution with the best time and space complexity. The solution to Goat Latin problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Goat Latin solution in C++
  4. Goat Latin solution in Java
  5. Goat Latin solution in Python
  6. Additional Resources
824. Goat Latin LeetCode Solution image

Problem Statement of Goat Latin

You are given a string sentence that consist of words separated by spaces. Each word consists of lowercase and uppercase letters only.
We would like to convert the sentence to “Goat Latin” (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows:

If a word begins with a vowel (‘a’, ‘e’, ‘i’, ‘o’, or ‘u’), append “ma” to the end of the word.

For example, the word “apple” becomes “applema”.

If a word begins with a consonant (i.e., not a vowel), remove the first letter and append it to the end, then add “ma”.

For example, the word “goat” becomes “oatgma”.

Add one letter ‘a’ to the end of each word per its word index in the sentence, starting with 1.

For example, the first word gets “a” added to the end, the second word gets “aa” added to the end, and so on.

Return the final sentence representing the conversion from sentence to Goat Latin.

Example 1:
Input: sentence = “I speak Goat Latin”
Output: “Imaa peaksmaaa oatGmaaaa atinLmaaaaa”
Example 2:
Input: sentence = “The quick brown fox jumped over the lazy dog”
Output: “heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa”

See also  144. Binary Tree Preorder Traversal LeetCode Solution

Constraints:

1 <= sentence.length <= 150
sentence consists of English letters and spaces.
sentence has no leading or trailing spaces.
All the words in sentence are separated by a single space.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

824. Goat Latin LeetCode Solution in C++

class Solution {
 public:
  string toGoatLatin(string sentence) {
    string ans;
    istringstream iss(sentence);

    int i = 1;
    for (string word; iss >> word;) {
      if (i > 1)
        ans += ' ';
      if (isVowel(word[0]))
        ans += word;
      else
        ans += word.substr(1) + word[0];
      ans += "ma" + string(i++, 'a');
    }

    return ans;
  }

 private:
  bool isVowel(char c) {
    static constexpr string_view kVowels = "aeiouAEIOU";
    return kVowels.find(c) != string_view::npos;
  }
};
/* code provided by PROGIEZ */

824. Goat Latin LeetCode Solution in Java

class Solution {
  public String toGoatLatin(String sentence) {
    StringBuilder sb = new StringBuilder();

    int i = 1;
    for (final String word : sentence.split(" ")) {
      if (i > 1)
        sb.append(" ");
      if (isVowel(word.charAt(0)))
        sb.append(word);
      else
        sb.append(word.substring(1) + word.charAt(0));
      sb.append("ma").append("a".repeat(i++));
    }

    return sb.toString();
  }

  private boolean isVowel(char c) {
    return "aeiouAEIOU".indexOf(c) != -1;
  }
}
// code provided by PROGIEZ

824. Goat Latin LeetCode Solution in Python

class Solution:
  def toGoatLatin(self, sentence: str) -> str:
    ans = []
    kVowels = 'aeiouAEIOU'

    i = 1
    for word in sentence.split():
      if i > 1:
        ans.append(' ')
      if word[0] in kVowels:
        ans.append(word)
      else:
        ans.append(word[1:] + word[0])
      ans.append('ma' + 'a' * i)
      i += 1

    return ''.join(ans)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.