< Summary

Information
Class: LeetCode.Algorithms.LongestPalindromicSubstring.LongestPalindromicSubstringTwoPointers
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LongestPalindromicSubstring\LongestPalindromicSubstringTwoPointers.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 62
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
LongestPalindrome(...)100%66100%
ExpandAroundCenter(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\LongestPalindromicSubstring\LongestPalindromicSubstringTwoPointers.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
 12namespace LeetCode.Algorithms.LongestPalindromicSubstring;
 13
 14/// <inheritdoc />
 15public class LongestPalindromicSubstringTwoPointers : ILongestPalindromicSubstring
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="s"></param>
 22    /// <returns></returns>
 23    public string LongestPalindrome(string s)
 1224    {
 1225        if (string.IsNullOrEmpty(s))
 126        {
 127            return string.Empty;
 28        }
 29
 1130        var left = 0;
 1131        var right = 0;
 32
 22433        for (var i = 0; i < s.Length; i++)
 10134        {
 10135            var oddLength = ExpandAroundCenter(s, i, i);
 10136            var evenLength = ExpandAroundCenter(s, i, i + 1);
 37
 10138            var maxLength = Math.Max(oddLength, evenLength);
 39
 10140            if (maxLength <= right - left)
 6741            {
 6742                continue;
 43            }
 44
 3445            left = i - ((maxLength - 1) / 2);
 3446            right = i + (maxLength / 2);
 3447        }
 48
 1149        return s.Substring(left, right - left + 1);
 1250    }
 51
 52    private static int ExpandAroundCenter(string s, int left, int right)
 20253    {
 35254        while (left >= 0 && right < s.Length && s[left] == s[right])
 15055        {
 15056            left--;
 15057            right++;
 15058        }
 59
 20260        return right - left - 1;
 20261    }
 62}