Taro Logo

Goat Latin

Easy
Meta logo
Meta
Topics:
Strings

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:

  1. 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".
  2. 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".
  3. 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"

Can you implement a function to perform this conversion?

Solution


Naive Solution

The most straightforward approach is to split the sentence into words, then iterate through each word, applying the Goat Latin transformation rules directly. This involves checking if the word starts with a vowel, and then performing the appropriate string manipulations. Finally, appending the correct number of 'a' characters based on the word's index.

Big O Runtime: O(N*M), where N is the number of words in the sentence, and M is the average length of a word. This is because, in the worst case, we iterate over all characters of all words.

Big O Space: O(N*M), where N is the number of words, and M is the average length of a word. This accounts for the space used to store the modified words.

Optimal Solution

The optimal solution is essentially the same algorithm as the naive solution but optimized for readability and conciseness. There are no clever tricks to fundamentally alter the time complexity since we must still process each word and character. Optimization here would involve ensuring efficient string manipulation using a StringBuilder (or equivalent) to avoid unnecessary object creation, and perhaps inlining vowel checking for speed.

Edge Cases:

  • Empty sentence: Return an empty string.
  • Sentence with only spaces: Return an empty string.
  • Words containing only vowels or only consonants should be handled correctly.
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

class Solution {
    public String toGoatLatin(String sentence) {
        Set<Character> vowels = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
        String[] words = sentence.split(" ");
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            StringBuilder newWord = new StringBuilder();

            if (vowels.contains(word.charAt(0))) {
                newWord.append(word);
            } else {
                newWord.append(word.substring(1));
                newWord.append(word.charAt(0));
            }

            newWord.append("ma");

            for (int j = 0; j <= i; j++) {
                newWord.append('a');
            }

            result.append(newWord);
            if (i < words.length - 1) {
                result.append(" ");
            }
        }

        return result.toString();
    }
}

Big O Runtime: O(N*M), where N is the number of words in the sentence, and M is the average length of a word. This is because, in the worst case, we iterate over all characters of all words.

Big O Space: O(N*M), where N is the number of words, and M is the average length of a word. This accounts for the space used to store the modified words.