본문 바로가기
프로그래밍 연구/백준문제

[백준] 10809번 알파벳 찾기

by 꽈악 2022. 3. 28.
import java.util.Scanner;

public class Ex10809 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		 
		char[] std = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
		
		String a = in.next();
		in.close();
		
		String result = "";
		
		int k = -1;
		
		for(int i = 0; i < std.length; i++) {
			k = -1;
			for(int j = 0; j < a.length(); j++) {
				if(std[i] == a.charAt(j)) {
					k = j;
					break;
				} 
			}

			result = result + k + " ";

		}
		
		System.out.print(result);
	}

}

 

 

다른 해답

public class Main {
 
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
		int[] arr = new int[26];
		
		for(int i = 0; i < arr.length; i++) {
			arr[i] = -1;
		}
 
		String S = br.readLine();
 
		for(int i = 0; i < S.length(); i++) {
			char ch = S.charAt(i);
    
			if(arr[ch - 'a'] == -1) {	// arr 원소 값이 -1 인 경우에만 초기화
				arr[ch - 'a'] = i;
			}
		}
 
		for(int val : arr) {	// 배열 출력
			System.out.print(val + " ");
		}
	}
}

 

 

피드백

- BufferedReader를 사용하기!

- [ch - 'a'] 는 알바벳의 순서를 뜻한다.

 

'프로그래밍 연구 > 백준문제' 카테고리의 다른 글

[백준] 2908번 상수  (0) 2022.04.12
[백준] 1152번 단어의 개수  (0) 2022.04.11
[백준] 1157번 문자열 '단어공부'  (0) 2022.03.31
[백준] 2675번 문자열 반복  (0) 2022.03.29