본문 바로가기
Problem Solving/LeetCode

[LeetCode] 824. Goat Latin - Java

by graycode 2026. 10. 21.

• 문제 링크

 

Goat Latin - LeetCode

Can you solve this real interview question? 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

leetcode.com

 

• 풀이 코드

public class Solution {

    public String toGoatLatin(String sentence) {
        StringBuilder sb = new StringBuilder();
        String[] arr = sentence.split(" ");
        for (int i = 0; i < arr.length; i++) {
            if (!isVowel(arr[i].charAt(0))) sb.append(arr[i].substring(1)).append(arr[i].charAt(0));
            else sb.append(arr[i]);
            sb.append("ma").repeat("a", i + 1).append(' ');
        }

        return sb.toString().trim();
    }

    private boolean isVowel(char c) {
        return (c = Character.toLowerCase(c)) == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
    }

}

댓글