< Summary

Information
Class: LeetCode.Algorithms.PalindromePartitioning.PalindromePartitioningBackTracking
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PalindromePartitioning\PalindromePartitioningBackTracking.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 72
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
Partition(...)100%11100%
Backtrack(...)100%66100%
IsPalindrome(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PalindromePartitioning\PalindromePartitioningBackTracking.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.PalindromePartitioning;
 13
 14/// <inheritdoc />
 15public class PalindromePartitioningBackTracking : IPalindromePartitioning
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * 2^n)
 19    ///     Space complexity - O(n * 2^n)
 20    /// </summary>
 21    /// <param name="s"></param>
 22    /// <returns></returns>
 23    public IList<IList<string>> Partition(string s)
 224    {
 225        IList<IList<string>> result = new List<IList<string>>();
 26
 227        Backtrack(s, 0, new List<string>(), result);
 28
 229        return result;
 230    }
 31
 32    private static void Backtrack(string s, int start, IList<string> currentPartition,
 33        ICollection<IList<string>> result)
 834    {
 835        if (start >= s.Length)
 336        {
 337            result.Add(new List<string>(currentPartition));
 38
 339            return;
 40        }
 41
 2642        for (var end = start; end < s.Length; end++)
 843        {
 844            if (!IsPalindrome(s, start, end))
 245            {
 246                continue;
 47            }
 48
 649            currentPartition.Add(s.Substring(start, end - start + 1));
 50
 651            Backtrack(s, end + 1, currentPartition, result);
 52
 653            currentPartition.RemoveAt(currentPartition.Count - 1);
 654        }
 855    }
 56
 57    private static bool IsPalindrome(string s, int start, int end)
 858    {
 959        while (start < end)
 360        {
 361            if (s[start] != s[end])
 262            {
 263                return false;
 64            }
 65
 166            start++;
 167            end--;
 168        }
 69
 670        return true;
 871    }
 72}