Solution 1.
object Solution {
def solution(s: String): String = {
val sorted_str: String = s.sorted + "1"
def func(idx: Int, res: String = ""): String = {
if (idx >= s.length) res
else {
if (sorted_str(idx) == sorted_str(idx + 1)) func(idx + 2, res)
else if ((idx!=0)&&(sorted_str(idx) == sorted_str(idx - 1))) func(idx + 2, res)
else func(idx + 1, res + sorted_str(idx))
}
}
func(0)
}
}
첫번째 코드는 테스트 케이스들은 통과했는데 실제 테스트에서는 10개 중에서 3개 정도가 틀렸다 ..
위의 코드의 문제는 3,4개 이상이 나왔을 때 잡지 못한다는 점이다.

Solution 2
def solution(s: String): String = {
/**
* 딱 한 번만 주어진 문자열을 리턴하는 것.
* 두 번 이상 나온 문자들만 모은 문자열
*/
def func(idx:Int, one:String ="", two:String=""):String ={
if(idx==s.length) two
else {
if(one.contains(s(idx))) func(idx+1, one, two+s(idx))
else func(idx+1,one+s(idx),two)}
}
val stand:String = func(0)
def filter(idx:Int, res:String=""):String={
if(idx==s.length) res
else{
if(stand.contains(s(idx))) filter(idx+1,res)
else filter(idx+1, res+s(idx))
}
}
filter(0).sorted
}
이건 통과한 코드!
'프로그래밍 > 프로그래머스' 카테고리의 다른 글
프로그래머스 등수 매기기 (1) | 2022.12.08 |
---|---|
숨어있는 숫자의 덧셈 solution code (0) | 2022.12.01 |
다항식 더하기 solution code (0) | 2022.12.01 |
최빈값 return (0) | 2022.11.21 |
11/18 FRI 프로그래머스 분수의 덧셈 (0) | 2022.11.18 |