LeetCode in Racket

Medium

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:

Follow up: Could you use search pruning to make your solution faster with a larger board?

Solution

(define/contract (exist board word)
  (-> (listof (listof char?)) string? boolean?)
  (let* ([rows (length board)]
         [cols (length (first board))]
         [word-len (string-length word)])
    
    (define (dfs board x y index)
      (if (= index word-len)
          #t
          (let ([current (list-ref (list-ref board x) y)]
                [next-char (string-ref word index)])
            (define directions '((-1 0) (1 0) (0 -1) (0 1)))  ; up down left right
            (define new-board 
              (list-set board x 
                        (list-set (list-ref board x) y #\-)))
            
            (ormap
             (λ (dir)
               (let ([new-x (+ x (first dir))]
                     [new-y (+ y (second dir))])
                 (and (>= new-x 0) (< new-x rows)
                      (>= new-y 0) (< new-y cols)
                      (char=? (list-ref (list-ref board new-x) new-y) next-char)
                      (dfs new-board new-x new-y (add1 index)))))
             directions))))
    
    (for*/or ([i (range rows)]
              [j (range cols)])
      (and (char=? (list-ref (list-ref board i) j) 
                   (string-ref word 0))
           (dfs board i j 1)))))