Easiest Java solution, self-explanatory


  • 2
    S
    public class Solution {
        public String solveEquation(String equation) {
            if(equation == null || equation.length() == 0) {
    	    return equation;
    	}
    
    	String[] parts = equation.split("=");
    	String left = parts[0];
    	String right = parts[1];
    
    	int[] lco = process(left);
    	int[] rco = process(right);
    
    	int x = lco[0] - rco[0];
    	int co = rco[1] - lco[1];
    
    	if(x == 0) {
    	    if(co == 0) {
            	return "Infinite solutions";
           	    } else {
    		return "No solution";
    	    }
    	} else if(co == 0) {
    	    return "x=0";
    	} else {
    	    return "x=" + "" + String.valueOf((int) (co/x));
    	}
        }
    
        public int[] process(String left) {
            char[] array = left.toCharArray();
    	int length = array.length;
    	int prev = 1;
    	int x = 0;
    	int co = 0;
    	int cur = 0;
    	
    	for(int i = 0; i < length; ++i) {
                if(array[i] != 'x') {
    		if(array[i] == '-') {
    		    prev = -1;
    		} else if(array[i] == '+') {
    		    prev = 1;
    		} else {
    		    cur = 0;
    	            while(i < length && array[i] != '+' && array[i] != '-' && array[i] != 'x') {
    		        cur = cur * 10 + ((int)(array[i] - '0'));
    			i++;
    		    }
    		    if(i < length && array[i] == 'x') {
    		        x = x + prev * ((int) (cur));
    			i++;
    		    } else {
    			co = co + prev * ((int) (cur));
    		    }
    		    --i;
    		    }
    	    } else {
    	        x += prev;
    	    }
            }
    	return new int[]{x, co};
        }
    }
    

  • 0
    F

    Great solution that I can get rid of regex expression. upvoted!

    while(i < length && array[i] != '+' && array[i] != '-' && array[i] != 'x') 
    

    this line can be changed into

    while(i < length && Character.isDigit(array[i])) 
    

    which can be shorter and more elegant.


  • 0
    S

    Thank you for your input, yeah I agree that is more elegant.


  • 4

    I don't really like a regex based algorithm problem and I also have a no regex solution, except the usage of split("=") and split("\+") :P

    class Solution {
        public String solveEquation(String equation) {
            if (equation == null || equation.length() < 3) {
                return "No solution";
            }
            String[] equations = equation.split("=");
            String[] l = equations[0].replaceAll("-", "+-").split("\\+");
            String[] r = equations[1].replaceAll("-", "+-").split("\\+");
            int[] sum = new int[2];
            for (String s : l) {
                if (s.length() < 1) {
                    continue;
                } else if (s.equals("x")) {
                    ++sum[0];
                } else if (s.equals("-x")) {
                    --sum[0];
                } else if (s.charAt(s.length()-1) == 'x') {
                    sum[0] += Integer.parseInt(s.substring(0, s.length() - 1));
                } else {
                    sum[1] += Integer.parseInt(s);
                }
            }
            for (String s : r) {
                if (s.length() < 1) {
                    continue;
                } else if (s.equals("x")) {
                    --sum[0];
                } else if (s.equals("-x")) {
                    ++sum[0];
                } else if (s.charAt(s.length()-1) == 'x') {
                    sum[0] -= Integer.parseInt(s.substring(0, s.length() - 1));
                } else {
                    sum[1] -= Integer.parseInt(s);
                }
            }
            if (sum[0] == 0) {
                return sum[1] == 0 ? "Infinite solutions" : "No solution";
            }
            return "x=" + String.valueOf(-sum[1] / sum[0]);
        }
    }
    

  • 0
    J

    @haruhiku I agree, applying regex always gave me a sense of cheating.
    My C++ solution without regex but some basic string operations:

    pair<int,int> analyzeHalve(string &s){
            if(s[0] == '+' || s[0] == '-') s.insert(s.begin(),'0');
            int sign = 1, a = 0, b = 0;
            for(int i = 0; i < s.size(); ++i){
                int pos = s.find_first_of("+-", i);
                if(pos == string :: npos) pos = (int)s.size();
                string tmp = s.substr(i, pos -i);
                if(tmp.back() == 'x'){
                    tmp.pop_back();
                    a += sign * (tmp.size() == 0 ? 1 : stoi(tmp) );
                }
                else
                    b += sign * ( stoi(tmp) );
                sign = s[pos] == '+' ? 1 : -1;
                
                i = pos;
            }
            return {a, b};
        }
        
        
        string solveEquation(string equation) {
            string lHalve, rHalve;
            stringstream ss(equation);
            getline(ss, lHalve, '=');
            getline(ss, rHalve, '=');
            auto p1 = analyzeHalve(lHalve);
            auto p2 = analyzeHalve(rHalve);
            if(p1.first == p2.first){
                if(p1.second == p2.second) return "Infinite solutions";
                else return  "No solution";
            }
            else {
                int ans = (p2.second - p1.second) / (p1.first - p2.first);  
                return "x=" + to_string(ans);
            }
        }
    

  • 0
    O

    @haruhiku clear solution,Very Good!!!


Log in to reply
 

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