CodingTest/Programmers Lv.1
[프로그래머스 C#] Lv.1 대충 만든 자판
주녘
2023. 8. 30. 12:42
https://school.programmers.co.kr/learn/courses/30/lessons/160586
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 정답코드 및 핵심 아이디어, 유의사항
- A ~ Z 까지의 키 입력 필요 횟수를 딕셔너리에 미리 저장
using System;
using System.Collections.Generic;
public class Solution
{
public int[] solution(string[] keymap, string[] targets)
{
List<int> answerList = new List<int>();
Dictionary<char, int> dict = new Dictionary<char, int>();
// 사전에 해당 알파벳에 따른 결과 값을 저장
for (char ch = 'A'; ch <= 'Z'; ch++)
{
// 사전에 넣기 위한 최소 값 찾기
int minIndex = 999;
foreach (string str in keymap)
{
int currIndex = str.IndexOf(ch);
if (currIndex != -1 && currIndex < minIndex)
minIndex = currIndex;
}
// 만약, 못 찾을 경우 사전에 추가하지 않음
if (minIndex != 999)
dict.Add(ch, minIndex + 1);
}
// 사전에 검색해서 체크
for (int i = 0; i < targets.Length; i++)
{
int total = 0;
for (int j = 0; j < targets[i].Length; j++)
{
if (dict.ContainsKey(targets[i][j]))
total += dict[targets[i][j]];
else
{
total = -1;
break;
}
}
answerList.Add(total);
}
return answerList.ToArray();
}
}