Talentd
FleetCode

3870. Count Commas in Range

Easy78.0% acceptance

You are given an integer n.

Return the total number of commas used when writing all integers from [1, n] (inclusive) in standard number formatting.

In standard formatting:

  • A comma is inserted after every three digits from the right.
  • Numbers with fewer than 4 digits contain no commas.

Example 1:

Input: n = 1002

Output: 3

Explanation:

The numbers "1,000", "1,001", and "1,002" each contain one comma, giving a total of 3.

Example 2:

Input: n = 998

Output: 0

Explanation:

All numbers from 1 to 998 have fewer than four digits. Therefore, no commas are used.

Constraints:

  • 1 <= n <= 105

Topics

Hints open one at a time

Hint 1
Numbers in the range [1000, 100000] have one comma.
3:19
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def countCommas(self, n: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
1002
Input
998
Your code is saved on this device.Solution