[알고리즘/C#] 프로그래머스 - 할인 행사 (Lv.2)

0시0분·2024년 8월 16일
0

알고리즘

목록 보기
20/23

🎈 풀이 방법
키를 string으로 하는 딕셔너리를 사용하면 쉽게 해결할 수 있는 문제였다.

using System;
using System.Collections.Generic;

public class Solution {
    public int solution(string[] want, int[] number, string[] discount)
    {
        int answer = 0;

        Dictionary<string, int> wantList = new Dictionary<string, int>();
        for (int i = 0; i < want.Length; ++i)
            wantList[want[i]] = number[i];

        Dictionary<string, int> discountList = new Dictionary<string, int>();
        for (int j = 0; j <= discount.Length - 10; ++j)
        {
            discountList.Clear();

            for (int i = 0; i < 10; ++i)
            {
                if (discountList.ContainsKey(discount[j + i]))
                    discountList[discount[j + i]]++;
                else
                    discountList[discount[j + i]] = 1;

            }

            bool membership = true;
            for (int i = 0; i < want.Length; ++i)
            {
                if (discountList.ContainsKey(want[i]) == false || wantList[want[i]] != discountList[want[i]])
                {
                    membership = false;
                    break;
                }
            }
            if (membership)
                answer++;
        }

        return answer;
    }
}

0개의 댓글