https://www.acmicpc.net/problem/10809
10809번: 알파벳 찾기
각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출
www.acmicpc.net
public class Boj10809_알파벳찾기 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] arr = new int[26];
Arrays.fill(arr, -1); // -1로 초기화
String s = br.readLine();
// indexOf는 앞에서부터 처음 발견되는 문자의 인덱스 반환
for (int i = 0; i < s.length(); i++) {
if (s.indexOf(s.charAt(i)) == i) arr[s.charAt(i) - 'a'] = i;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] + " ");
}
System.out.println(sb);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
백준 5585 거스름돈 (0) | 2022.02.15 |
---|---|
백준 11719 그대로 출력하기2 (0) | 2022.02.15 |
백준 5598 카이사르 암호 (0) | 2022.02.14 |
백준 2292번 : 벌집 (Python, 파이썬) (0) | 2022.02.14 |
백준 1157 단어공부 (0) | 2022.02.14 |