| | 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 | | using System.Numerics; |
| | 13 | | using System.Text; |
| | 14 | |
|
| | 15 | | namespace LeetCode.Algorithms.FindTheClosestPalindrome; |
| | 16 | |
|
| | 17 | | /// <inheritdoc /> |
| | 18 | | public class FindTheClosestPalindromeIterative : IFindTheClosestPalindrome |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// Time complexity - O(L), where L is the length of the string |
| | 22 | | /// Space complexity - O(L), where L is the length of the string |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="n"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | public string NearestPalindromic(string n) |
| 13 | 27 | | { |
| 13 | 28 | | var num = BigInteger.Parse(n); |
| | 29 | |
|
| 13 | 30 | | var candidates = new List<BigInteger> |
| 13 | 31 | | { |
| 13 | 32 | | BigInteger.Pow(10, n.Length - 1) - 1, BigInteger.Pow(10, n.Length) + 1 |
| 13 | 33 | | }; |
| | 34 | |
|
| 13 | 35 | | var prefix = BigInteger.Parse(n[..((n.Length + 1) / 2)]); |
| | 36 | |
|
| 104 | 37 | | for (var i = prefix - 1; i <= prefix + 1; i++) |
| 39 | 38 | | { |
| 39 | 39 | | var candidate = BuildPalindrome(i, n.Length % 2 == 0); |
| | 40 | |
|
| 39 | 41 | | candidates.Add(BigInteger.Parse(candidate)); |
| 39 | 42 | | } |
| | 43 | |
|
| 13 | 44 | | var minDiff = BigInteger.Abs(candidates[0] - num); |
| 13 | 45 | | var result = candidates[0]; |
| | 46 | |
|
| 169 | 47 | | foreach (var candidate in candidates) |
| 65 | 48 | | { |
| 65 | 49 | | if (candidate == num) |
| 6 | 50 | | { |
| 6 | 51 | | continue; |
| | 52 | | } |
| | 53 | |
|
| 59 | 54 | | var diff = BigInteger.Abs(candidate - num); |
| | 55 | |
|
| 59 | 56 | | if (diff >= minDiff && (diff != minDiff || candidate >= result)) |
| 42 | 57 | | { |
| 42 | 58 | | continue; |
| | 59 | | } |
| | 60 | |
|
| 17 | 61 | | minDiff = diff; |
| 17 | 62 | | result = candidate; |
| 17 | 63 | | } |
| | 64 | |
|
| 13 | 65 | | return result.ToString(); |
| 13 | 66 | | } |
| | 67 | |
|
| | 68 | | private static string BuildPalindrome(BigInteger prefix, bool isEven) |
| 39 | 69 | | { |
| 39 | 70 | | var stringBuilder = new StringBuilder(); |
| | 71 | |
|
| 39 | 72 | | var prefixString = prefix.ToString(); |
| | 73 | |
|
| 39 | 74 | | stringBuilder.Append(prefixString); |
| | 75 | |
|
| 397 | 76 | | foreach (var c in prefixString[..^(isEven ? 0 : 1)].Reverse()) |
| 140 | 77 | | { |
| 140 | 78 | | stringBuilder.Append(c); |
| 140 | 79 | | } |
| | 80 | |
|
| 39 | 81 | | return stringBuilder.ToString(); |
| 39 | 82 | | } |
| | 83 | | } |