Simple Java Code ☠️
Code\njava []\nclass Solution {\n    public String fractionAddition(String expression) {\n        int n = expression.length();\n        int c = 0;\n        for(
# Code\n```java []\nclass Solution {\n    public String fractionAddition(String expression) {\n        int n = expression.length();\n        int c = 0;\n        for(int i=0;i<n;i++){\n            if(expression.charAt(i)==\'/\'){\n                c++;\n            }\n        }\n        int[] num = new int[c];\n        int[] den = new int[c];\n        int[] s = new int[c];\n        int i = 0;\n        if(expression.charAt(0)==\'-\'){\n            s[0]=-1;\n            i=1;\n        }\n        else{\n            s[0]=1;\n        }\n        int j=0,k=0,l=1;\n        while (i < n) {\n            if (expression.charAt(i) == \'-\' || expression.charAt(i) == \'+\') {\n                if (expression.charAt(i) == \'-\') {\n                    s[l] = -1;\n                } else {\n                    s[l] = 1;\n                }\n                l++;\n                i++;\n            }\n            int nu=0;\n            while (i < n && Character.isDigit(expression.charAt(i))) {\n                nu=nu*10+(expression.charAt(i)-\'0\');\n                i++;\n            }\n            num[j++] = nu;\n            i++; \n            int de = 0;\n            while (i < n && Character.isDigit(expression.charAt(i))) {\n                de=de* 10+(expression.charAt(i)-\'0\');\n                i++;\n            }\n            den[k++]=de;\n        }\n        int lcm = den[0];\n        for(i=1;i<c;i++){\n            lcm = lcm*(den[i]/helper(lcm,den[i]));;\n        }\n        for(i=0;i<c;i++){\n            int a = (lcm/den[i])*s[i];\n            num[i]*=a;\n        }\n        int sum=0;\n        for(i=0;i<c;i++){\n            sum+=num[i];\n        }\n        int x = helper(Math.abs(sum),lcm);\n        sum=sum/x;\n        lcm=lcm/x;\n        String str = Integer.toString(sum)+"/"+Integer.toString(lcm);\n        return str;\n    }\n    public static int helper(int a,int b) {\n        if (b==0)\n            return a;\n        return helper(b, a % b);\n    }\n}\n```\n\n\n\n\n### Logic Breakdown\n\n1. **Count Fractions**:\n   - Determine the number of fractions in the input string. This is done by counting the number of \'/\' characters in the string.\n\n2. **Initialize Data Structures**:\n   - Create arrays to store:\n     - **Numerators** of the fractions.\n     - **Denominators** of the fractions.\n     - **Signs** associated with each fraction (whether it\'s positive or negative).\n\n3. **Parse the Input String**:\n   - Traverse the string to extract and parse:\n     - **Signs**: Determine if the fraction is positive or negative.\n     - **Numerators** and **Denominators**: Extract these values based on their position in the string.\n   - Handle transitions between fractions and signs appropriately to fill the arrays.\n\n4. **Compute Least Common Multiple (LCM)**:\n   - Calculate the LCM of all denominators. The LCM is required to find a common denominator so that all fractions can be added or subtracted correctly.\n\n5. **Convert Fractions to Common Denominator**:\n   - Adjust each fraction so that its denominator matches the computed LCM. This involves:\n     - Multiplying the numerator by the factor required to convert its denominator to the LCM.\n\n6. **Sum the Numerators**:\n   - Sum all the numerators after they have been adjusted to the common denominator. The resulting value is the numerator of the final result.\n\n7. **Reduce the Resulting Fraction**:\n   - Simplify the resulting fraction by dividing both the numerator and the denominator by their greatest common divisor (GCD). This ensures the fraction is in its simplest form.\n\n8. **Format the Result**:\n   - Convert the simplified numerator and denominator into a string format representing the final fraction. If the numerator is zero, ensure the denominator is set to 1 to follow the problem\u2019s requirements.\n\n### Detailed Steps\n\n1. **Counting Fractions**:\n   - Use the \'/\' character to count how many fractions are present in the expression.\n\n2. **Array Initialization**:\n   - Initialize arrays for numerators, denominators, and signs based on the number of fractions.\n\n3. **Parsing the Expression**:\n   - Traverse the string and use conditions to detect signs (\'+\' or \'-\') and extract numeric values for numerators and denominators.\n\n4. **LCM Calculation**:\n   - Compute the LCM of all denominators to facilitate addition/subtraction. This involves multiplying and dividing based on GCD calculations.\n\n5. **Adjusting Fractions**:\n   - For each fraction, adjust the numerator according to how much the denominator needs to be scaled up to match the LCM.\n\n6. **Summing Up**:\n   - After adjusting, sum all the numerators to get the final numerator of the result.\n\n7. **Simplification**:\n   - Divide the final numerator and denominator by their GCD to get the fraction in its simplest form.\n\n8. **Formatting**:\n   - Convert the simplified result into a string format for output.\n\n\nLet\'s walk through the logic with an example: \n\n**Expression: `"1/2+1/3-1/4"`**\n\n### Step-by-Step Explanation\n\n1. **Count Fractions**:\n   - There are three fractions in the expression (`"1/2"`, `"1/3"`, `"1/4"`).\n\n2. **Initialize Data Structures**:\n   - Arrays to hold:\n     - **Numerators**: `[1, 1, -1]` (for `"1/2"`, `"1/3"`, and `"1/4"`)\n     - **Denominators**: `[2, 3, 4]`\n     - **Signs**: `[1, 1, -1]` (signs corresponding to each fraction)\n\n3. **Parse the Input String**:\n   - Traverse the string to extract the numerators, denominators, and signs:\n     - `"1/2"`: Numerator = 1, Denominator = 2, Sign = `+`\n     - `"1/3"`: Numerator = 1, Denominator = 3, Sign = `+`\n     - `"1/4"`: Numerator = 1, Denominator = 4, Sign = `-`\n\n4. **Compute Least Common Multiple (LCM)**:\n   - Calculate the LCM of the denominators `[2, 3, 4]`:\n     - LCM of 2 and 3 is 6.\n     - LCM of 6 and 4 is 12.\n     - So, the LCM of `[2, 3, 4]` is `12`.\n\n5. **Convert Fractions to Common Denominator**:\n   - Convert each fraction to have a denominator of 12:\n     - `"1/2"`: To get denominator 12, multiply numerator by `12 / 2 = 6`. So, it becomes `6/12`.\n     - `"1/3"`: To get denominator 12, multiply numerator by `12 / 3 = 4`. So, it becomes `4/12`.\n     - `"1/4"`: To get denominator 12, multiply numerator by `12 / 4 = 3`. So, it becomes `-3/12` (considering the negative sign).\n\n6. **Sum the Numerators**:\n   - Sum the numerators after converting to a common denominator:\n     - `6 + 4 - 3 = 7`.\n   - So, the resulting fraction is `7/12`.\n\n7. **Reduce the Resulting Fraction**:\n   - The greatest common divisor (GCD) of `7` and `12` is `1`, so the fraction is already in its simplest form.\n\n8. **Format the Result**:\n   - The final result is `"7/12"`.\n\n\n\n\n- **Time Complexity**: \\(O(n + c log d)\\), where \\(n\\) is the length of the input string, \\(c\\) is the number of fractions, and \\(d\\) is the magnitude of the denominators.\n- **Space Complexity**: \\(O(c)\\), where \\(c\\) is the number of fractions.\n\n\n\n
['Math', 'String', 'Simulation', 'Java']