Meta Data#
Difficulty: easy First Attempt: 2026-04-25
- Submissions: Accepted (first try)
Approach#
Both conditions are string operations:
- Contains
xat 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#
xis 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).
