알고리즘/SWEA

[SWEA] 7272번 : 안경이 없어!

코딍코딍 2023. 11. 10. 23:14

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWl0ZQ8qn7UDFAXz

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

문제

 

입력

 

출력

 

 


소스코드

import java.io.*;
import java.util.*;

class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st;
        HashMap<Character, Integer> map = new HashMap<>();

        map.put('A', 1);map.put('B', 2);map.put('C', 0);
        map.put('D', 1);map.put('E', 0);map.put('F', 0);
        map.put('G', 0);map.put('H', 0);map.put('I', 0);
        map.put('J', 0);map.put('K', 0);map.put('L', 0);
        map.put('N', 0);map.put('M', 0);map.put('O', 1);
        map.put('P', 1);map.put('Q', 1);map.put('R', 1);
        map.put('S', 0);map.put('T', 0);map.put('U', 0);
        map.put('V', 0);map.put('W', 0);map.put('X', 0);
        map.put('Y', 0);map.put('Z', 0);

        int t = Integer.parseInt(br.readLine());
        for (int i = 0; i < t; i++) {
            st = new StringTokenizer(br.readLine());
            String str1 = st.nextToken();
            String str2 = st.nextToken();

            String result = "DIFF";
            boolean flag = false;
            if(str1.length() == str2.length()) {
                for (int j = 0; j < str1.length(); j++) {
                    if(map.get(str1.charAt(j)) != map.get(str2.charAt(j))) {
                        flag = true;
                        break;
                    }
                }
                if(!flag) result = "SAME";
            }

            sb.append("#" + (i + 1) + " " + result + "\n");
        }

        System.out.println(sb);
    }
}

 

해결 방법

  1. 각 알파벳을 키로 삼고, 해당 알파벳의 구멍 개수를 값으로 두는 Map을 생성한다.
  2. 만약 입력받은 두 문자열의 길이가 동일하다면, 각 문자의 대응 위치에 있는 문자가 동일한지를 확인하여 두 문자열이 동일한지 여부를 판단한다.
  3. 만약 입력받은 두 문자열의 길이가 다르다면, 두 문자열은 동일한 문자열이 될 수 없다.