157. 한 번만 등장한 문자

김해준's avatar
Dec 30, 2025
157. 한 번만 등장한 문자

사용언어 : Java

문제
notion image
풀이
import java.util.*; class Solution { public String solution(String s) { HashMap<Character,Integer> map = new HashMap(); HashSet<Character> set = new HashSet(); for(int i = 0 ; i < s.length(); i++) { char temp = s.charAt(i); if(map.get(temp) == null) map.put(temp,1); else map.put(temp,(map.get(temp)) + 1); set.add(temp); } int count = 0; for(var n : map.values()) if(n == 1)count++; Character[] strArr = new Character[count]; int index = 0; for(var str : set) { if(map.get(str) == 1) { strArr[index] = str; index++; } } Arrays.sort(strArr); String answer = ""; for(int i = 0 ; i < strArr.length; i++) { answer+=strArr[i]; } return answer; } }
notion image
Share article