LeetCode in Kotlin

972. Equal Rational Numbers

Hard

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:

The repeating portion of a decimal expansion is conventionally denoted within a pair of round brackets. For example:

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:

Solution

import kotlin.math.abs
import kotlin.math.pow

class Solution {
    fun isRationalEqual(s: String, t: String): Boolean {
        return abs(valueOf(s) - valueOf(t)) < 1e-9
    }
    private val ratios = doubleArrayOf(1.0, 1.0 / 9, 1.0 / 99, 1.0 / 999, 1.0 / 9999)
    private fun valueOf(s: String): Double {
        if (!s.contains("(")) return java.lang.Double.valueOf(s)
        val integerNonRepeating = java.lang.Double.valueOf(s.substring(0, s.indexOf('(')))
        val nonRepeatingLength = s.indexOf('(') - s.indexOf('.') - 1
        val repeating = s.substring(s.indexOf('(') + 1, s.indexOf(')')).toInt()
        val repeatingLength = s.indexOf(')') - s.indexOf('(') - 1
        return integerNonRepeating +
            repeating * 0.1.pow(nonRepeatingLength.toDouble()) * ratios[repeatingLength]
    }
}