Equal Rational Numbers - Problem

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: 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).

Input & Output

Example 1 — Same Rational Number
$ Input: s = "0.(52)", t = "0.5(25)"
Output: true
💡 Note: Both represent the same repeating decimal 0.525252..., just with different groupings of the repeating part.
Example 2 — Different Numbers
$ Input: s = "0.1666(6)", t = "0.166(66)"
Output: true
💡 Note: Both represent 1/6 = 0.16666..., showing different valid representations of the same repeating pattern.
Example 3 — No Repeating vs Repeating
$ Input: s = "0.9(9)", t = "1."
Output: true
💡 Note: 0.9999... equals exactly 1.0, demonstrating the mathematical equivalence.

Constraints

  • Each string contains only digits, '.', '(', ')'
  • Both strings are valid representations of rational numbers
  • No leading zeros except for numbers less than 1
  • 1 ≤ s.length, t.length ≤ 20

Visualization

Tap to expand
Equal Rational Numbers: Different Forms, Same ValueInput 1:0.(52)Input 2:0.5(25)Convert to fractionsBoth equal:52/99true
Understanding the Visualization
1
Input Strings
Two different string representations of rational numbers
2
Parse & Convert
Extract components and convert to exact fractions
3
Compare
Check if fractions are mathematically equal
Key Takeaway
🎯 Key Insight: Convert repeating decimals to exact fractions using mathematical formulas for precise comparison
Asked in
Google 15 Facebook 12 Microsoft 8
28.5K Views
Medium Frequency
~35 min Avg. Time
847 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