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.
In general a rational number can be represented using up to three parts: an integer part, a non-repeating part, and a repeating part. The number will be represented in one of the following three ways:
<IntegerPart>
(e.g. 0, 12, 123)<IntegerPart><.><NonRepeatingPart>
(e.g. 0.5, 1., 2.12, 2.0001)<IntegerPart><.><NonRepeatingPart><(><RepeatingPart><)>
(e.g. 0.1(6), 0.9(9), 0.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)
Both 0.1(6) or 0.1666(6) or 0.166(66) are correct representations of 1 / 6.
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) = "".
Note:
<IntegerPart>
will not begin with 2 or more zeros. (There is no other restriction on the digits of each part.)1 <= <IntegerPart>.length <= 4
0 <= <NonRepeatingPart>.length <= 4
1 <= <RepeatingPart>.length <= 4
struct Solution;
impl Solution {
fn is_rational_equal(s: String, t: String) -> bool {
let a = Self::parse(s);
let b = Self::parse(t);
(a - b).abs() < std::f64::EPSILON
}
fn parse(s: String) -> f64 {
let n = s.len();
if let Some(dot) = s.find('.') {
let integer_part = &s[..dot];
if let Some(lparen) = s.find('(') {
let non_repeating_part = &s[dot + 1..lparen];
let repeating_part = &s[lparen + 1..n - 1];
Self::rational(integer_part, non_repeating_part, repeating_part)
} else {
let non_repeating_part = &s[dot + 1..];
Self::rational(integer_part, non_repeating_part, "")
}
} else {
s.parse::<f64>().unwrap()
}
}
fn rational(integer_part: &str, non_repeating_part: &str, repeating_part: &str) -> f64 {
let a = integer_part.parse::<f64>().unwrap_or(0.0);
let n = non_repeating_part.len();
let b = non_repeating_part.parse::<f64>().unwrap_or(0.0);
let m = repeating_part.len();
let c = repeating_part.parse::<f64>().unwrap_or(0.0);
let mut d = 0.0;
for _ in 0..m {
d *= 10.0;
d += 9.0;
}
let mut e = 1.0;
for _ in 0..n {
e *= 10.0;
}
if n == 0 && m == 0 {
return a;
}
if n == 0 {
return a + c / d;
}
if m == 0 {
return a + b / e;
}
a + (b + c / d) / e
}
}
#[test]
fn test() {
let s = "0.(52)".to_string();
let t = "0.5(25)".to_string();
let res = true;
assert_eq!(Solution::is_rational_equal(s, t), res);
let s = "0.1666(6)".to_string();
let t = "0.166(66)".to_string();
let res = true;
assert_eq!(Solution::is_rational_equal(s, t), res);
let s = "0.9(9)".to_string();
let t = "1.".to_string();
let res = true;
assert_eq!(Solution::is_rational_equal(s, t), res);
}