https://school.programmers.co.kr/learn/courses/30/lessons/120888
문자열 my_string
이 매개변수로 주어집니다. my_string
에서 중복된 문자를 제거하고 하나의 문자만 남긴 문자열을 return하도록 solution 함수를 완성해주세요.
1 ≤ my_string ≤ 110
my_string은 대문자, 소문자, 공백으로 구성되어 있습니다.
대문자와 소문자를 구분합니다.
공백(" ")도 하나의 문자로 구분합니다.
중복된 문자 중 가장 앞에 있는 문자를 남깁니다.
my_string | result |
---|---|
"people" | "peol" |
"We are the world" | "We arthwold" |
"people"에서 중복된 문자 "p"와 "e"을 제거한 "peol"을 return합니다.
"We are the world"에서 중복된 문자 "e", " ", "r" 들을 제거한 "We arthwold"을 return합니다.
class Solution {
public String solution(String my_string) {
String answer = "";
String temp=my_string;
boolean isOnly=false;
for(int i=0;i<my_string.length();i++){
for(int j=i;j<my_string.length();j++){
if(answer.contains(String.valueOf(my_string.charAt(j)))){
break;
}
else{
answer=answer+my_string.charAt(i);
break;
}
}
}
return answer;
}
}
.contains()
를 이용해서 하나씩 answer에 저장해두고, 만약 같은 거 있으면 넘어가고 answer에 없으면 저장하는 식으로 코딩했다!