백준 10987, 모음의 개수

2021. 8. 16. 21:40CodingTest

문제풀이

위 문제를 해결하기 위해서 HashMap에 모음을 저장하고 입력받은 문자열을 하나씩 검사하여 key에 포함되는지 검사합니다. 해당 문자가 HashMap Key에 포함되는 경우에만 카운트 변수를 증가시킵니다.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;


public class Main {
	
	public static int solution(String str)
	{
		int answer = 0;
		Map<Character, Character> map = new HashMap<>();
		map.put('a', 'a');
		map.put('e', 'e');
		map.put('i', 'i');
		map.put('o', 'o');
		map.put('u', 'u');
		
		
		for(int i=0;i<str.length();i++)
		{
			if(map.containsKey(str.charAt(i)))
			{
				answer++;
			}
		}
		return answer;
	}
	public static void main(String args[]) throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String str = br.readLine();
		
		System.out.println(solution(str));
		
	}
}