Tags

  • AWS (7)
  • Apigee (3)
  • ArchLinux (5)
  • Array (6)
  • Backtracking (6)
  • BinarySearch (6)
  • C++ (19)
  • CI&CD (3)
  • Calculus (2)
  • DesignPattern (43)
  • DisasterRecovery (1)
  • Docker (8)
  • DynamicProgramming (20)
  • FileSystem (11)
  • Frontend (2)
  • FunctionalProgramming (1)
  • GCP (1)
  • Gentoo (6)
  • Git (15)
  • Golang (1)
  • Graph (10)
  • GraphQL (1)
  • Hardware (1)
  • Hash (1)
  • Kafka (1)
  • LinkedList (13)
  • Linux (27)
  • Lodash (2)
  • MacOS (3)
  • Makefile (1)
  • Map (5)
  • MathHistory (1)
  • MySQL (21)
  • Neovim (10)
  • Network (66)
  • Nginx (6)
  • Node.js (33)
  • OpenGL (6)
  • PriorityQueue (1)
  • ProgrammingLanguage (9)
  • Python (10)
  • RealAnalysis (20)
  • Recursion (3)
  • Redis (1)
  • RegularExpression (1)
  • Ruby (19)
  • SQLite (1)
  • Sentry (3)
  • Set (4)
  • Shell (3)
  • SoftwareEngineering (12)
  • Sorting (2)
  • Stack (4)
  • String (2)
  • SystemDesign (13)
  • Terraform (2)
  • Tree (24)
  • Trie (2)
  • TwoPointers (16)
  • TypeScript (3)
  • Ubuntu (4)
  • Home

    [LeetCode 79] Word Search

    Published Feb 17, 2022 [  Graph  ]

    Problem

    Given an m x n grid of characters board and a string word, return true if word exists in the grid.

    The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.

    Example 1:

    Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
    Output: true
    

    Example 2:

    Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
    Output: true
    

    Example 3:

    Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
    Output: false
    

    Constraints:

    - m == board.length
    - n = board[i].length
    - 1 <= m, n <= 6
    - 1 <= word.length <= 15
    - board and word consists of only lowercase and uppercase English letters.
    

    Thoughts

    • As the data is a 2D array, we can use DFS to 2D array
    • We can keep the index of the word we are checking and compare it with the board value

    TypeScript

    function exist(board: string[][], word: string): boolean {
        // it is always good to know the dimension of the 2D board
        const ROW: number = board.length;
        const COL: number = board[0].length;
        
        // we always need a set for dfs
        const visited: Set<string> = new Set<string>();
        
        const dfs = (row: number, col: number, index: number): boolean => {
            // recursive call always has base case
            // good one
            if(index === word.length) return true
            // bad one
            const key: string = `${row},${col}`;
            if(row < 0 || row >= ROW || col < 0 || col >= COL || visited.has(key) || board[row][col] !== word[index]) return false;
            
            // recursive call here
            visited.add(key)
            let res: boolean = ( dfs(row, col - 1, index + 1) ||
                    dfs(row - 1, col, index + 1) ||
                    dfs(row, col + 1, index + 1) ||
                    dfs(row + 1, col, index + 1))
            visited.delete(key)
            return res;
        }
        
        // driver logic
        for(let i = 0; i < ROW; i++){
            for(let j = 0; j < COL; j++){
                if(dfs(i, j, 0) === true) {
                    return true
                }
            }
        }
        
        return false;
    };
    

    Reference