[Coding]모음 제거

Jason·2024년 1월 30일
0

Coding problems

목록 보기
4/7
post-thumbnail

Platform

programmers


Description

영어에선 a, e, i, o, u 다섯 가지 알파벳을 모음으로 분류합니다. 문자열 my_string이 매개변수로 주어질 때 모음을 제거한 문자열을 return하도록 solution 함수를 완성해주세요.

입출력 예 #1
"bus"에서 모음 u를 제거한 "bs"를 return합니다.
입출력 예 #2
"nice to meet you"에서 모음 i, o, e, u를 모두 제거한 "nc t mt y"를 return합니다.

Solution

class Solution {
    public String solution(String my_string) {
        String answer = my_string.replace("a", "")
                                 .replace("e", "")
                                 .replace("i", "")
                                 .replace("o", "")
                                 .replace("u", "");
        
        return answer;
    }
}

Other solution

class Solution {
    public String solution(String my_string) {
        String answer = "";

        answer = my_string.replaceAll("[aeiou]", "");

        return answer;
    }
}

TIL - regex [characters]

  • my_string.replaceAll("[aeiou]", "")
    a,e,i,o,u 에 해당하는 문자들을 모두 공백처리한다.
    여기서 [char1, char2...] 는 [ ] 안의 어느 것이든 해당할 수 있음.
profile
어제보다 매일 1% 성장하고 있습니다.

0개의 댓글