プログラマレベル2)メニューの更新


リンク:https://programmers.co.kr/learn/courses/30/lessons/72411
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
class Solution {
    	static HashMap<String, Integer> answer;

	static void combination(char[] arr, boolean[] visited, int start, int n, int r) {

		if (r == 0) {
			print(arr, visited, n);
			return;
		}

		for (int i = start; i < n; i++) {
			visited[i] = true;
			combination(arr, visited, i + 1, n, r - 1);
			visited[i] = false;
		}
	}

	// 배열 출력
	static void print(char[] arr, boolean[] visited, int n) {
		StringBuilder sb = new StringBuilder();
		ArrayList<Character> copy_list = new ArrayList<Character>();
		for (int i = 0; i < n; i++) {
			if (visited[i]) {
				copy_list.add(arr[i]);
			}
		}

		Collections.sort(copy_list);
		for (int i = 0; i < copy_list.size(); i++) {
			sb.append(copy_list.get(i));
		}
		if (answer.containsKey(sb.toString()))
			answer.put(sb.toString(), answer.get(sb.toString()) + 1);
		else
			answer.put(sb.toString(), 1);
	}

    public String[] solution(String[] orders, int[] course) {
       char[] arr;
		boolean[] visited;
		ArrayList<String> max_list=new ArrayList<String>();
		for (int j = 0; j <= course.length - 1; j++) {
			 answer = new HashMap<>();
			for (int i = 0; i < orders.length; i++) {
				arr = orders[i].toCharArray();
				visited = new boolean[arr.length];
				int n = arr.length;
				combination(arr, visited, 0, n, course[j]);
			}
			if(!answer.isEmpty()) {
				int maxValue = Collections.max(answer.values());
				for(Map.Entry<String, Integer> m : answer.entrySet()) {
					if(m.getValue()==maxValue &&m.getValue()>1) {
						max_list.add(m.getKey());
					}
				}
			}


		}
		String[] answer = new String[max_list.size()];
		Collections.sort(max_list);
		for (int i = 0; i < max_list.size(); i++) {
			answer[i] = max_list.get(i);
		}
        return answer;
    }
}