✔️ 내 답-> 오답
import java.util.*;
public class p10 {
public static int[] solution(String in, String c ) {
in = in.replace(c,"0");
int j =0;
int[] arr = new int[in.length()];
for(!arr.isEmpty()){
int i =j;
j = in.indexOf("0",i);
arr[in.indexOf("0",i)]=0;
if( (j-i-1)%2==0){
for(int x =1;x<=(j-i-1)/2;x++)
arr[i+x]=x;
arr[j-x]=x;
}
else {
for(int x =1;x<=(j-i-1)/2;x++){
arr[i+x]=x;
arr[j-x]=x;
}
arr[i+(j-i-1)/2+1] =(j-i-1)/2+1;
}
return Arrays.toString(arr);
}
}
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
String in = kb.next();
String c = kb.next();
System.out.println(solution(in,c));
}
}
강의 보고 힌트 얻어서 다시 풀어본
import java.util.*;
public class Main {
public static int[] solution(String in, char c ) {
int p=1000;
int[] arr = new int[in.length()];
for(int i=0;i<in.length();i++) {
if(in.charAt(i)==c) {
p=0;
arr[i]=p;
}
else {
p++ ;
arr[i]=p;
}
}
p=1000;
for(int j=in.length()-1;j>=0;j--) {
if(in.charAt(j)!=c) {
p++;
if(arr[j]>p) arr[j]=p;
}
else{
p=0;
}
}
return arr;
}
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
String in = kb.next();
char c = kb.next().charAt(0);
for(int x : solution(in,c)){
System.out.printf("%d ",x);
}
}
}