Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

972. Equal Rational Numbers

Hard44.3% Acceptance
MathString
Asked by:
M
Microsoft
ProblemSolutions (8)VideosCompanies (1)Notes

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: <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
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

In #972 Equal Rational Numbers, two strings represent rational numbers that may contain repeating decimals written in parentheses, such as 0.(52). The goal is to determine whether both strings represent the same rational value.

A robust approach is to convert each representation into a normalized rational fraction. Parse the integer part, the non‑repeating decimal part, and the repeating section separately. Using mathematical formulas, transform the repeating decimal into a fraction. For example, a repeating block can be represented as the difference between two numbers divided by powers of 10. After forming the numerator and denominator, reduce the fraction using the greatest common divisor (GCD) and compare the two simplified fractions.

An alternative idea is to expand both decimals to a sufficiently long precision and compare them, but the fraction approach is more precise and avoids floating‑point errors. Parsing and fraction construction run in O(n) time where n is the string length, with small constant space overhead.

Complexity

ApproachTime ComplexitySpace Complexity
Parse string and convert repeating decimal to reduced fractionO(n)O(1)
Expand decimals to fixed precision and compareO(n)O(n)

Video Solution Available

Math with Mr. J

View all video solutions

Solutions (8)

Convert Strings to Fractions

We can represent each decimal number, potentially with repeating decimals, as a fraction. By converting the number represented by the string into a fraction (numerator and denominator) in a consistent way, we can directly compare the fractions. This involves algebraic manipulation to handle the repeating parts.

For example, 0.1(6) can be represented as 1/6. By converting the repeating part into a fraction, you maintain precision without dealing with floating point inaccuracies. Once both numbers are represented as fractions, compare the fractions as ratios of their numerators and denominators to determine if they represent the same number.

Time Complexity: O(n), where n is the length of the maximum string length, since we handle each character a constant number of times.

Space Complexity: O(1), as we're using a constant amount of additional space for our number components and calculations.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <string.h>
3#include <math.h>
4
5long long gcd


    



Explanation

This solution defines a function toFraction that converts a string representation of a rational number into its (numerator, denominator) form by interpreting its integer, non-repeating, and repeating sections separately.

The method uses algebraic equivalents for fractions of repeating decimals to convert them. gcd is used to reduce the resulting fraction. Once both numbers are converted, they are compared directly as fractions.

Simulate Repetition and Direct Comparison

Instead of converting into a fraction, simulate the actual number by expanding it up to a certain number of places where we are confident they will differ if they are indeed different numbers. Focus on accurately reproducing the decimal expansion including the repeating part, and use this expanded version for comparison.

You could use string manipulation to handle the repeat section and use a loop to add the repeating digits until you achieve a sufficient significant length (e.g., 20-30 digits) for a robust comparison.

Time Complexity: O(p), where p is the precision we choose (20 in this case), because we repeatedly generate strings to be compared until this precision is achieved.

Space Complexity: O(p), primarily used to store sequences of the generated decimal until precision is satisfied.

PythonJavaScript
1def expand_decimal(s, precision






Video Solutions

Watch expert explanations and walkthroughs

Rational Numbers Explained | Math with Mr. J

Math with Mr. J
9:54284,750 views

Asked By Companies

1 companies
M
Microsoft

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Integer to RomanMedium
Roman to IntegerEasy
Multiply StringsMedium
Add BinaryEasy
More similar problems

Related Topics

MathString

Problem Stats

Acceptance Rate44.3%
DifficultyHard
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Equal Rational Numbers asked in FAANG interviews?

Yes, problems like #972 Equal Rational Numbers are representative of interview questions at top companies. They test a candidate's understanding of number representation, string parsing, and avoiding floating-point precision issues.

What is the optimal approach for Equal Rational Numbers?

The optimal approach is to parse each number and convert it into a rational fraction. By separating the integer, non-repeating, and repeating parts, you can compute an exact numerator and denominator and reduce them using GCD. If the reduced fractions match, the rational numbers are equal.

What data structure or concept is most useful for Equal Rational Numbers?

The problem mainly relies on mathematical reasoning and string parsing. Using rational number representation with numerator and denominator, along with the GCD algorithm, helps normalize and compare values precisely.

Why can't we directly compare floating-point values in Equal Rational Numbers?

Floating-point numbers introduce rounding errors, especially for repeating decimals. Converting the values to exact fractions avoids precision loss and guarantees accurate comparison of rational numbers.

(
long
long
a
,
long
long
b
)
{
6
return
b
==
0
?
a
:
gcd
(
b
,
a
%
b
)
;
7
}
8
9
void
splitNumber
(
const
char
*
s
,
long
long
*
intPart
,
long
long
*
nonRepeat
,
long
long
*
repeat
,
int
*
nonRepeatLen
,
int
*
repeatLen
)
{
10
*
intPart
=
0
;
*
nonRepeat
=
0
;
*
repeat
=
0
;
11
*
nonRepeatLen
=
0
;
*
repeatLen
=
0
;
12
int
intPartRead
=
1
;
13
14
for
(
int
i
=
0
;
s
[
i
]
;
++
i
)
{
15
if
(
s
[
i
]
>=
'0'
&&
s
[
i
]
<=
'9'
)
{
16
if
(
intPartRead
)
*
intPart
=
*
intPart
*
10
+
(
s
[
i
]
-
'0'
)
;
17
else
if
(
*
repeatLen
==
0
)
*
nonRepeat
=
*
nonRepeat
*
10
+
(
s
[
i
]
-
'0'
)
,
(
*
nonRepeatLen
)
++
;
18
else
*
repeat
=
*
repeat
*
10
+
(
s
[
i
]
-
'0'
)
,
(
*
repeatLen
)
++
;
19
}
else
if
(
s
[
i
]
==
'.'
)
{
20
intPartRead
=
0
;
21
}
else
if
(
s
[
i
]
==
'('
)
{
22
*
repeatLen
=
1
;
// start reading repeat
23
}
24
}
25
}
26
27
void
toFraction
(
const
char
*
s
,
long
long
*
numerator
,
long
long
*
denominator
)
{
28
long
long
intPart
,
nonRepeat
,
repeat
;
29
int
nonRepeatLen
,
repeatLen
;
30
splitNumber
(
s
,
&
intPart
,
&
nonRepeat
,
&
repeat
,
&
nonRepeatLen
,
&
repeatLen
)
;
31
32
if
(
repeatLen
==
0
)
{
// no repeat part
33
*
numerator
=
intPart
*
pow
(
10
,
nonRepeatLen
)
+
nonRepeat
;
34
*
denominator
=
pow
(
10
,
nonRepeatLen
)
;
35
}
else
{
36
long
long
powNR10
=
pow
(
10
,
nonRepeatLen
)
;
37
long
long
powR10
=
pow
(
10
,
repeatLen
)
;
38
*
numerator
=
intPart
*
powNR10
*
(
powR10
-
1
)
+
nonRepeat
*
(
powR10
-
1
)
+
repeat
;
39
*
denominator
=
powNR10
*
(
powR10
-
1
)
;
40
}
41
42
long
long
commonDiv
=
gcd
(
*
numerator
,
*
denominator
)
;
43
*
numerator
/=
commonDiv
;
44
*
denominator
/=
commonDiv
;
45
}
46
47
int
isEqualRational
(
const
char
*
s
,
const
char
*
t
)
{
48
long
long
numS
,
denS
,
numT
,
denT
;
49
toFraction
(
s
,
&
numS
,
&
denS
)
;
50
toFraction
(
t
,
&
numT
,
&
denT
)
;
51
return
numS
==
numT
&&
denS
==
denT
;
52
}
53
54
int
main
(
)
{
55
char
s
[
]
=
"0.(52)"
,
t
[
]
=
"0.5(25)"
;
56
if
(
isEqualRational
(
s
,
t
)
)
printf
(
"true\n"
)
;
57
else
printf
(
"false\n"
)
;
58
return
0
;
59
}
60
=
20
)
:
2
before_repeat
,
repeat
=
s
.
strip
(
')'
)
.
split
(
'('
)
if
'('
in
s
else
(
s
,
''
)
3
expanded
=
before_repeat
4
5
# Add the repeat part enough times
6
while
len
(
expanded
)
<=
precision
:
7
expanded
+=
repeat
8
9
return
expanded
10
11
12
def
remove_leading_trailing_zeros
(
s
)
:
13
s
=
s
.
rstrip
(
'0'
)
.
rstrip
(
'.'
)
14
return
s
.
lstrip
(
'0'
)
or
'0'
15
16
17
def
is_equal_up_to_precision
(
s
,
t
,
precision
=
20
)
:
18
s_expanded
=
expand_decimal
(
s
,
precision
)
19
t_expanded
=
expand_decimal
(
t
,
precision
)
20
return
remove_leading_trailing_zeros
(
s_expanded
)
==
remove_leading_trailing_zeros
(
t_expanded
)
21
22
# Example usage
23
s
=
"0.(52)"
24
t
=
"0.5(25)"
25
print
(
"true"
if
is_equal_up_to_precision
(
s
,
t
)
else
"false"
)
26

Explanation

This solution focuses on simulating the decimal expansion by repeating the portion inside parentheses enough times to ensure that, if the numbers are the same, they will coincide over a sufficient length to determine equality. The expand_decimal function repeats the periodic section until reaching a designated precision and then compares the results much like directly comparing two string values.