• 문제 링크
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';
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 832. Flipping an Image - Java (0) | 2026.10.23 |
|---|---|
| [LeetCode] 830. Positions of Large Groups - Java (0) | 2026.10.23 |
| [LeetCode] 821. Shortest Distance to a Character - Java (0) | 2026.10.20 |
| [LeetCode] 819. Most Common Word - Java (0) | 2026.10.19 |
| [LeetCode] 812. Largest Triangle Area - Java (0) | 2026.10.18 |
댓글