Talentd
FleetCode

3945. Digit Frequency Score

Easy90.7% acceptance

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:

  • The digit 1 appears 1 time, contributing 1 * 1 = 1.
  • The digit 2 appears 2 times, contributing 2 * 2 = 4.
  • Thus, the score of n is 1 + 4 = 5.

Example 2:

Input: n = 101

Output: 2

Explanation:

  • The digit 0 appears 1 time, contributing 0 * 1 = 0.
  • The digit 1 appears 2 times, contributing 1 * 2 = 2.
  • Thus, the score of n is 2.

Constraints:

  • 1 <= n <= 109

Hints open one at a time

Hint 1
The answer is the sum of the digits.
3:28
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def digitFrequencyScore(self, n: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
122
Input
101
Your code is saved on this device.Solution