Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find the Longest Consecutive Sequence of the Same Digit

Basic Acceptance 84.75% Points 10.00

You are given a non-negative integer n. Your task is to return the length of the longest run of identical consecutive digits in the number.

A run is a sequence of two or more identical digits appearing consecutively. If no digit repeats consecutively, the longest run has a length of 1.

Examples
Example 1

Input: n = 1112233
Output: 3
Explanation: The digit 1 appears three times consecutively, forming the longest run of length 3.

Example 2

Input: n = 4455566
Output: 3
Explanation: The digit 5 appears three times consecutively, forming the longest run of length 3.

Example 3

Input: n = 1234
Output: 1
Explanation: No digit repeats consecutively, so the longest run has a length of 1.

Constraints
  • 0 = n = 10¹8
Companies
Amazon ServiceNow Morgan Stanly
Topics
Data Structures Basics Array String + 1 more
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions