| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.FractionAdditionAndSubtraction; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class FractionAdditionAndSubtractionSimulation : IFractionAdditionAndSubtraction |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="expression"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public string FractionAddition(string expression) |
| 10 | 24 | | { |
| 10 | 25 | | var numerator = 0; |
| 10 | 26 | | var denominator = 1; |
| 10 | 27 | | var i = 0; |
| | 28 | |
|
| 64 | 29 | | while (i < expression.Length) |
| 54 | 30 | | { |
| 54 | 31 | | var sign = 1; |
| | 32 | |
|
| 54 | 33 | | switch (expression[i]) |
| | 34 | | { |
| | 35 | | case '-': |
| 35 | 36 | | sign = -1; |
| | 37 | |
|
| 35 | 38 | | i++; |
| 35 | 39 | | break; |
| | 40 | | case '+': |
| 14 | 41 | | i++; |
| 14 | 42 | | break; |
| | 43 | | } |
| | 44 | |
|
| 54 | 45 | | var currentNumerator = 0; |
| | 46 | |
|
| 109 | 47 | | while (i < expression.Length && char.IsDigit(expression[i])) |
| 55 | 48 | | { |
| 55 | 49 | | currentNumerator = (currentNumerator * 10) + (int)char.GetNumericValue(expression[i]); |
| | 50 | |
|
| 55 | 51 | | i++; |
| 55 | 52 | | } |
| | 53 | |
|
| 54 | 54 | | currentNumerator *= sign; |
| | 55 | |
|
| 54 | 56 | | i++; |
| | 57 | |
|
| 54 | 58 | | var currentDenominator = 0; |
| | 59 | |
|
| 116 | 60 | | while (i < expression.Length && char.IsDigit(expression[i])) |
| 62 | 61 | | { |
| 62 | 62 | | currentDenominator = (currentDenominator * 10) + (int)char.GetNumericValue(expression[i]); |
| 62 | 63 | | i++; |
| 62 | 64 | | } |
| | 65 | |
|
| 54 | 66 | | numerator = (numerator * currentDenominator) + (currentNumerator * denominator); |
| 54 | 67 | | denominator *= currentDenominator; |
| | 68 | |
|
| 54 | 69 | | var divisor = GetDivisor(numerator, denominator); |
| | 70 | |
|
| 54 | 71 | | numerator /= divisor; |
| 54 | 72 | | denominator /= divisor; |
| 54 | 73 | | } |
| | 74 | |
|
| 10 | 75 | | if (denominator < 0) |
| 1 | 76 | | { |
| 1 | 77 | | numerator = -numerator; |
| 1 | 78 | | denominator = -denominator; |
| 1 | 79 | | } |
| | 80 | |
|
| 10 | 81 | | return $"{numerator}/{denominator}"; |
| 10 | 82 | | } |
| | 83 | |
|
| | 84 | | private static int GetDivisor(int numerator, int denominator) |
| 54 | 85 | | { |
| 245 | 86 | | while (denominator != 0) |
| 191 | 87 | | { |
| 191 | 88 | | var temp = denominator; |
| 191 | 89 | | denominator = numerator % denominator; |
| 191 | 90 | | numerator = temp; |
| 191 | 91 | | } |
| | 92 | |
|
| 54 | 93 | | return numerator; |
| 54 | 94 | | } |
| | 95 | | } |