<p>You are given an <code>m x n</code> matrix <code>board</code>, representing the<strong> current </strong>state of a crossword puzzle. The crossword contains lowercase English letters (from solved words), <code>&#39; &#39;</code> to represent any <strong>empty </strong>cells, and <code>&#39;#&#39;</code> to represent any <strong>blocked</strong> cells.</p>

<p>A word can be placed<strong> horizontally</strong> (left to right <strong>or</strong> right to left) or <strong>vertically</strong> (top to bottom <strong>or</strong> bottom to top) in the board if:</p>

<ul>
	<li>It does not occupy a cell containing the character <code>&#39;#&#39;</code>.</li>
	<li>The cell each letter is placed in must either be <code>&#39; &#39;</code> (empty) or <strong>match</strong> the letter already on the <code>board</code>.</li>
	<li>There must not be any empty cells <code>&#39; &#39;</code> or other lowercase letters <strong>directly left or right</strong><strong> </strong>of the word if the word was placed <strong>horizontally</strong>.</li>
	<li>There must not be any empty cells <code>&#39; &#39;</code> or other lowercase letters <strong>directly above or below</strong> the word if the word was placed <strong>vertically</strong>.</li>
</ul>

<p>Given a string <code>word</code>, return <code>true</code><em> if </em><code>word</code><em> can be placed in </em><code>board</code><em>, or </em><code>false</code><em> <strong>otherwise</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/10/04/crossword-ex1-1.png" style="width: 478px; height: 180px;" />
<pre>
<strong>Input:</strong> board = [[&quot;#&quot;, &quot; &quot;, &quot;#&quot;], [&quot; &quot;, &quot; &quot;, &quot;#&quot;], [&quot;#&quot;, &quot;c&quot;, &quot; &quot;]], word = &quot;abc&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> The word &quot;abc&quot; can be placed as shown above (top to bottom).
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/10/04/crossword-ex2-1.png" style="width: 180px; height: 180px;" />
<pre>
<strong>Input:</strong> board = [[&quot; &quot;, &quot;#&quot;, &quot;a&quot;], [&quot; &quot;, &quot;#&quot;, &quot;c&quot;], [&quot; &quot;, &quot;#&quot;, &quot;a&quot;]], word = &quot;ac&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong> It is impossible to place the word because there will always be a space/letter above or below it.</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/10/04/crossword-ex3-1.png" style="width: 478px; height: 180px;" />
<pre>
<strong>Input:</strong> board = [[&quot;#&quot;, &quot; &quot;, &quot;#&quot;], [&quot; &quot;, &quot; &quot;, &quot;#&quot;], [&quot;#&quot;, &quot; &quot;, &quot;c&quot;]], word = &quot;ca&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> The word &quot;ca&quot; can be placed as shown above (right to left). 
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == board.length</code></li>
	<li><code>n == board[i].length</code></li>
	<li><code>1 &lt;= m * n &lt;= 2 * 10<sup>5</sup></code></li>
	<li><code>board[i][j]</code> will be <code>&#39; &#39;</code>, <code>&#39;#&#39;</code>, or a lowercase English letter.</li>
	<li><code>1 &lt;= word.length &lt;= max(m, n)</code></li>
	<li><code>word</code> will contain only lowercase English letters.</li>
</ul>

class Solution:
    def placeWordInCrossword(self, board: List[List[str]], word: str) -> bool:
        for i in range(len(board)):
            for j in range(len(board[i])):
                if board[i][j] != '#':
                    for direction in ['up', 'down', 'right', 'left']:
                        # check if word is too long to fit
                        if direction == 'up':
                            if i - len(word) + 1 < 0:
                                continue
                        elif direction == 'down':
                            if i + len(word) - 1 >= len(board):
                                continue
                        elif direction == 'left':
                            if j - len(word) + 1 < 0:
                                continue
                        elif direction == 'right':
                            if j + len(word) - 1 >= len(board[i]):
                                continue
                        
                        if self.emptyPad(board, i, j, word, direction) and self.checkWordFit(board, i, j, word, direction):
                            return True
                    
        return False

    def checkWordFit(self, board, i, j, word, direction):
        if not word:
            return True

        if i < 0 or i >= len(board) or j < 0 or j >= len(board[0]): # out of bounds
            return False

        if board[i][j] == ' ' or board[i][j] == word[0]:
            horiz_change = 0
            vert_change = 0

            if direction == 'up':
                vert_change = -1
            elif direction == 'down':
                vert_change = 1
            elif direction == 'right':
                horiz_change = 1
            else:
                horiz_change = -1

            return self.checkWordFit(board, i + vert_change, j + horiz_change, word[1:], direction)
            
    def emptyPad(self, board, start_i, start_j, word, direction):
        if direction == 'left' or direction == 'right':
            if direction == 'left':
                left_j = start_j - len(word) + 1
                right_j = start_j
            else:
                left_j = start_j
                right_j = start_j + len(word) - 1

            if left_j > 0 and board[start_i][left_j - 1] != '#':
                return False
            if right_j < len(board[0]) - 1 and board[start_i][right_j + 1] != '#':
                return False
        else:
            if direction == 'up':
                top_i = start_i - len(word) + 1
                bottom_i = start_i
            else:
                top_i = start_i
                bottom_i = start_i + len(word) - 1

            if top_i > 0 and board[top_i - 1][start_j] != '#':
                return False
            if bottom_i < len(board) - 1 and board[bottom_i + 1][start_j] != '#':
                return False

        return True


        



        
        
        
32500