CodingTest/Programmers Lv.1
[프로그래머스 C#] Lv.1 숫자 짝꿍
주녘
2023. 8. 18. 17:53
https://school.programmers.co.kr/learn/courses/30/lessons/131128
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 정답코드 및 핵심 아이디어, 유의사항
- Lv.1이라고 굉장히 쉽게 생각하고 생각없이 string을 사용했다가 시간초과를 경험함
https://godgjwnsgur7.tistory.com/66
[C#] StringBuilder vs String 성능차이 비교
godgjwnsgur7.tistory.com
using System;
using System.Linq;
using System.Text;
public class Solution
{
public string solution(string X, string Y)
{
StringBuilder sb = new StringBuilder();
int[] ints1 = new int[10];
int[] ints2 = new int[10];
foreach (Char c in X)
ints1[c - '0']++;
foreach (Char c in Y)
ints2[c - '0']++;
for (int i = 9; i >= 0; i--)
{
int count = ints1[i] < ints2[i] ? ints1[i] : ints2[i];
for (int j = 0; j < count; j++)
sb.Append(i);
}
if (sb.Length == 0)
return "-1";
if (sb.ToString().ToCharArray().Where<Char>(x => x == '0').Count() == sb.Length)
return "0";
return sb.ToString();
}
}