Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Ambiguous Coordinates

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "816" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

We had some 2-dimensional coordinates, like "(1, 3)" or "(2, 0.5)". Then, we removed all commas, decimal points, and spaces and ended up with the string s. For example, "(1, 3)" becomes s = "(13)" and "(2, 0.5)" becomes s = "(205)". Return a list of strings representing all possibilities for what our original coordinates could have been. Our original representation never had extraneous zeroes, so we never started with numbers like "00", "0.0", "0.00", "1.0", "001", "00.01", or any other number that can be represented with fewer digits. Also, a decimal point within a number never occurs without at least one digit occurring before it, so we never started with numbers like ".1". The final answer list can be returned in any order. All coordinates in the final answer have exactly one space between them (occurring after the comma.) Example 1: Input: s = "(123)" Output: ["(1, 2.3)","(1, 23)","(1.2, 3)","(12, 3)"] Example 2: Input: s = "(0123)" Output: ["(0, 1.23)","(0, 12.3)","(0, 123)","(0.1, 2.3)","(0.1, 23)","(0.12, 3)"] Explanation: 0.0, 00, 0001 or 00.01 are not allowed. Example 3: Input: s = "(00011)" Output: ["(0, 0.011)","(0.001, 1)"] Constraints: 4 <= s.length <= 12 s[0] == '(' and s[s.length - 1] == ')'. The rest of s are digits.

Explanation

  • Generate all possible splits: Iterate through the string (excluding parentheses) to consider all possible splits into two substrings, representing the x and y coordinates.
    • Generate valid numbers: For each substring, generate all valid numbers that can be formed by inserting a decimal point at different positions. A helper function will handle this while avoiding leading zeros and trailing decimal points without digits after.
    • Combine and filter: Combine the generated x and y coordinates to form coordinate pairs. Add each pair to the result list.
  • Runtime Complexity: O(n^3), where n is the length of the input string s. Storage Complexity: O(n^3) due to storing all possible coordinate strings.

Code

    def restore_ip_addresses(s):
    def isValid(s):
        if len(s) > 1 and s[0] == '0':
            return False
        if '.' in s:
            parts = s.split('.')
            if len(parts) > 1 and parts[1] == '':
                return False
        return True

    def generate_numbers(s):
        res = []
        if len(s) == 0:
            return res

        # No decimal point
        if isValid(s):
            res.append(s)

        # With decimal point
        for i in range(1, len(s)):
            left = s[:i]
            right = s[i:]
            if len(right) > 0 and right[-1] == '.':
                continue
            if isValid(left + '.' + right):
                res.append(left + '.' + right)

        return res

    s = s[1:-1]
    result = []
    for i in range(1, len(s)):
        x = s[:i]
        y = s[i:]

        x_nums = generate_numbers(x)
        y_nums = generate_numbers(y)

        for x_num in x_nums:
            if len(x_num) > 1 and x_num[0] == '0' and '.' not in x_num:
                continue
            if '.' in x_num and x_num.index('.') == 0:
                continue

            for y_num in y_nums:
                if len(y_num) > 1 and y_num[0] == '0' and '.' not in y_num:
                    continue
                if '.' in y_num and y_num.index('.') == 0:
                    continue

                result.append(f"({x_num}, {y_num})")

    return result

More from this blog

C

Chatmagic blog

2894 posts