public int[] solution(String[] gems) {
Map<String, Integer> gemCount = new HashMap<>();
Set<String> uniqueGems = new HashSet<>();
Queue<String> gemQueue = new LinkedList<>();
int minLength = gems.length + 1;
int start = 0;
int startIndex = 0;
uniqueGems.addAll(Arrays.asList(gems));
for (int i = 0; i < gems.length; i++) {
gemCount.put(gems[i], gemCount.getOrDefault(gems[i], 0) + 1);
gemQueue.add(gems[i]);
while (gemCount.get(gemQueue.peek()) > 1) {
gemCount.put(gemQueue.peek(), gemCount.get(gemQueue.poll()) - 1);
startIndex++;
}
if (gemCount.size() == uniqueGems.size() && minLength > (i - startIndex)) {
minLength = i - startIndex;
start = startIndex + 1;
}
}
return new int[] { start, start + minLength };
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/67258