Suppose we have a set of strings (Sentence), in that set there are few words. Each words are consists of lowercase and uppercase letters. Our task is to convert the sentence into Goat-Latin form. The Goat Latin is similar to the Pig Latin. There are some few conditions.
So if the example is like “Adam wants to go to the university”, the converted string will be “Adammaa antswmaaa otmaaaa ogmaaaaa otmaaaaaa hetmaaaaaaa universitymaaaaaaaa”
To solve this, the task is simple, take the sentence and split it into words, then for each word, check the given conditions and perform the action as mentioned above.
Let us see the following implementation to get better understanding −
class Solution: def toGoatLatin(self, S): """ :type S: str :rtype: str """ temp = S.split(" ") counter = 1 result = [] vowel = {"a","e","i","o","u"} for i in temp: if i[0].lower() in vowel: x = i + "ma" + ("a"*counter) else: x=i[1:]+i[0] + "ma" +("a"*counter) counter+=1 result.append(x) return " ".join(c for c in result) ob1 = Solution() print(ob1.toGoatLatin("Adam wants to go to the university"))
"Adam wants to go to the university"
"Adammaa antswmaaa otmaaaa ogmaaaaa otmaaaaaa hetmaaaaaaa universitymaaaaaaaa"