Skip to main content

Valid Digit Number - Video Solutions

EasyMath

3908. Valid Digit Number (Leetcode Easy)

7 video solutions available

Valid Digit Number - Video Solution

Watch 7 video solutions for Valid Digit Number, a easy level problem involving Math. This walkthrough by Programming Live with Larry has 146 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

You are given an integer n and a digit x.

A number is considered valid if:

  • It contains at least one occurrence of digit x, and
  • It does not start with digit x.

Return true if n is valid, otherwise return false.

 

Example 1:

Input: n = 101, x = 0

Output: true

Explanation:

The number contains digit 0 at index 1. It does not start with 0, so it satisfies both conditions. Thus, the answer is true​​​​​​​.

Example 2:

Input: n = 232, x = 2

Output: false

Explanation:

The number starts with 2, which violates the condition. Thus, the answer is false.

Example 3:

Input: n = 5, x = 1

Output: false

Explanation:

The number does not contain digit 1. Thus, the answer is false.

 

Constraints:

  • 0 <= n <= 105​​​​​​​
  • 0 <= x <= 9
Read full problem with examples

Approach Overview

Problem Overview: Given a string, determine whether it represents a valid digit number. A valid digit number contains only characters from 0-9 and no letters, symbols, or whitespace.

Approach 1: Character-by-Character Validation (O(n) time, O(1) space)

Scan the string once and verify each character lies in the ASCII range for digits. During iteration, check whether '0' <= c <= '9'. The moment a non-digit appears, return false. This approach works well because digit validation is a constant-time comparison per character and requires no extra memory. It is the most common solution in interviews since it demonstrates clear understanding of string processing and basic character handling.

Approach 2: Built-in Digit Check (O(n) time, O(1) space)

Most languages expose helpers such as isdigit() or similar utilities. Iterate through the string and call the helper for each character. Internally these functions perform the same digit range check but provide cleaner syntax and fewer manual comparisons. This approach is slightly more readable while still maintaining linear runtime. It is useful when writing production code where readability matters more than demonstrating low-level logic.

Approach 3: Regular Expression Matching (O(n) time, O(1) space)

A concise alternative uses a regex pattern like ^[0-9]+$. The engine verifies that the entire string consists only of digits. Regex solutions are compact and expressive, especially when validation rules grow more complex. However, they introduce regex engine overhead and are usually less preferred in interviews unless the question explicitly involves regular expressions or string parsing.

Recommended for interviews: The character-by-character validation approach is typically expected. It shows you understand ASCII comparisons and can implement efficient single-pass validation. Mentioning the regex alternative demonstrates awareness of practical shortcuts, but the manual iteration solution signals stronger problem-solving fundamentals.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Character-by-Character ValidationO(n)O(1)Best general solution for interviews and low-level validation
Built-in Digit Check (isdigit)O(n)O(1)Cleaner production code when language helpers are available
Regular Expression MatchO(n)O(1)Concise validation when regex is already used in the codebase