Given an alphanumeric string, consisting of a single alphabet **X**, which represents an expression of the form:

A operator B = C

where A, B and C denotes integers and the operator can be either of +, -, * or /

The task is to evaluate the missing digit **X **present in any of the integers A, B and C such that the given expression holds to be valid.

**Examples:**

Input:S = “3x + 12 = 46”Output:4Explanation:

If we subtract 12 from 46, we will get 34.

So, on comparing 3x and 34. the value of x = 4

Input:S = “4 – 2 = x”Output:2Explanation:

After solving the equation, the value of x = 2.

**Approach:** Follow the steps below to solve the problem:

- Split the string to extract the two operands, operator and the resultant.
- Check if X is present in the resultant or not. If so, then compute the value of the resultant by applying operations on the first operand and second operand with the operator.
- Otherwise, if X is not present in the resultant. Then check if X is present in the first operand. If so, then apply the operation on the second operand and resultant with the operator.
- Otherwise, if X is not present in the first operand also. Then check if X is present in the second operand. If so, then apply the operation on the first operand and resultant with the operator.

Below is the implementation of the above approach:

## Python3

`# Python3 program to find missing ` `# digit x in expression ` ` ` ` ` `def` `MissingDigit(exp): ` ` ` ` ` `# Split the expression to ` ` ` `# extract operands, operator ` ` ` `# and resultant ` ` ` `exp ` `=` `list` `(exp.split()) ` ` ` ` ` `first_operand ` `=` `exp[` `0` `] ` ` ` `operator ` `=` `exp[` `1` `] ` ` ` `second_operand ` `=` `exp[` `2` `] ` ` ` `resultant ` `=` `exp[` `-` `1` `] ` ` ` ` ` `# If x is present in resultant ` ` ` `if` `'x'` `in` `resultant: ` ` ` `x ` `=` `resultant ` ` ` `first_operand ` `=` `int` `(first_operand) ` ` ` `second_operand ` `=` `int` `(second_operand) ` ` ` ` ` `if` `operator ` `=` `=` `'+'` `: ` ` ` `res ` `=` `first_operand ` `+` `second_operand ` ` ` `elif` `operator ` `=` `=` `'-'` `: ` ` ` `res ` `=` `first_operand ` `-` `second_operand ` ` ` `elif` `operator ` `=` `=` `'*'` `: ` ` ` `res ` `=` `first_operand ` `*` `second_operand ` ` ` `else` `: ` ` ` `res ` `=` `first_operand ` `/` `/` `second_operand ` ` ` ` ` `# If x in present in operands ` ` ` `else` `: ` ` ` ` ` `resultant ` `=` `int` `(resultant) ` ` ` ` ` `# If x in the first operand ` ` ` `if` `'x'` `in` `first_operand: ` ` ` ` ` `x ` `=` `first_operand ` ` ` `second_operand ` `=` `int` `(second_operand) ` ` ` ` ` `if` `operator ` `=` `=` `'+'` `: ` ` ` `res ` `=` `resultant ` `-` `second_operand ` ` ` `elif` `operator ` `=` `=` `'-'` `: ` ` ` `res ` `=` `resultant ` `+` `second_operand ` ` ` `elif` `operator ` `=` `=` `'*'` `: ` ` ` `res ` `=` `resultant ` `/` `/` `second_operand ` ` ` `else` `: ` ` ` `res ` `=` `resultant ` `*` `second_operand ` ` ` ` ` `# If x is in the second operand ` ` ` `else` `: ` ` ` ` ` `x ` `=` `second_operand ` ` ` `first_operand ` `=` `int` `(first_operand) ` ` ` ` ` `if` `operator ` `=` `=` `'+'` `: ` ` ` `res ` `=` `resultant` `-` `first_operand ` ` ` `elif` `operator ` `=` `=` `'-'` `: ` ` ` `res ` `=` `first_operand ` `-` `resultant ` ` ` `elif` `operator ` `=` `=` `'*'` `: ` ` ` `res ` `=` `resultant ` `/` `/` `first_operand ` ` ` `else` `: ` ` ` `res ` `=` `first_operand ` `/` `/` `resultant ` ` ` ` ` `res ` `=` `str` `(res) ` ` ` `k ` `=` `0` ` ` `for` `i ` `in` `x: ` ` ` `if` `i ` `=` `=` `'x'` `: ` ` ` `result ` `=` `res[k] ` ` ` `break` ` ` `else` `: ` ` ` `k ` `=` `k ` `+` `1` ` ` ` ` `return` `result ` ` ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `# input expression ` ` ` `exp ` `=` `"3x + 12 = 46"` ` ` ` ` `print` `(MissingDigit(exp)) ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(L), where is the length of the equation.**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of N-digit numbers having digit XOR as single digit
- Find the remainder when First digit of a number is divided by its Last digit
- Find the missing integer in an array if mean is given
- Find the missing value from the given equation a + b = c
- Find the last two missing digits of the given phone number
- Find temperature of missing days using given sum and average
- Find all missing numbers from a given sorted array
- Count 'd' digit positive integers with 0 as a digit
- Check if frequency of each digit is less than the digit
- Count n digit numbers not having a particular digit
- Perform n steps to convert every digit of a number in the format [count][digit]
- Largest number less than N with digit sum greater than the digit sum of N
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Last digit of a number raised to last digit of N factorial
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Find all possible outcomes of a given expression
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
- Find index of closing bracket for a given opening bracket in an expression

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.