Easy Simple Java + detail explanation
Full detail explanation: https://medium.com/@hch.hkcontact/goldman-sachs-top-50-leetcode-questions-q9-fraction-addition-and-subtraction-eed82a3e3fd3\n\nSteps:\n
Full detail explanation: https://medium.com/@hch.hkcontact/goldman-sachs-top-50-leetcode-questions-q9-fraction-addition-and-subtraction-eed82a3e3fd3\n\nSteps:\nLoop the character (ch) in the given string\nCase 1:if not met "/"\n- add ch to string\n\nCase 2: if not met "/" & meet "/"\n- parse string to numerator\n- make the boolean that met "/" be true\n\nCase 3: if met "/" & meet "+" or "-"\n- parse string to denom\n- make new fraction\n- if has stored fraction, compute them together to get a fraction and store it\n- make the boolean that met "/" be false\n\nCase 4: if met "/"\n- add ch to string\n\nreturn the last computed fraction as string\n\nPerformance:\n- time complexity: O(n), n is no. of characters of the input string\n- space complexity: O(m), m is no. of characters of one of the fractions of input string\n\n```\nclass Fraction{\n    int numerator;\n    int denominator;\n    \n    public Fraction(int numerator, int denominator){\n        this.numerator=numerator;\n        this.denominator=denominator;\n    }\n    \n    public String toString(){\n        return String.valueOf(numerator)+"/"+denominator;\n    }\n}\n\n\n\nclass Solution {\n    \n    private int hcf(int a, int b){\n        if(b==0) return a;\n        return hcf(b, a%b);\n    }\n    \n    private Fraction computeFraction(Fraction f1, Fraction f2){\n        int curNumerator = f1.numerator * f2.denominator + f2.numerator * f1.denominator;\n        int curDenominator = f1.denominator * f2.denominator;\n        if(curNumerator==0) return new Fraction(0,1);        \n        int bigger = Math.max(Math.abs(curNumerator), Math.abs(curDenominator));\n        int smaller = Math.min(Math.abs(curNumerator), Math.abs(curDenominator));\n        int hcf = hcf(bigger, smaller);\n        return new Fraction(curNumerator/hcf, curDenominator/hcf);\n\n    }\n    \n    \n    public String fractionAddition(String expression) {\n\n        char[] ar = expression.toCharArray();\n        int i=0;\n        int n = ar.length;\n        \n        Fraction stored = null;\n        boolean positive = true;\n        int numerator = 0;\n        int denominator = 0;\n        StringBuilder sb = new StringBuilder();\n        \n        boolean metSlash = false;\n        \n        while(i<n){\n            char ch = ar[i];\n            if(!metSlash&&ch==\'/\'){\n                numerator = Integer.parseInt(sb.toString());\n                sb=new StringBuilder();\n                metSlash=true;\n            }else if(!metSlash){                \n                sb.append(ch);\n            }else if(i==n-1||(metSlash&&(ch==\'+\'||ch==\'-\'))){\n                if(i==n-1) sb.append(ch);\n                denominator = Integer.parseInt(sb.toString());\n                Fraction curFac = new Fraction(numerator, denominator);\n                if(stored!=null) curFac=computeFraction(stored, curFac);\n                stored=curFac;\n                sb=new StringBuilder();\n                sb.append(ch);\n                metSlash=false;\n            }else if(metSlash){\n                sb.append(ch);\n            }\n            i++;\n        }\n        \n        return stored.toString();\n    }\n}\n```
['Java']