
첫 번째 글자는 무조건 -1을 넣어줍니다.
두 번째 글자부터 순서대로 반복문으로 돕니다.
자신의 바로 앞까지의 배열 안에 자신이 존재하는지 확인합니다
만약 없다면 -1을 넣고 다음으로 넘어갑니다.
만약 있다면 마지막 배열부터 순서대로 자신과 동일한지 확인하고 아니라면 앞으로 넘어갑니다.
import java.util.*;
import java.util.stream.*;
class Solution {
public int[] solution(String s) {
int[] answer = new int[s.length()];
answer[0] = -1;
List<Character> charList = s.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.toList());
for (int i = 1; i < s.length(); i ++) {
Character c = s.charAt(i);
List<Character> subCharList = charList.subList(0, i);
if (subCharList.contains(c)) {
for (int j = subCharList.size()-1; j >= 0; j --) {
if (subCharList.get(j) == c) {
answer[i] = subCharList.size()-j;
break;
}
}
}
else {
answer[i] = -1;
}
}
return answer;
}
}
