C++ "normalize" equation string for easier parsing


  • 0
        string solveEquation(string equation) {
            int i = equation.find('=');
            auto left = getCoeff(equation.substr(0, i));
            auto right = getCoeff(equation.substr(i+1));
            
            if (left[1] != right[1]) 
                return "x=" + to_string((right[0]-left[0])/(left[1]-right[1]));
            else if (left[0] != right[0]) 
                return "No solution"; 
            else 
                return "Infinite solutions";        
        }
        
        // Get combined coefficients of 'x' and constants
        vector<int> getCoeff(string s) {
            s = normalize(s);
            vector<int> res(2);
            for (int i = 0, pre = 0; i < s.size(); ++i) {
                if (isdigit(s[i])) continue;
                bool isx = (s[i] == 'x');
                res[isx] += stoi(s.substr(pre,i-pre));
                pre = (i += isx);
            }
            return res;
        }    
        
        // normalize equation by adding missing '1' coefficient for 'x'
        string normalize(string s) {        
            string res;
            for (char c : s) {
                if (c == 'x' and (res.empty() or !isdigit(res.back()))) res += '1';
                res += c;
            }
            return '0' + res + '+';      
        }
    

Log in to reply
 

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