#3908
Easy Algorithms Valid digit number
70.1% acceptance
May 13, 2026
13
0
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.
Solution
Rust
Time O(1)
Space O(1)
impl Solution {
pub fn valid_digit(n: i32, x: i32) -> bool {
let s = n.to_string();
let xc = (b'0' + x as u8) as char;
s.chars().next() != Some(xc) && s.contains(xc)
}
}