본문 바로가기
Data Structure & Algorithm/백준

1157번 단어 공부

by 6cess 2022. 9. 19.

백준 문제를 풀면서 처음으로 객체를 써보았다.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
public class Main {
	
    //알파벳 객체
	public static class Alphabet {
		
		char s;
		int count;
        
		public char getS() {
			return s;
		}
		public void setS(char s) {
			this.s = s;
		}
		public int getCount() {
			return count;
		}
		public void setCount(int count) {
			this.count = count;
		}	
		
		public void countUp () {
			count++;
		}
	} 
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); 
		
		String st = br.readLine();
		
		st = st.toUpperCase();
		
		ArrayList<Alphabet> abc = new ArrayList<>();
		
		for(int i=0; i<st.length(); i++ ) {
			
			boolean flag = false;
			
            //abc 리스트에 이미 생성된 객체가 있다면 카운트 업
			for( Alphabet a : abc ) {
				if(a.getS() == st.charAt(i)){
					a.countUp();
					
					flag = true;
					break;
				}
			}
			
			//만약 어떤 alphbet 객체도 만나지 못했다면 객체 생성 
			if(flag == false) {
				Alphabet alpha = new Alphabet();
				alpha.setS(st.charAt(i));
				alpha.countUp();
				abc.add(alpha);
				}
		}
		
		int num = 0;
		char most = ' ';
		
		for( Alphabet a : abc) {
			if(a.getCount() > num) {
				num = a.getCount();
				most = a.getS();
			} else if(a.getCount() == num) {
				most = '?';
			}
		}
		
		bw.write(most);
		bw.flush();
		bw.close();
	}
}