| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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 | | |
| | | 12 | | namespace LeetCode.Algorithms.WordSearch; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed 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) |
| | 3 | 25 | | { |
| | 3 | 26 | | var visited = new bool[board.Length, board[0].Length]; |
| | | 27 | | |
| | 14 | 28 | | for (var i = 0; i < board.Length; i++) |
| | 6 | 29 | | { |
| | 50 | 30 | | for (var j = 0; j < board[i].Length; j++) |
| | 21 | 31 | | { |
| | 21 | 32 | | if (board[i][j] != word[0]) |
| | 16 | 33 | | { |
| | 16 | 34 | | continue; |
| | | 35 | | } |
| | | 36 | | |
| | 5 | 37 | | if (Search(board, word, i, j, 0, visited)) |
| | 2 | 38 | | { |
| | 2 | 39 | | return true; |
| | | 40 | | } |
| | 3 | 41 | | } |
| | 4 | 42 | | } |
| | | 43 | | |
| | 1 | 44 | | return false; |
| | 3 | 45 | | } |
| | | 46 | | |
| | | 47 | | private static bool Search(IReadOnlyList<char[]> board, string word, int i, int j, int index, bool[,] visited) |
| | 44 | 48 | | { |
| | 44 | 49 | | if (index == word.Length) |
| | 2 | 50 | | { |
| | 2 | 51 | | return true; |
| | | 52 | | } |
| | | 53 | | |
| | 42 | 54 | | if (i < 0 || i >= board.Count || j < 0 || j >= board[0].Length) |
| | 12 | 55 | | { |
| | 12 | 56 | | return false; |
| | | 57 | | } |
| | | 58 | | |
| | 30 | 59 | | if (visited[i, j] || board[i][j] != word[index]) |
| | 16 | 60 | | { |
| | 16 | 61 | | return false; |
| | | 62 | | } |
| | | 63 | | |
| | 14 | 64 | | visited[i, j] = true; |
| | | 65 | | |
| | 14 | 66 | | if (Search(board, word, i + 1, j, index + 1, visited) || |
| | 14 | 67 | | Search(board, word, i - 1, j, index + 1, visited) || |
| | 14 | 68 | | Search(board, word, i, j + 1, index + 1, visited) || |
| | 14 | 69 | | Search(board, word, i, j - 1, index + 1, visited)) |
| | 9 | 70 | | { |
| | 9 | 71 | | return true; |
| | | 72 | | } |
| | | 73 | | |
| | 5 | 74 | | visited[i, j] = false; |
| | | 75 | | |
| | 5 | 76 | | return false; |
| | 44 | 77 | | } |
| | | 78 | | } |