Medium

Quiz

#592 Fraction Addition and Subtraction

APPROACH

Given a string expression containing an arithmetic formula of fractions combined with + and - operators, evaluate it and return the result as a fully reduced fraction string. If the result is a whole number, express it as value/1.

Example 1:

Input: expression = "-1/2+1/2"
Output: "0/1"

Example 2:

Input: expression = "-1/2+1/2+1/3"
Output: "1/3"

Example 3:

Input: expression = "1/3-1/2"
Output: "-1/6"
1 of 4
1:00

What is the optimal approach for this problem?