99클럽 코테스터디 6기 08일차 TIL (LeetCode 2283)

glory_young·2025년 4월 9일
post-thumbnail

문제접근

문제 : LeetCode 2283. Check if Number Has Equal Digit Count and Digit Value
(https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/description/)
유형 : 문자열/해시

1) 입력된 문자열에서 각 숫자가 몇번 나왔는지 카운트 한다
2) 카운트한 값과 입력된 문자열을 비교한다

제출코드

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Solution {
public:
    bool digitCount(string num) {
        vector<int> numCount(10, 0);
        string numTrue;
        int n = num.length();

        for (int i = 0; i < n; i++) {
            int m = (int)num[i] - '0';
            numCount[m]++;
        }
        for (int i = 0; i < n; i++) {
            numTrue += (char)(numCount[i] + '0');
        }
        if (numTrue == num)
            return true;
        else
            return false;
    }
};

문제해결

시간복잡도 : O(n)
문자형과 정수형을 섞어서 코드를 작성할 때 실수하지 않도록 주의 하기


오늘의 회고

매일 꾸준히 공부하기

0개의 댓글