You are given an integer n and a digit x.
A number is considered valid if:
x, andx.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 <= 1050 <= x <= 9Problem 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.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Character-by-Character Validation | O(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 Match | O(n) | O(1) | Concise validation when regex is already used in the codebase |
Practice Valid Digit Number with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor