< Summary

Information
Class: LeetCode.Algorithms.FindTheClosestPalindrome.FindTheClosestPalindromeIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheClosestPalindrome\FindTheClosestPalindromeIterative.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 83
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
NearestPalindromic(...)100%1212100%
BuildPalindrome(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheClosestPalindrome\FindTheClosestPalindromeIterative.cs

#LineLine coverage
 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
 12using System.Numerics;
 13using System.Text;
 14
 15namespace LeetCode.Algorithms.FindTheClosestPalindrome;
 16
 17/// <inheritdoc />
 18public 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)
 1327    {
 1328        var num = BigInteger.Parse(n);
 29
 1330        var candidates = new List<BigInteger>
 1331        {
 1332            BigInteger.Pow(10, n.Length - 1) - 1, BigInteger.Pow(10, n.Length) + 1
 1333        };
 34
 1335        var prefix = BigInteger.Parse(n[..((n.Length + 1) / 2)]);
 36
 10437        for (var i = prefix - 1; i <= prefix + 1; i++)
 3938        {
 3939            var candidate = BuildPalindrome(i, n.Length % 2 == 0);
 40
 3941            candidates.Add(BigInteger.Parse(candidate));
 3942        }
 43
 1344        var minDiff = BigInteger.Abs(candidates[0] - num);
 1345        var result = candidates[0];
 46
 16947        foreach (var candidate in candidates)
 6548        {
 6549            if (candidate == num)
 650            {
 651                continue;
 52            }
 53
 5954            var diff = BigInteger.Abs(candidate - num);
 55
 5956            if (diff >= minDiff && (diff != minDiff || candidate >= result))
 4257            {
 4258                continue;
 59            }
 60
 1761            minDiff = diff;
 1762            result = candidate;
 1763        }
 64
 1365        return result.ToString();
 1366    }
 67
 68    private static string BuildPalindrome(BigInteger prefix, bool isEven)
 3969    {
 3970        var stringBuilder = new StringBuilder();
 71
 3972        var prefixString = prefix.ToString();
 73
 3974        stringBuilder.Append(prefixString);
 75
 39776        foreach (var c in prefixString[..^(isEven ? 0 : 1)].Reverse())
 14077        {
 14078            stringBuilder.Append(c);
 14079        }
 80
 3981        return stringBuilder.ToString();
 3982    }
 83}