Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

KSI일기장

프로그래머스 중복된 문자 제거 (자바) 본문

JavaAlgorithm

프로그래머스 중복된 문자 제거 (자바)

MyDiaryYo 2023. 12. 20. 15:56

 

 

 

class Solution {
    public String solution(String my_string) {
        String[] st = my_string.split("");	//문자열 my_string을 배열로 변환
        String answer = "";

        for (int i = 0; i < st.length; i++) {	//배열 st를 하나씩 반복해 접근(i)
            int count = 0;

            for (int j = 0; j < st.length; j++) {	//배열 st를 하나씩 반복해 접근해(j) 위에서 가져온 i와 비교
                if (st[i].equals(st[j])) {		//st[i]와 st[j]가 같은 경우
                    count++;				//카운트 1씩 증가
                    if (count > 1){			//카운트가 2이상인 경우(st[i]와 st[j]가 같은게 2개 이상인 경우)
                        st[j] = "";			//st[j]를 빈문자열( "" )로 만든다
                    }
                }
            }
        }
        answer = String.join("", st);			//배열 st를 문자열로 변화해 answer에 넣어준다
        return answer;
    }
}

 

 

class Solution {
    public String solution(String my_string) {
        String answer = "";
        for(int i=0; i<my_string.length(); i++){	//문자열 my_string를 하나씩 반복 접근
            if(!answer.contains(String.valueOf(my_string.charAt(i)))){	//answer에 위에서 가져온 my_string 문자 하나가 포함되어있지 않은 경우
                answer += my_string.charAt(i);	//포함되있지 않은 해당 문자를 answer에 추가
            }
        }
        return answer;
    }
}