Equal Rational Numbers - Problem
Can Two Different-Looking Numbers Actually Be Equal?

You're given two strings s and t that represent rational numbers (numbers that can be expressed as fractions). Your task is to determine if these two strings represent the same mathematical value, even though they might look completely different!

Here's the twist: these numbers can have repeating decimals represented with parentheses. For example:
"0.1(6)" means 0.1666666... (the 6 repeats forever)
"1.(9)" means 1.9999999... which actually equals 2!
"123.00(1212)" means 123.001212121212...

The strings follow these formats:
1. Integer only: "12", "0", "123"
2. Decimal (no repeat): "0.5", "1.", "2.12"
3. Repeating decimal: "0.1(6)", "1.(9)", "123.00(1212)"

Goal: Return true if both strings represent the same rational number, false otherwise.

Input & Output

example_1.py — Basic Repeating Decimal
$ Input: s = "0.1(6)", t = "0.16666666666"
Output: true
💡 Note: 0.1(6) means 0.1666666... (6 repeats forever), which equals the finite decimal representation up to precision limits
example_2.py — Famous Mathematical Identity
$ Input: s = "1.(9)", t = "2.0"
Output: true
💡 Note: 1.(9) means 1.999999... which mathematically equals exactly 2.0. This is a famous identity: 0.999... = 1
example_3.py — Different Looking Same Numbers
$ Input: s = "0.(52)", t = "0.525252"
Output: false
💡 Note: 0.(52) = 0.525252525252... (infinite), while 0.525252 is finite. They are different numbers

Constraints

  • Each given string will have length in range [1, 50]
  • The strings only contain digits, '.', '(', and ')'
  • The integer part will not start with '0' unless it is "0"
  • For the repeating part, there won't be trailing zeros
  • The given strings represent non-negative rational numbers

Visualization

Tap to expand
The Mathematical Translation ProcessString Input"0.1(6)"Parse Parts0 + 0.1 + (6)Convert15/90 → 1/6Formula for 0.abc(def):Fraction = (abcdef - abc) / (999...000...)where 999... has length of repeating partCompare1/6 vs 1/6EQUAL!Key insight: Mathematical equality is preserved through exact fraction conversion💡 This method handles ALL edge cases including 0.999... = 1!
Understanding the Visualization
1
Identify the Pattern
Separate the integer part, non-repeating decimal, and repeating part
2
Apply the Formula
Use mathematical formulas to convert repeating decimals to exact fractions
3
Simplify
Reduce both fractions to lowest terms using Greatest Common Divisor
4
Compare
Cross-multiply to check if a/b = c/d without floating point errors
Key Takeaway
🎯 Key Insight: Every rational number has a unique fraction representation in lowest terms - that's the mathematical fingerprint we use for perfect comparison!
Asked in
Google 8 Meta 5 Microsoft 3 Apple 2
32.4K Views
Medium Frequency
~35 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen