-
[프로그래머스 - charAt()] 가장 까까운 같은 글자Coding Test 2023. 7. 2. 21:11728x90
class Solution { public int[] solution(String s) { int[] answer = new int[s.length()]; String[] arr = s.split(""); // 주어진 문자열을 하나씩 저장 answer[0] = -1; // 맨 앞은 무조건 -1 for(int i = 1; i < arr.length; i++) { for (int j = i - 1; j >= 0; j--) { if (s.charAt(j) == s.charAt(i)) { // 일치 answer[i] = i - j; break; } else { // 불일치 answer[i] = -1; } } } return answer; } }
728x90반응형'Coding Test' 카테고리의 다른 글
[프로그래머스 - removeIf()] 배열 조각하기 (0) 2023.07.13 [프로그래머스 - 입문] 안전지대 (0) 2023.07.05 [프로그래머스] 체육복 (0) 2023.07.02 [프로그래머스 - 소수] 소수 찾기 (0) 2023.06.25 [프로그래머스 - 해시] 의상 (0) 2023.06.24