#1157: 단어 공부

#1157: 단어 공부

Java 15
import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        input = input.toUpperCase();
        sc.close();

        if(input.isEmpty()){
            System.out.println("?");
            System.exit(0);
        }

        int[] count = new int[26];
        for(int i = 0; i < input.length(); i++){
            count[input.charAt(i)-65]++;
        }

        int max = Arrays.stream(count).max().getAsInt();
        boolean isOverlapped = false;
        for(int i = findIndexOf(Arrays.stream(count).max().getAsInt(), count)+1; i < count.length; i++){
            if(count[i] != 0 && count[i] == max){
                System.out.println("?");
                isOverlapped = true;
                break;
            }
        }

        if(!isOverlapped){
            int result = findIndexOf(Arrays.stream(count).max().getAsInt(), count);
            System.out.printf("%c\n", result+65);
        }
    }

// https://stackoverflow.com/questions/6171663/how-to-find-the-index-of-an-element-in-an-int-array
    static int findIndexOf(int V, int[] arr) {
        return IntStream.range(0, arr.length)
                .filter(i->arr[i]==V)
                .findFirst()
                .getAsInt();
    }
}

어려운 문제인줄 알았는데 의외로 쉽게 풀었음 ㅇㅇ

1 Like
JavaScript
var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
    const base = Array.from(Array(26)).map((x, y) => y + 65).map(x=>String.fromCharCode(x))
    const init = lines.join("").toUpperCase().split("")
    const temp = []
    
    let i=0
    
    while(i<base.length){
        temp.push(init.filter(x=>x===base[i]).length)
        i+=1
    }
              
    const max = Math.max(...temp)
    
    temp.filter(x=>x===max).length>1
	?console.log("?")
	:console.log(base[temp.indexOf(max)])
});

풀다보니 재밌어서 계속 풀게되네요;