코딩테스트/lv0

[프로그래머스 코딩테스트] java Lv.0 한 번만 등장한 문자

chantleman 2024. 8. 7. 09:22

    import java.util.*;
    class Solution {
        public String solution(String s) {
            String answer = "";
            char [] ch = s.toCharArray();
            Arrays.sort(ch);        

            char temp = ' ';
            int cnt=0;
            for(char c: ch){
                if(c==temp)
                {
                    cnt++;
                }
                else{
                    if(cnt==1)
                    {
                        answer+=temp;

                    }
                    temp=' ';
                        cnt=0;
                    temp=c;
                    cnt++;
                }
            }
            if(cnt==1)
            {
                answer+=temp;
                temp=' ';
                cnt=0;
             }

            return answer;
        }
    }