{x}
blog image

Equal Rational Numbers

Given two strings s and t, each of which represents a non-negative rational number, return true if and only if they represent the same number. The strings may use parentheses to denote the repeating part of the rational number.

A rational number can be represented using up to three parts: <IntegerPart>, <NonRepeatingPart>, and a <RepeatingPart>. The number will be represented in one of the following three ways:

  • <IntegerPart>
    • For example, 12, 0, and 123.
  • <IntegerPart><.><NonRepeatingPart>
    • For example, 0.5, 1., 2.12, and 123.0001.
  • <IntegerPart><.><NonRepeatingPart><(><RepeatingPart><)>
    • For example, 0.1(6), 1.(9), 123.00(1212).

The repeating portion of a decimal expansion is conventionally denoted within a pair of round brackets. For example:

  • 1/6 = 0.16666666... = 0.1(6) = 0.1666(6) = 0.166(66).

 

Example 1:

Input: s = "0.(52)", t = "0.5(25)"
Output: true
Explanation: Because "0.(52)" represents 0.52525252..., and "0.5(25)" represents 0.52525252525..... , the strings represent the same number.

Example 2:

Input: s = "0.1666(6)", t = "0.166(66)"
Output: true

Example 3:

Input: s = "0.9(9)", t = "1."
Output: true
Explanation: "0.9(9)" represents 0.999999999... repeated forever, which equals 1.  [See this link for an explanation.]
"1." represents the number 1, which is formed correctly: (IntegerPart) = "1" and (NonRepeatingPart) = "".

 

Constraints:

  • Each part consists only of digits.
  • The <IntegerPart> does not have leading zeros (except for the zero itself).
  • 1 <= <IntegerPart>.length <= 4
  • 0 <= <NonRepeatingPart>.length <= 4
  • 1 <= <RepeatingPart>.length <= 4

Solution Explanation for LeetCode 972: Equal Rational Numbers

This problem requires comparing two rational numbers represented as strings, potentially with repeating decimal parts. The key is to convert these string representations into fractions (numerator/denominator) for accurate comparison.

Algorithm:

  1. String Parsing: The input strings need to be parsed to extract the integer, non-repeating decimal, and repeating decimal parts. Regular expressions or manual string manipulation can achieve this.

  2. Fraction Conversion: Each part of the string is converted into a fraction.

    • Integer part: Directly represents the numerator; the denominator is 1.
    • Non-repeating part: The numerator is the decimal part; the denominator is 10 raised to the power of the length of the non-repeating part.
    • Repeating part: This is the trickiest part. The numerator is the repeating part minus its initial value; the denominator is a number formed by a sequence of 9s followed by 0s (the number of 9s equals the length of the repeating part, and the number of 0s equals the length of the non-repeating part).
  3. Fraction Addition: Add the three fractions (integer, non-repeating, and repeating) to get a single fraction representing the entire rational number. This involves finding a common denominator and adding the numerators.

  4. Fraction Simplification (Optional): Simplify the resulting fraction by finding the greatest common divisor (GCD) of the numerator and denominator. While not strictly necessary for comparison, it can make the comparison faster.

  5. Comparison: Finally, compare the two fractions. Two fractions a/b and c/d are equal if and only if ad = bc.

Code (Python):

import math
 
def equalRationalNumbers(s, t):
    def toFraction(s):
        if '(' not in s:
            if '.' not in s:
                return int(s), 1
            integer_part, decimal_part = s.split('.')
            return int(integer_part) * (10**len(decimal_part)) + int(decimal_part), 10**len(decimal_part)
        else:
            integer_part, decimal_part, repeating_part = s.split('.')
            integer_part = int(integer_part)
            non_repeating = decimal_part
            repeating = repeating_part[:-1]
            non_repeating_len = len(non_repeating)
            repeating_len = len(repeating)
            num = integer_part*(10**(non_repeating_len+repeating_len)) + int(non_repeating)*(10**repeating_len) + int(repeating) - int(non_repeating)
            den = (10**(non_repeating_len)) * (10**repeating_len -1)
            return num, den
 
    num1, den1 = toFraction(s)
    num2, den2 = toFraction(t)
    return num1 * den2 == num2 * den1
 

Time Complexity Analysis:

  • The toFraction function has a time complexity of O(n), where n is the length of the input string, due to string splitting and integer conversions.
  • The rest of the equalRationalNumbers function performs constant-time operations.
  • Therefore, the overall time complexity of the solution is O(n), where n is the maximum length of the two input strings.

Space Complexity Analysis:

The space complexity is O(1) because the algorithm uses a fixed amount of extra space regardless of the input string length. The space used for storing intermediate fractions is constant.

Note: Error handling (for invalid input formats) could be added to make the code more robust. Also, consider using Python's fractions module for more efficient fraction arithmetic if performance is critical for very large numbers.