• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

구간을 나누어 조건부로 처리해보았습니다 크크

23.08.25 12:42 작성 23.08.25 13:51 수정 조회수 306

0

- 학습 관련 질문을 남겨주세요. 상세히 작성하면 더 좋아요!
- 먼저 유사한 질문이 있었는지 검색해보세요.
- 서로 예의를 지키며 존중하는 문화를 만들어가요.
- 잠깐! 인프런 서비스 운영 관련 문의는 1:1 문의하기를 이용해주세요.

 

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String word = sc.next();
        char chr = sc.next().charAt(0);
        
        System.out.println(solution(word, chr));
    }

    public static String solution(String word, char chr) {
        String answer = "";

        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < word.length(); i++) {
            if (word.charAt(i) == chr)
                list.add(i);
        }

        int index = 0;

        for (int i = 0; i < word.length(); i++) {

            if(i == list.get(index+1)
                && i!= list.get(list.size()-1))
                index++;

            if(i < list.get(0))
                answer += list.get(0)-i;

            else if(i == list.get(index))
                answer += 0;

            else if(i > list.get(list.size()-1))
                answer += i - list.get(list.size()-1);

            else
                answer += Math.min((i-list.get(index)), (list.get(index+1)-i));

            answer += " ";
                
        }

        return answer;
    }
}

답변 1

답변을 작성해보세요.

0

안녕하세요^^

잘 하셨습니다. 다만 아래와 같은 입력은 에러가 나네요.

abceabc e

한상민님의 프로필

한상민

질문자

2023.08.26

허걱 감사합니다.......😭