n to a string S to easily access its digits.dp with a length of K+1, where K is the number of digits in n. Set dp[K] to 1, which represents the empty number.n from left to right, using index i from 0 to K-1.S[i] of n, count the number of digits in the given digits array that are less than S[i] and update dp[i] accordingly.S[i] is in the digits array, add dp[i+1] to dp[i] to include the count of numbers that start with S[i] and are valid for the remaining digits.dp[0] will contain the total count of valid numbers that are less than or equal to n.dp[0] as the result.