코딩테스트_c#/프로그래머스
프로그래머스 ) 연습문제 - 대충 만든 자판 Level.1 _C#
노년인생
2024. 2. 14. 15:49
728x90
반응형
대충 만든 자판 문제
내 코드
using System;
using System.Collections.Generic;
public class Solution {
public int[] solution(string[] keymap, string[] targets)
{
Dictionary<char, int > dic = new Dictionary<char, int>();
List<int> answer = new List<int>();
int num = 0;
//알파벳별 최소순서 정리
for (int i = 0; i < keymap.Length; i++)
{
for (int j = 0; j < keymap[i].Length; j++)
{
if (!dic.ContainsKey(keymap[i][j]))
{
dic.Add(keymap[i][j], j+1);
}
else
{
if(dic[keymap[i][j]] > j+1)
{
dic[keymap[i][j]] = j+1;
}
}
}
}
//확인
for (int i = 0; i < targets.Length; i++)
{
for (int j = 0; j < targets[i].Length; j++)
{
if(dic.ContainsKey(targets[i][j]))
{
num += dic[targets[i][j]];
}
else
{
num = -1;
break;
}
}
answer.Add(num);
num = 0;
}
return answer.ToArray();
}
}
728x90
반응형