Simple Easy Intuition : Step-by-Step Guide in JAVA
Intuition\n Describe your first thoughts on how to solve this problem. \nHere\'s the intuition in simple words:\n\n### Simple Intuition for Adding Fractions:\n\
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nHere\'s the intuition in simple words:\n\n### Simple Intuition for Adding Fractions:\n\n1. **Same Denominator**: \n   - To add fractions, they need to have the same bottom number (denominator). If they don\'t, you can\'t add them directly.\n\n2. **Make Denominators the Same**: \n   - You adjust the fractions so they have the same denominator by multiplying the bottom numbers together. This way, both fractions are "speaking the same language."\n\n3. **Adjust the Numerators**:\n   - Once the denominators match, you adjust the top numbers (numerators) accordingly. This ensures each fraction is still the same value, just expressed differently.\n\n4. **Add the Numerators**:\n   - Now that the denominators are the same, you simply add the numerators together to get the result.\n\n5. **Simplify**:\n   - Finally, you simplify the fraction if possible, to make it as simple as possible.\n\nBy following these steps, you can correctly add fractions even if they start with different denominators.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHere\u2019s the approach in simple words:\n\n1. **Start with a Common Denominator**:\n   - When adding fractions, first make sure all the fractions have the same bottom number (denominator). You do this by multiplying the denominators together.\n\n2. **Adjust the Numerators**:\n   - For each fraction, adjust the top number (numerator) so it matches the new common denominator. This keeps the value of the fraction the same.\n\n3. **Add the Numerators**:\n   - Once all fractions have the same denominator, you add up the numerators (the top numbers) to get a new numerator.\n\n4. **Combine and Simplify**:\n   - The final fraction will have the new numerator you calculated and the common denominator. If possible, simplify this fraction by dividing both the top and bottom by the greatest common divisor (GCD).\n\nThis approach lets you add fractions step by step, even if they have different denominators to begin with.\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public String fractionAddition(String expression) {\n        // If the expression doesn\'t start with a minus, prepend a plus for easier parsing\n        if (expression.charAt(0) != \'-\') {\n            expression = \'+\' + expression;\n        }\n\n        int numeratorSum = 0;\n        int denominatorProduct = 1;\n\n        int i = 0;\n        while (i < expression.length()) {\n            // Parse the sign\n            char sign = expression.charAt(i);\n            i++;\n\n            // Parse the numerator\n            int numerator = 0;\n            while (i < expression.length() && Character.isDigit(expression.charAt(i))) {\n                numerator = numerator * 10 + (expression.charAt(i) - \'0\');\n                i++;\n            }\n            if (sign == \'-\') {\n                numerator = -numerator;\n            }\n\n            // Skip the \'/\' character\n            i++;\n\n            // Parse the denominator\n            int denominator = 0;\n            while (i < expression.length() && Character.isDigit(expression.charAt(i))) {\n                denominator = denominator * 10 + (expression.charAt(i) - \'0\');\n                i++;\n            }\n\n            // Compute the sum of numerators with a common denominator\n            // a/b+c/d=(a*d+c*b)/(b*d);\n            numeratorSum = numeratorSum * denominator + numerator * denominatorProduct;\n            denominatorProduct *= denominator;\n \n            // Simplify the fraction by dividing by the GCD\n            int gcd = gcd(Math.abs(numeratorSum), denominatorProduct);\n            numeratorSum /= gcd;\n            denominatorProduct /= gcd;\n        }\n\n        return numeratorSum + "/" + denominatorProduct;\n    }\n\n    // Helper function to compute the Greatest Common Divisor (GCD)\n    private int gcd(int a, int b) {\n        if (b == 0) return a;\n        return gcd(b, a % b);\n    }\n}\n\n```
['Java']