문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
공백으로 구분된 단어로 구성된 문자열 sentence가 주어진다. 각 단어는 소문자와 대문자로만 구성된다.
해당 문장을 "Goat Latin"("Pig Latin"과 유사한 인공 언어)로 변환하고 싶다. "Goat Latin"의 규칙은 다음과 같다.
문장을 Goat Latin으로 변환한 최종 문장을 반환해라.
#1
Input: setence = "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
#2
Input: setence = "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
class Solution {
public String toGoatLatin(String sentence) {
Set<Character> vowels = new HashSet<>(
Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')
);
String result = "";
int i = 0;
int j = 0;
for(String s : sentence.split("\\s")){
result += " " + (vowels.contains(s.charAt(0)) ? s : s.substring(1) + s.charAt(0)) + "ma";
for(j = 0, ++i; j < i; ++j){
result += "a";
}
}
return result.substring(1);
}
}