Back to blog
Dec 21, 2025
4 min read

Valid Digit Number

Check if a string consists only of digits and does not have leading zeros.

Difficulty: Easy | Acceptance: 70.00% | Paid: No Topics: N/A

Given a string s, return true if s is a valid digit number, otherwise return false.

A valid digit number is defined as a string that:

  1. Consists only of digits (‘0’-‘9’).
  2. Does not have leading zeros unless the number is exactly “0”.

Examples

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

1 <= s.length <= 10^5
s consists of printable ASCII characters.

Approach 1: Iterative Scan

Intuition We can manually iterate through the string to verify the conditions. First, handle the edge case where the string is “0”. Then, ensure the first character is not ‘0’. Finally, iterate through the rest of the string to ensure all characters are digits.

Steps

  • Check if the string is empty. If so, return false.
  • If the string is “0”, return true.
  • If the first character is ‘0’, return false (leading zero).
  • Iterate through the string from the first character to the end.
  • If any character is not a digit, return false.
  • If the loop completes, return true.
python
class Solution:
    def isDigitNumber(self, s: str) -&gt; bool:
        if not s:
            return False
        if s == "0":
            return True
        if s[0] == '0':
            return False
        return s.isdigit()

Complexity

  • Time: O(n), where n is the length of the string. We iterate through the string once.
  • Space: O(1), we only use a constant amount of extra space.
  • Notes: This is the most efficient approach as it stops as soon as an invalid character is found.

Approach 2: Regular Expression

Intuition We can use a regular expression to match the pattern of a valid digit number. The pattern should match either a single “0” or a non-zero digit followed by any number of digits.

Steps

  • Define the regular expression ^[1-9]\\d*$|^0$.
  • ^ asserts the position at the start of the string.
  • [1-9] matches a non-zero digit.
  • \\d* matches zero or more digits.
  • $ asserts the position at the end of the string.
  • | acts as an OR operator.
  • ^0$ matches the string “0”.
  • Test the string against the regular expression.
python
import re

class Solution:
    def isDigitNumber(self, s: str) -&gt; bool:
        pattern = r"^[1-9]\d*$|^0$"
        return bool(re.match(pattern, s))

Complexity

  • Time: O(n), where n is the length of the string. The regex engine needs to scan the string.
  • Space: O(1), constant space for the regex pattern.
  • Notes: While concise, regex can sometimes be slower than a manual loop due to the overhead of the regex engine.

Approach 3: Built-in String Methods

Intuition Many languages provide built-in methods to check if a string is numeric or to check for prefixes. We can combine these to solve the problem concisely.

Steps

  • Check if the string is “0”. If so, return true.
  • Check if the string starts with “0”. If so, return false.
  • Use the language’s built-in method to check if the string consists only of digits.
python
class Solution:
    def isDigitNumber(self, s: str) -&gt; bool:
        if s == "0":
            return True
        if s.startswith("0"):
            return False
        return s.isdigit()

Complexity

  • Time: O(n), where n is the length of the string. Built-in methods typically iterate through the string.
  • Space: O(1), constant space.
  • Notes: This approach is very readable and leverages standard library functions.