Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Equal Rational Numbers

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "972" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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: , , and a . The number will be represented in one of the following three ways: For example, 12, 0, and 123. <.> For example, 0.5, 1., 2.12, and 123.0001. <.><(><)> 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 does not have leading zeros (except for the zero itself). 1 <= .length <= 4 0 <= .length <= 4 1 <= .length <= 4

Explanation

Here's a breakdown of the solution approach and the code:

  • Convert to Fraction: The core idea is to convert both strings into Fraction objects. This allows for accurate comparisons of rational numbers, avoiding floating-point precision issues.
  • Handle Repeating Decimals: The conversion process involves correctly interpreting the repeating part of the decimal. A formula is used to convert the repeating part into a fraction and add it to the non-repeating part.
  • Simplify and Compare: The Fraction class automatically simplifies the fractions. We then directly compare the two Fraction objects for equality.

  • Runtime Complexity: O(1) - The parsing and conversion are bounded by the maximum lengths of the input string parts (which are constants as specified by the constraints). The Fraction constructor and comparison also take constant time as the numerator and denominator are also bounded by the constraint lengths.

  • Storage Complexity: O(1) - We are using a fixed number of variables regardless of the size of the input.

Code

    from fractions import Fraction

def to_fraction(s):
    if '(' not in s:
        if '.' not in s:
            return Fraction(int(s), 1)
        else:
            if s.endswith('.'):
                s = s[:-1]
            integer_part, decimal_part = s.split('.')
            if not decimal_part:
                 return Fraction(int(integer_part), 1)

            numerator = int(integer_part + decimal_part)
            denominator = 10 ** len(decimal_part)
            return Fraction(numerator, denominator)
    else:
        integer_decimal, repeating = s.split('(')
        repeating = repeating[:-1]  # Remove ')'

        if '.' not in integer_decimal:
            integer_part = integer_decimal
            non_repeating_part = ""
        else:
            integer_part, non_repeating_part = integer_decimal.split('.')

        if not non_repeating_part:
            non_repeating_value = 0
            non_repeating_length = 0
        else:
            non_repeating_value = int(integer_part + non_repeating_part) if integer_part else int(non_repeating_part)
            non_repeating_length = len(non_repeating_part)

        repeating_value = int(repeating)
        repeating_length = len(repeating)

        numerator = (non_repeating_value * (10 ** repeating_length) + repeating_value) - (int(integer_part) * (10 ** (non_repeating_length + repeating_length)) if integer_part else 0) - (non_repeating_value if non_repeating_length > 0 else 0)

        denominator = (10 ** (non_repeating_length + repeating_length)) - (10 ** non_repeating_length)

        return Fraction(numerator, denominator)

def isRationalEqual(s, t):
    return to_fraction(s) == to_fraction(t)

More from this blog

C

Chatmagic blog

2894 posts