↓Skip to main content
  1. LeetCode/

LeetCode 3908: Valid Digit Number

·1 min· ·
LeetCode Easy Math
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-04-25

  • Submissions: Accepted (first try)

Approach
#

Both conditions are string operations:

  • Contains x at least once: str(x) in s.
  • Does not start with x: not s.startswith(str(x)).

My Solution
#

class Solution:
    def validDigit(self, n: int, x: int) -> bool:
        s = str(n)
        return str(x) in s and not s.startswith(str(x))

Complexity
#

Time O(d), space O(d).

Mistakes to Avoid
#

  • x is an integer; convert it to a string before comparing with strings.

Interview Explanation
#

I convert n to a string and check that it contains x and that its first character is not x. Time O(d).

Related

LeetCode 976: Largest Perimeter Triangle
·2 mins
LeetCode Easy Math Greedy Sorting Geometry Array
LeetCode 976 note: after sorting, check consecutive triples from the largest down; the first with a + b > c gives the largest perimeter
LeetCode 2269: Find the K-Beauty of a Number
·2 mins
LeetCode Easy Sliding-Window Math String
LeetCode 2269 note: convert the number to a string and check every length-k substring; 0 cannot be a divisor
LeetCode 1281: Subtract the Product and Sum of Digits of an Integer
·1 min
LeetCode Easy Math
LeetCode 1281 note: walk the digits once, accumulating product and sum, and return product minus sum
LeetCode 1512: Number of Good Pairs
·1 min
LeetCode Easy Math Hash-Table Counting Array
LeetCode 1512 note: count while scanning; each number pairs with every earlier equal number, so add its current count to the answer
LeetCode 2469: Convert the Temperature
·1 min
LeetCode Easy Math
LeetCode 2469 note: apply the formulas directly, Kelvin = C + 273.15 and Fahrenheit = C × 1.8 + 32
LeetCode 3697: Compute Decimal Representation
·1 min
LeetCode Easy Math Array
LeetCode 3697 note: each non-zero digit times its place value is one component; output them from the highest place down