< Summary

Information
Class: LeetCode.Algorithms.FindTheIndexOfTheFirstOccurrenceInString.FindTheIndexOfTheFirstOccurrenceInStringWithKMP
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheIndexOfTheFirstOccurrenceInString\FindTheIndexOfTheFirstOccurrenceInStringWithKMP.cs
Line coverage
80%
Covered lines: 48
Uncovered lines: 12
Coverable lines: 60
Total lines: 106
Line coverage: 80%
Branch coverage
77%
Covered branches: 17
Total branches: 22
Branch coverage: 77.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
StrStr(...)81.25%161687.87%
ComputeLongestPrefixSuffixArray(...)66.66%7670.37%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheIndexOfTheFirstOccurrenceInString\FindTheIndexOfTheFirstOccurrenceInStringWithKMP.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.FindTheIndexOfTheFirstOccurrenceInString;
 13
 14/// <inheritdoc />
 15public class FindTheIndexOfTheFirstOccurrenceInStringWithKMP : IFindTheIndexOfTheFirstOccurrenceInString
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n + m)
 19    ///     Space complexity - O(m)
 20    /// </summary>
 21    /// <param name="haystack"></param>
 22    /// <param name="needle"></param>
 23    /// <returns></returns>
 24    public int StrStr(string haystack, string needle)
 225    {
 226        if (string.IsNullOrEmpty(needle))
 027        {
 028            return 0;
 29        }
 30
 231        if (string.IsNullOrEmpty(haystack))
 032        {
 033            return -1;
 34        }
 35
 236        var lps = ComputeLongestPrefixSuffixArray(needle);
 37
 238        var i = 0;
 239        var j = 0;
 40
 1241        while (i < haystack.Length)
 1142        {
 1143            if (needle[j] == haystack[i])
 744            {
 745                j++;
 746                i++;
 747            }
 48
 1149            if (j == needle.Length)
 150            {
 151                return i - j;
 52            }
 53
 1054            if (i >= haystack.Length || needle[j] == haystack[i])
 555            {
 556                continue;
 57            }
 58
 559            if (j != 0)
 160            {
 161                j = lps[j - 1];
 162            }
 63            else
 464            {
 465                i += 1;
 466            }
 567        }
 68
 169        return -1;
 270    }
 71
 72    private static int[] ComputeLongestPrefixSuffixArray(string needle)
 273    {
 274        var length = needle.Length;
 275        var lps = new int[length];
 76
 277        lps[0] = 0;
 78
 279        var len = 0;
 280        var i = 1;
 81
 882        while (i < length)
 683        {
 684            if (needle[i] == needle[len])
 085            {
 086                len++;
 087                lps[i] = len;
 088                i++;
 089            }
 90            else
 691            {
 692                if (len == 0)
 693                {
 694                    lps[i] = len;
 695                    i++;
 696                }
 97                else
 098                {
 099                    len = lps[len - 1];
 0100                }
 6101            }
 6102        }
 103
 2104        return lps;
 2105    }
 106}