230817 가장 가까운 같은 글자

Jongleee·2023년 8월 17일
0

TIL

목록 보기
340/576
public int[] solution(String s) {
	int[] answer = new int[s.length()];
	Map<Character, Integer> map = new HashMap<>();

	for (int i = 0; i < s.length(); i++) {
		char c = s.charAt(i);

		if (map.containsKey(c)) {
			answer[i] = i - map.get(c);
		} else {
			answer[i] = -1;
		}

		map.put(c, i);
	}

	return answer;
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/142086

0개의 댓글