Given a string expression
representing an expression of fraction addition and subtraction, return the calculation result in string format.
The final result should be an irreducible fraction. If your final result is an integer, change it to the format of a fraction that has a denominator 1
. So in this case, 2
should be converted to 2/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"
Constraints:
'0'
to '9'
, '/'
, '+'
and '-'
. So does the output.±numerator/denominator
. If the first input fraction or the output is positive, then '+'
will be omitted.[1, 10]
. If the denominator is 1
, it means this fraction is actually an integer in a fraction format defined above.[1, 10]
.This problem involves adding and subtracting fractions represented as strings. The challenge lies in efficiently handling the string parsing and fraction arithmetic, and then simplifying the final result to an irreducible fraction.
The core idea behind the solution is to use a least common multiple (LCM) approach to avoid dealing with fractions directly until the very end. The solution leverages the fact that the denominators are relatively small (between 1 and 10). Instead of finding the LCM for every pair of fractions, a large enough common denominator is pre-computed which will always be divisible by all potential denominators (from 1 to 10). The choice of 6 * 7 * 8 * 9 * 10
as the common denominator (y
in the code) guarantees that all possible denominators will divide into it.
Here's a breakdown of the solution:
Preprocessing:
expression
is checked and a +
sign is prepended if it starts with a digit, ensuring consistent handling of signs.y
(678910) is initialized. This simplifies the calculations by avoiding explicit LCM computations at each step.x
is initialized to 0.Iterating through Fractions:
while
loop. Each iteration processes a single fraction:+
or -
) of the fraction is extracted.x
is updated by adding the contribution of the current fraction. The formula x += sign * int(a) * y // int(b)
converts the fraction to a representation with the common denominator y
and adds it to the running sum x
.Simplifying the Result:
x/y
. To simplify this to an irreducible fraction, the greatest common divisor (GCD) of x
and y
is calculated using Euclid's algorithm (gcd
function).x
and y
are divided by the GCD to obtain the final irreducible fraction.Output:
"x/y"
and returned.Time Complexity: O(n), where n is the length of the input string. The loop iterates through the string once to parse and process each fraction. The GCD calculation takes O(log min(x, y)) time which is dominated by the string processing time.
Space Complexity: O(1). The solution uses a constant amount of extra space regardless of the input size. The intermediate variables used to store numerators, denominators, and the results are all of fixed size, not dependent on the input string's length.
The code implementations in Python, Java, Go, and JavaScript provided earlier in the response follow this algorithm precisely. Each demonstrates the same fundamental approach, adapted to the specifics of each language's syntax and standard libraries. The GCD function is a common component used in all the languages.