n to a string to process each digit individually.solve that takes the current position, a tight flag, a bitmask representing used digits, and a flag for leading zeros.solve for the next position.solve for the first position with the appropriate initial conditions and subtract 1 to exclude the number n itself if it's not special.