반응형
import java.util.*;
/*
* 알파벳 소문자로만 이루어진 단어 S가 주어진다.
* 각각의 알파벳에 대해서, 단어에 포함되어 있는 경우에는 처음 등장하는 위치를,
* 포함되어 있지 않은 경우에는 -1을 출력하는 프로그램을 작성하시오.
*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String word = scan.next();
List<Character> chrList = new ArrayList<Character>(Arrays.asList('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'));
List<Integer> numList = new ArrayList<Integer>(Arrays.asList(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1));
for (int i = 0; i < word.length(); i++) {
if (chrList.contains(word.toCharArray()[i])) {
int idx = chrList.indexOf(word.toCharArray()[i]);
if (numList.get(idx) == -1)
numList.set(idx, i);
}
}
for (int i = 0; i < chrList.size(); i++) {
System.out.print(numList.get(i));
if (i != numList.size() - 1)
System.out.print(' ');
}
}
}
입력받은 단어의 알파벳 INDEX를 출력해야함.
나는 알파벳 INDEX를 비교하기위해서 나는 LIST를 만들었는데 굳이 이렇게까지 할필요가 없었다.
다른사람들 풀이 보니까 ASKII를 이용하여 풀이하더라..
(입력받은 문자의 각 자리에서 -97하여 INDEX를 찾는방법.)
ASKII 코드 표 ▼
* 혼자 개인적으로 문제 풀고있기 때문에 풀이가 미흡 할 수 있음.
* 코드에 문제될 수 있는 부분이나, 더 좋은 아이디어 댓글에 남겨주시면 감사하게 추가 공부해보겠습니다!