152. A로 B 만들기

김해준's avatar
Dec 30, 2025
152. A로 B 만들기

사용언어 : Java

문제
notion image
풀이
/* 아 그냥 뒤집어서 같은지 체크만 하면 되는줄 알았는데 그게 아니네 이거 HashMap을 쓸까? Dictionary를 쓸까? 문자열 이거 문자로 전부 쪼개서 한글자씩 갯수 체크하면 될듯 */ import java.util.*; class Solution { public int solution(String before, String after) { String[] arr1 = before.split(""); String[] arr2 = after.split(""); HashMap<String,Integer> map = new HashMap(); for(int i = 0 ; i < arr1.length; i++) { if(map.get(arr1[i]) == null) { map.put(arr1[i] , 1); } else { map.put(arr1[i], map.get(arr1[i]) + 1); } } for(int i = 0 ; i < arr2.length; i++) { if(map.get(arr2[i]) == null)return 0; map.put(arr2[i], map.get(arr2[i]) - 1); if(map.get(arr2[i]) < 0)return 0; } return 1; } }
notion image
Share article