LeetCode Courses 3870. Count Commas in Range
LeetCode

3870. Count Commas in Range

We iterate from an f-string brute-force count to a closed-form one-liner by partitioning the input range. The "partition the input space" pattern crops up in counting problems.

1 chapter Beginner Written walk-through Updated 26 May 2026 Phil · @FirnoxGames
Free

Free for everyone. No account needed.

Start reading →

What you'll learn

1 3870. Count Commas in Range 📖 6 min read

We iterate from an f-string brute-force count to a closed-form one-liner by partitioning the input range. The "partition the input space" pattern crops up in counting problems.

In this chapter
  1. Problem
  2. Ridiculous brute force
  3. Using logic
  4. Without branching

Problem

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.
Included with this course
📖Written version of every chapter