C# 공부/[C#] 문제풀이

[C# 문제풀이 1일차] 숫자 짝꿍

햅2024 2024. 11. 7. 11:10

문제 설명


두 정수 X, Y의 임의의 자리에서 공통으로 나타나는 정수 k(0 ≤ k ≤ 9)들을 이용하여 만들 수 있는 가장 큰 정수를 두 수의 짝꿍이라 합니다(단, 공통으로 나타나는 정수 중 서로 짝지을 수 있는 숫자만 사용합니다). X, Y의 짝꿍이 존재하지 않으면, 짝꿍은 -1입니다. X, Y의 짝꿍이 0으로만 구성되어 있다면, 짝꿍은 0입니다.

예를 들어, X = 3403이고 Y = 13203이라면, X와 Y의 짝꿍은 X와 Y에서 공통으로 나타나는 3, 0, 3으로 만들 수 있는 가장 큰 정수인 330입니다. 다른 예시로 X = 5525이고 Y = 1255이면 X와 Y의 짝꿍은 X와 Y에서 공통으로 나타나는 2, 5, 5로 만들 수 있는 가장 큰 정수인 552입니다(X에는 5가 3개, Y에는 5가 2개 나타나므로 남는 5 한 개는 짝 지을 수 없습니다.)
두 정수 X, Y가 주어졌을 때, X, Y의 짝꿍을 return하는 solution 함수를 완성해주세요.

더보기

제한사항


3 ≤ X, Y의 길이(자릿수) ≤ 3,000,000입니다.
X, Y는 0으로 시작하지 않습니다.
X, Y의 짝꿍은 상당히 큰 정수일 수 있으므로, 문자열로 반환합니다.


입출력 예


X Y result
"100" "2345" "-1"
"100" "203045" "0"
"100" "123450" "10"
"12321" "42531" "321"
"5525" "1255" "552"


입출력 예 설명


입출력 예 #1
X, Y의 짝꿍은 존재하지 않습니다. 따라서 "-1"을 return합니다.


입출력 예 #2
X, Y의 공통된 숫자는 0으로만 구성되어 있기 때문에, 두 수의 짝꿍은 정수 0입니다. 따라서 "0"을 return합니다.


입출력 예 #3
X, Y의 짝꿍은 10이므로, "10"을 return합니다.


입출력 예 #4
X, Y의 짝꿍은 321입니다. 따라서 "321"을 return합니다.


입출력 예 #5
지문에 설명된 예시와 같습니다.

 


 

주어진 각 숫자를 listX와 listY로 나눈 후 두 list를 비교하여 숫자짝꿍을 알아내야 할 것이다.

 

1.X와 Y를 각각의 list로 나눈다.

List<int> listX = new List<int>();
List<int> listY = new List<int>();
for(int i=0;i<X.Length;i++)
{
    listX.Add(Convert.ToInt32(X.Substring(i,1)));     
}
for(int i=0;i<Y.Length;i++)
{
    listY.Add(Convert.ToInt32(Y.Substring(i,1)));     
}

 

2.listX와 listY를 비교한다. 여기서 listY를 tempListY로 임시 복사하여, 이미 비교된 값은 제거해준다.

List<int> commonValues = new List<int>();
List<int> tempListY = new List<int>(listY);

foreach (int item in listX)
{
    if (tempListY.Contains(item))
    {
        commonValues.Add(item);
        tempListY.Remove(item); // 이미 비교된 값은 제거하여 중복 처리
    }
}

 

3.숫자 짝꿍이 없으면 -1을 출력하고, list를 내림차순으로 정리한 후 숫자를 만든다.

 if(commonValues.Count == 0)
{
    result = "-1";
}
else
{
    commonValues.Sort();  
    commonValues.Reverse();
    for(int i=0;i<commonValues.Count;i++)
    {
        result += commonValues[i];
    }

}

 

여기까지 하면 끝난 줄 알았으나... "00"이 return 될 경우 틀렸다고 떴다ㅠㅠㅠ 따라서 00일 경우 0으로 리턴하기 위한 부분을 추가로 코딩하였다.

 int nTemp = -1;
        
try{
    nTemp = Convert.ToInt32(result);
}
catch{

}

if(nTemp == 0) result = "0";

 

 

전체 코드

더보기
using System;
using System.Collections.Generic;
using System.Linq;

public class Solution 
{
    public string solution(string X, string Y) 
    {
        string result = "";
        List<int> listX = new List<int>();
        List<int> listY = new List<int>();
        for(int i=0;i<X.Length;i++)
        {
            listX.Add(Convert.ToInt32(X.Substring(i,1)));     
        }
        for(int i=0;i<Y.Length;i++)
        {
            listY.Add(Convert.ToInt32(Y.Substring(i,1)));     
        }
                      
        //List<int> commonValues = listX.Intersect(listY).ToList();
        List<int> commonValues = new List<int>();
        List<int> tempListY = new List<int>(listY);

       foreach (int item in listX)
        {
            if (tempListY.Contains(item))
            {
                commonValues.Add(item);
                tempListY.Remove(item); // 이미 비교된 값은 제거하여 중복 처리
            }
        }
            
        if(commonValues.Count == 0)
        {
            result = "-1";
        }
        else
        {
            commonValues.Sort();  
            commonValues.Reverse();
            for(int i=0;i<commonValues.Count;i++)
            {
                result += commonValues[i];
            }
        
        }
        
        
        int nTemp = -1;
        
        try{
            nTemp = Convert.ToInt32(result);
        }
        catch{
            
        }
        
        if(nTemp == 0) result = "0";
        
        return result;
    }
}