LeetCode-in-Java

3945. Digit Frequency Score

Easy

You are given an integer n.

The score of n is defined as the sum of d * freq(d) over all distinct digits d, where freq(d) denotes the number of times the digit d appears in n.

Return an integer denoting the score of n.

Example 1:

Input: n = 122

Output: 5

Explanation:

Example 2:

Input: n = 101

Output: 2

Explanation:

Constraints:

Solution

public class Solution {
    public int digitFrequencyScore(int n) {
        int sum = 0;
        while (n > 0) {
            sum += (n % 10);
            n /= 10;
        }
        return sum;
    }
}