< Summary

Information
Class: LeetCode.Algorithms.WordSearch.WordSearchRecursive
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\WordSearch\WordSearchRecursive.cs
Line coverage
100%
Covered lines: 36
Uncovered lines: 0
Coverable lines: 36
Total lines: 78
Line coverage: 100%
Branch coverage
100%
Covered branches: 30
Total branches: 30
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Exist(...)100%88100%
Search(...)100%2222100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\WordSearch\WordSearchRecursive.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.WordSearch;
 13
 14/// <inheritdoc />
 15public class WordSearchRecursive : IWordSearch
 16{
 17    /// <summary>
 18    ///     Time complexity - O(m * n * 3^L), where m and n are the dimensions of the board and L is the length of the w
 19    ///     Space complexity - O(m * n), where m and n are the dimensions of the board
 20    /// </summary>
 21    /// <param name="board"></param>
 22    /// <param name="word"></param>
 23    /// <returns></returns>
 24    public bool Exist(char[][] board, string word)
 325    {
 326        var visited = new bool[board.Length, board[0].Length];
 27
 1428        for (var i = 0; i < board.Length; i++)
 629        {
 5030            for (var j = 0; j < board[i].Length; j++)
 2131            {
 2132                if (board[i][j] != word[0])
 1633                {
 1634                    continue;
 35                }
 36
 537                if (Search(board, word, i, j, 0, visited))
 238                {
 239                    return true;
 40                }
 341            }
 442        }
 43
 144        return false;
 345    }
 46
 47    private static bool Search(IReadOnlyList<char[]> board, string word, int i, int j, int index, bool[,] visited)
 4448    {
 4449        if (index == word.Length)
 250        {
 251            return true;
 52        }
 53
 4254        if (i < 0 || i >= board.Count || j < 0 || j >= board[0].Length)
 1255        {
 1256            return false;
 57        }
 58
 3059        if (visited[i, j] || board[i][j] != word[index])
 1660        {
 1661            return false;
 62        }
 63
 1464        visited[i, j] = true;
 65
 1466        if (Search(board, word, i + 1, j, index + 1, visited) ||
 1467            Search(board, word, i - 1, j, index + 1, visited) ||
 1468            Search(board, word, i, j + 1, index + 1, visited) ||
 1469            Search(board, word, i, j - 1, index + 1, visited))
 970        {
 971            return true;
 72        }
 73
 574        visited[i, j] = false;
 75
 576        return false;
 4477    }
 78}