Skip to main content
LeetCode 166, Medium. Topics: Hash Table, Math, String. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:

Problem

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. If multiple answers are possible, return any of them. It is guaranteed that the length of the answer string is less than 10^4 for all the given inputs. Note that if the fraction can be represented as a finite length string, you must return it.

Examples

Constraints

  • -2^31 <= numerator, denominator <= 2^31 - 1
  • denominator != 0

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026