#592

Fraction Addition and Subtraction

specialist · 675 · lc medium +30 · verified · 66.4% accepted · 891 likes · top 72%

play →

Description

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"

Code

1
2
3