Algorithm
Problem Name: 824. 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"
.
- For example, the word
- 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"
.
- For example, the word
- Add one letter
'a'
to the end of each word per its word index in the sentence, starting with1
.- For example, the first word gets
"a"
added to the end, the second word gets"aa"
added to the end, and so on.
- For example, the first word gets
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"
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.
Code Examples
#1 Code Example with Java Programming
Code -
Java Programming
class Solution {
public String toGoatLatin(String sentence) {
String[] words = sentence.split("\\s+");
StringBuilder result = new StringBuilder();
StringBuilder aWord = new StringBuilder();
Set < Character> vowels = Set.of('a', 'e', 'i', 'o', 'u');
for (String word : words) {
aWord.append('a');
char firstChar = word.charAt(0);
if (vowels.contains(Character.toLowerCase(firstChar))) {
result.append(word).append("ma");
} else {
result.append(word.substring(1)).append(firstChar).append("ma");
}
result.append(aWord.toString()).append(" ");
}
return result.toString().trim();
}
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
const toGoatLatin = function(sentence) {
const arr = sentence.split(' ')
const vowel = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'])
for(let i = 0, n = arr.length; i < n; i++) {
const first = arr[i][0]
const ma = vowel.has(first) ? 'ma' : ''
const tmp = !vowel.has(first) ? `${arr[i].slice(1)}${first}ma` : arr[i]
const suffix = 'a'.repeat(i + 1)
arr[i] = `${tmp}${ma}${suffix}`
}
return arr.join(' ')
};
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Python Programming
Code -
Python Programming
class Solution:
def toGoatLatin(self, S):
s, vowels = S.split(), {"a", "e", "i", "o", "u"}
return " ".join([(s[i][0].lower() in vowels and s[i] or s[i][1:] + s[i][0]) + "m" + "a" * (i + 2) for i in range(len(s))])
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with C# Programming
Code -
C# Programming
using System.Collections.Generic;
using System.Text;
namespace LeetCode
{
public class _0824_GoatLatin
{
public string ToGoatLatin(string S)
{
var vowels = new HashSet < char>() { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
var wordIndex = 1;
var sb = new StringBuilder();
foreach (var word in S.Split())
{
if (vowels.Contains(word[0]))
sb.Append(word);
else
{
sb.Append(word.Substring(1));
sb.Append(word.Substring(0, 1));
}
sb.Append("ma");
sb.Append(new string('a', wordIndex++));
sb.Append(" ");
}
sb.Remove(sb.Length - 1, 1);
return sb.ToString();
}
}
}
Copy The Code &
Try With Live Editor
Input
Output