import java.util.*;
class Main {
public int[] solution(String str, String c) {
int[] answer = new int[str.length()];
for (int i = 0; i < str.length(); i++)
answer[i] = Integer.MAX_VALUE;
int index = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c.charAt(0)) {
index = i;
for (int j = 0; j < str.length(); j++) {
if (answer[j] > Math.abs(index - j))
answer[j] = Math.abs(index - j);
}
}
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
for (int num : T.solution(kb.next(), kb.next())) {
System.out.print(num + " ");
}
}
}
import java.util.*;
class Main {
public int[] solution(String str, String c) {
int[] answer = new int[str.length()];
int p = 1000;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == c.charAt(0))
p = 0;
answer[i] = p;
p++;
}
for (int i = str.length() - 1; i >= 0; i--) {
if (str.charAt(i) == c.charAt(0))
p = 0;
answer[i]=Math.min(answer[i], p);
p++;
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
for (int num : T.solution(kb.next(), kb.next())) {
System.out.print(num + " ");
}
}
}
'알고리즘 > JAVA' 카테고리의 다른 글
12. 암호 (0) | 2021.08.31 |
---|---|
11. 문자열 압축 (0) | 2021.08.31 |
9. 숫자만 추출 (0) | 2021.08.31 |
8. 유효한 팰린드롬 (0) | 2021.08.31 |
7. 회문 문자열 (0) | 2021.08.31 |
댓글