코딩 알고리즘 문제/Leetcode

824. Goat Latin (String)

highlightmoon 2025. 10. 23. 04:38
반응형

링크 - https://leetcode.com/problems/goat-latin/description/?envType=company&envId=facebook&favoriteSlug=facebook-thirty-days

난이도 - Easy

Intuition

단순 string문제이다. .lower(), .split(" "), " ".join()등의 string기본 methods들을 알면 쉽게 풀 수 있다.

Code

class Solution:
    def toGoatLatin(self, sentence: str) -> str:
        words = sentence.split(" ")
        a = "a"
        ma = "ma"
        for i, word in enumerate(words):
            if word[0].lower() in "aeiou":
                word += ma
            else:
                word = word[1:] + word[0] + ma
            word += a
            words[i] = word
            a += "a"

        return " ".join(words)

Complexity

Time Complexity: O(n)

Space Complexity: O(n)

반응형