Python via helper function


  • 5

    Here I used helper to convert left and right equations into the coef and const which represent the coefficient of x and remaining constant.

    class Solution(object):
        def solveEquation(self, equation):
            """
            :type equation: str
            :rtype: str
            """
            def helper(s):
                sign, n = 1, len(s)
                # i, coef, const stand for current index, and accumulative 'x' coefficient and constant
                i = coef = const = 0
                while i < n:
                    if s[i] == '+':
                        sign = 1
                    elif s[i] == '-':
                        sign = -1
                    elif s[i].isdigit():
                        j = i
                        while j < n and s[j].isdigit():
                            j += 1
                        tmp = int(s[i:j])
                        if j < n and s[j] == 'x':
                            coef += tmp * sign
                            j += 1
                        else:
                            const += tmp * sign
                        i = j-1
                    else:
                        coef += 1 * sign
                    i += 1
                return coef, const
                
            left, right = equation.split('=')
            k1, b1 = helper(left)
            k2, b2 = helper(right)
            # k1x + b1 = k2x + b2
            ans = 'x=' + str((b2 - b1) / (k1 - k2)) if k1 != k2 and b1 != b2 \
                  else "Infinite solutions" if k1 == k2 and b1 == b2 \
                  else "No solution" if b2 != b1 else 'x=0'
            return ans
    

  • 1
    S

    I like it, easy to understand.


  • 0
    W

    Very easy to understand. Thanks to this nice code!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.