티스토리 뷰
문제
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
입력
첫째 줄에 알파벳 대소문자로 이루어진 단어가 주어진다. 주어지는 단어의 길이는 1,000,000을 넘지 않는다.
출력
첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?를 출력한다.
후기
처음 Hashmap으로 풀었다가 계속 메모리 에러가 나서, 다른 블로그 글들 확인 후 다시 리팩토링 했다.
alphabetASCIIArray 배열을 만들고 index 26(알파벳 갯수)까지 설정한다.
1) 입력받은 수를 대문자로 모두 변경
2) 아스키코드로 변환 후 배열에 각 알파벳 인덱스에 해당하는 값을 증가시킨다.
3) 배열의 각 값을 비교 후 가장 큰 값의 인덱스를 char로 변환 후 출력
ASCII 관련 문제를 정리할 필요성을 느꼈다.
Java Solution
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Q1157 { public static void main(String[] args) throws IOException { StudyWord studyWord; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String word = br.readLine().toUpperCase(); studyWord = new StudyWord(word); studyWord.searchAlphabetToFindLetter(); char theMostUsedLetter = studyWord.printTheMostUsedLetter(); System.out.println(theMostUsedLetter); } } class StudyWord { private static final int toShiftASCII = 65; private static final char duplication = '?'; private static final int numOfAlphabet = 26; private String word; private int[] alphabetASCIIArray = new int[numOfAlphabet]; private int toCompareTmpLetter = 0; private boolean flagDuplication = true; private int theMostUsedLetterIdx; StudyWord(String word) { this.word = word; } private void countEachAlphabet(int i) { alphabetASCIIArray[word.charAt(i) - toShiftASCII]++; } void searchAlphabetToFindLetter() { for (int i = 0; i < word.length(); i++) { countEachAlphabet(i); findTheMostUsedLetter(i); } } private void findTheMostUsedLetter(int i) { if (toCompareTmpLetter == alphabetASCIIArray[word.charAt(i) - toShiftASCII]) { flagDuplication = true; } if (toCompareTmpLetter < alphabetASCIIArray[word.charAt(i) - toShiftASCII]) { toCompareTmpLetter = alphabetASCIIArray[word.charAt(i) - toShiftASCII]; theMostUsedLetterIdx = i; flagDuplication = false; } } char printTheMostUsedLetter() { if (!flagDuplication) { char theMostUsedLetter = word.charAt(theMostUsedLetterIdx); return theMostUsedLetter; } return duplication; } } |
Java Solution(HashMap)
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; public class Q1157 { public static void main(String[] args) throws IOException { StudyWord studyWord; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String word = br.readLine(); studyWord = new StudyWord(word); studyWord.inputSplitLetterFromUppercase(); String letter = studyWord.countEachLetterInMap(); System.out.println(letter); } } class StudyWord { private static final int baseNumOfLetter = 1; private static final String notOneTheMostLetter = "?"; private String word; private char splitLetterFromWord; private int iterEachLetterCountValue = 0; private int theMostUsedLetterValue = 0; private String theMostUsedLetterKey; private Map<Character, Integer> mapOfLetter = new HashMap<>(); StudyWord(String word) { this.word = word; } void inputSplitLetterFromUppercase() { for (int i = 0; i < word.length(); i++) { String changeUppercase = word.replaceAll("[^a-zA-Z]", "").toUpperCase(); splitLetterFromWord = changeUppercase.charAt(i); inputEachLetterInMap(); } } private void inputEachLetterInMap() { if (flagContainLetter(splitLetterFromWord)) { int num = mapOfLetter.get(splitLetterFromWord); mapOfLetter.put(splitLetterFromWord, baseNumOfLetter + num); } if (!flagContainLetter(splitLetterFromWord)) { mapOfLetter.put(splitLetterFromWord, baseNumOfLetter); } } private boolean flagContainLetter(char splitUppercaseWord) { return mapOfLetter.containsKey(splitUppercaseWord); } String countEachLetterInMap() { for (char tmpEachLetterKey : mapOfLetter.keySet()) { iterEachLetterCountValue = mapOfLetter.get(tmpEachLetterKey); findTheMostUsedLetter(tmpEachLetterKey); } return theMostUsedLetterKey; } private void findTheMostUsedLetter(char tmpEachLetterKey) { if (iterEachLetterCountValue > theMostUsedLetterValue) { theMostUsedLetterValue = iterEachLetterCountValue; theMostUsedLetterKey = Character.toString(tmpEachLetterKey); } else{ theMostUsedLetterKey = notOneTheMostLetter; } } |
Q. 정규 표현식 이란? 자주쓰이는 패턴 : https://highcode.tistory.com/6
[자주 쓰이는 패턴] 1) 숫자만 : ^[0-9]*$ 2) 영문자만 : ^[a-zA-Z]*$ 3) 한글만 : ^[가-힣]*$ 4) 영어 & 숫자만 : ^[a-zA-Z0-9]*$ 5) E-Mail : ^[a-zA-Z0-9]+@[a-zA-Z0-9]+$ 6) 휴대폰 : ^01(?:0|1|[6-9]) - (?:\d{3}|\d{4}) - \d{4}$ 7) 일반전화 : ^\d{2,3} - \d{3,4} - \d{4}$ 8) 주민등록번호 : \d{6} \- [1-4]\d{6} 9) IP 주소 : ([0-9]{1,3}) \. ([0-9]{1,3}) \. ([0-9]{1,3}) \. ([0-9]{1,3}) |
Q. 아스키코드 관련 알고리즘
'Algorithms > BOJ' 카테고리의 다른 글
백준 17298 (0) | 2023.01.11 |
---|---|
Q10972 다음순열 (0) | 2020.03.06 |
백준 Q.1159 농구 경기 (0) | 2020.01.08 |
백준 Q.1302 베스트셀러 (0) | 2020.01.07 |
백준 Q.9375 패션왕 신해빈 (0) | 2020.01.06 |