PROGRAMMERS-962 행성에 불시착한 우주비행사 머쓱이는 외계행성의 언어를 공부하려고 합니다. 알파벳이 담긴 배열 spell
과 외계어 사전 dic
이 매개변수로 주어집니다. spell
에 담긴 알파벳을 한번씩만 모두 사용한 단어가 dic
에 존재한다면 1, 존재하지 않는다면 2를 return하도록 solution 함수를 완성해주세요.
spell
과 dic
의 원소는 알파벳 소문자로만 이루어져있습니다.spell
의 크기 ≤ 10spell
의 원소의 길이는 1입니다.dic
의 크기 ≤ 10dic의
원소의 길이 ≤ 10spell
의 원소를 모두 사용해 단어를 만들어야 합니다.spell
의 원소를 모두 사용해 만들 수 있는 단어는 dic
에 두 개 이상 존재하지 않습니다.dic
과 spell
모두 중복된 원소를 갖지 않습니다.spell | dic | result |
---|---|---|
["p", "o", "s"] | ["sod", "eocd", "qixm", "adio", "soo"] | 2 |
["z", "d", "x"] | ["def", "dww", "dzx", "loveaw"] | 1 |
["s", "o", "m", "d"] | ["moos", "dzx", "smm", "sunmmo", "som"] | 2 |
입출력 예 #1
입출력 예 #2
입출력 예 #3
유의사항
using System;
using System.Linq;
public class Solution {
public int solution(string[] spell, string[] dic) {
int answer = 2;
foreach(string dicC in dic)
{
bool check = true;
foreach(string spellC in spell)
{
if(dicC.IndexOf(spellC) == -1)
{
check = false;
break;
}
}
if(check == true)
{
answer = 1;
break;
}
}
// 다른 풀이
// int answer = dic.Any(x => string.Concat(x.OrderBy(o => o)) == string.Concat(spell.OrderBy(z => z))) ? 1 : 2;
return answer;
}
}
출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges