| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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.CountSymmetricIntegers; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class CountSymmetricIntegersIterative : ICountSymmetricIntegers |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(N log n) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="low"></param> |
| | | 22 | | /// <param name="high"></param> |
| | | 23 | | /// <returns></returns> |
| | | 24 | | public int CountSymmetricIntegers(int low, int high) |
| | 2 | 25 | | { |
| | 2 | 26 | | var count = 0; |
| | | 27 | | |
| | 266 | 28 | | for (var i = low; i <= high; i++) |
| | 131 | 29 | | { |
| | 131 | 30 | | var numString = i.ToString(); |
| | 131 | 31 | | var length = numString.Length; |
| | | 32 | | |
| | 131 | 33 | | if (length % 2 != 0) |
| | 10 | 34 | | { |
| | 10 | 35 | | continue; |
| | | 36 | | } |
| | | 37 | | |
| | 121 | 38 | | var n = length / 2; |
| | 242 | 39 | | int sumLeft = 0, sumRight = 0; |
| | | 40 | | |
| | 546 | 41 | | for (var j = 0; j < n; j++) |
| | 152 | 42 | | { |
| | 152 | 43 | | sumLeft += numString[j] - '0'; |
| | 152 | 44 | | sumRight += numString[j + n] - '0'; |
| | 152 | 45 | | } |
| | | 46 | | |
| | 121 | 47 | | if (sumLeft == sumRight) |
| | 13 | 48 | | { |
| | 13 | 49 | | count++; |
| | 13 | 50 | | } |
| | 121 | 51 | | } |
| | | 52 | | |
| | 2 | 53 | | return count; |
| | 2 | 54 | | } |
| | | 55 | | } |