< Summary

Information
Class: LeetCode.Algorithms.ShortestPalindrome.ShortestPalindromeTwoPointers
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ShortestPalindrome\ShortestPalindromeTwoPointers.cs
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 61
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ShortestPalindrome(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ShortestPalindrome\ShortestPalindromeTwoPointers.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.Text;
 13
 14namespace LeetCode.Algorithms.ShortestPalindrome;
 15
 16/// <inheritdoc />
 17public class ShortestPalindromeTwoPointers : IShortestPalindrome
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n^2)
 21    ///     Space complexity - O(n)
 22    /// </summary>
 23    /// <param name="s"></param>
 24    /// <returns></returns>
 25    public string ShortestPalindrome(string s)
 1226    {
 1227        if (s.Length <= 1)
 428        {
 429            return s;
 30        }
 31
 832        var left = 0;
 33
 13634        for (var right = s.Length - 1; right >= 0; right--)
 6035        {
 6036            if (s[right] == s[left])
 5137            {
 5138                left++;
 5139            }
 6040        }
 41
 842        if (left == s.Length)
 443        {
 444            return s;
 45        }
 46
 447        var resultStringBuilder = new StringBuilder();
 48
 449        var nonPalindromeSuffix = s[left..];
 50
 2651        for (var i = nonPalindromeSuffix.Length - 1; i >= 0; i--)
 952        {
 953            resultStringBuilder.Append(nonPalindromeSuffix[i]);
 954        }
 55
 456        resultStringBuilder.Append(ShortestPalindrome(s[..left]));
 457        resultStringBuilder.Append(nonPalindromeSuffix);
 58
 459        return resultStringBuilder.ToString();
 1260    }
 61}