[LeetCode] Valid Digit Number

3908. Valid Digit Number

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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
bool validDigit(int n, int x) {
bool has = false;
while(n >= 10) {
has |= n % 10 == x;
n /= 10;
}
return has and n != x;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/valid-digit-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.