{"id":22513473,"url":"https://github.com/emahtab/word-search","last_synced_at":"2026-03-19T23:03:04.329Z","repository":{"id":79525995,"uuid":"231798145","full_name":"eMahtab/word-search","owner":"eMahtab","description":"Word Search in a 2D Grid","archived":false,"fork":false,"pushed_at":"2022-04-14T06:37:29.000Z","size":34,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-30T08:04:41.339Z","etag":null,"topics":["dfs","leetcode","problem-solving","word-search"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-01-04T16:59:57.000Z","updated_at":"2022-04-14T06:37:32.000Z","dependencies_parsed_at":"2023-03-18T13:40:26.289Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/word-search","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/word-search","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fword-search","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fword-search/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fword-search/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fword-search/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/word-search/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fword-search/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29060624,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-03T22:28:58.191Z","status":"ssl_error","status_checked_at":"2026-02-03T22:28:56.515Z","response_time":96,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dfs","leetcode","problem-solving","word-search"],"created_at":"2024-12-07T03:12:26.866Z","updated_at":"2026-02-03T23:01:09.232Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Word Search\n## https://leetcode.com/problems/word-search\n\nGiven a 2D board and a word, find if the word exists in the grid.\n\nThe word can be constructed from letters of sequentially adjacent cell, where \"adjacent\" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.\n\n```\nExample:\n\nboard =\n[\n  ['A','B','C','E'],\n  ['S','F','C','S'],\n  ['A','D','E','E']\n]\n\nGiven word = \"ABCCED\", return true.\nGiven word = \"SEE\", return true.\nGiven word = \"ABCB\", return false.\n```\n## Approach :\nOk lets ask this question before we talk about code implementation, how do we solve this problem in our head :grey_question:\n\nWe first search for first matching letter in the grid which matches with the first letter of the given word.\nThen we try to find the match for remaining letters of the word in the grid.\nSearching for matching letters in 4 directions  :\n1. one column forward  (row, column + 1)\n2. one column backward (row, column - 1)\n3. one row above (row - 1, column)\n4. one row down  (row + 1, column)\n\n**Note that the order in which we look for matching letters doesn't matter, we can search in different order (2, 1, 3, 4) or (3, 4, 1, 2) etc.**\n\n\nSo we will iterate over grid row by row, scanning from first column to last column. We will first check if \n`board[row][column] == word.charAt(0)` is true, if it is, it means now we can start our search.\n\n#### 2 Base cases :\n\n1. We will define a recursive method which will help us find the matches at index 1, index 2, index 3 and so on. We will increment the index by one, once we find a match for the character in the grid. If the value of index becomes equal to length of the given word, it means we found the entire match and we will return true (this will be one of our base case in the recursive method).\n\n2. Also we need to check whether the value of row and column are within the bounds of the board and \nif `board[row][column] != word.charAt(index)`, if not we will return false (this will be our second base case in the recursive method)\n\nOtherwise it means, we haven't found the entire word match and the character at `index` matches with `board[row][column]`\nSo now we search for next characters match in 4 cells `(row, column + 1)  (row, column - 1)  (row - 1, column)  (row + 1, column)` , since we have to look for match, for the next character in the word we increment the index by one.\n\nIf any one of above four searches ( 4 recursive calls) resturns true we return true.\n\n### Handling False Match\nSince our implementation is recursive, we may get in a scenario where, we have an exact match for a character but we have already counted that cell as a match before in our search. \n\n**If we consider a cell, where we already found a character match, again, this will result in a false match.** \n\nFor example in the given board below, there is no match for the word `ABCCC` , but if we don't mark the already matched characters in our search, our implementation will result true, which will be wrong.\n\n```\n[\n  ['A','B','C','E'],\n  ['S','F','C','S'],\n  ['A','D','E','E']\n]\n```\n\nSo to handle the false match issue, we mark each matched cell in our search with `#` symbol. By doing this we will avoid finding the same cell match for the character.\n\n```\n[\n  ['#','#','#','E'],\n  ['S','F','#','S'],\n  ['A','D','E','E']\n]\n```\nAnd our search will return false, because it won't find match for the last `C` in the word `ABCCC`\n\n**We revert the value in the board to its previous value once we finish one entire search ( update # to actual value which was present at that cell)**\n\n**❗️ :exclamation: You can add breakpoint in the code to see, cell values updating to # as we progress in our search**\n\n## Implementation :\n\n```java\npublic  boolean exist(char[][] board, String word) {\n\tif(board == null || board.length == 0)\n\t\treturn false;\n\t\t\n\tfor(int row = 0; row \u003c board.length; row++) {\n\t    for(int column = 0; column \u003c board[0].length; column++) {\n\t\t if(board[row][column] == word.charAt(0) \u0026\u0026 searchWord(board, row, column, word, 0)) {\n\t\t\treturn true;\n\t\t }\n\t    }\n\t}\n    return false;\n}\n\nprivate  boolean searchWord(char[][] board, int row, int column, String word, int index) {\n\tif(index == word.length()) {\n\t   return true;\n\t}\n\tif(row \u003c 0 || row \u003e= board.length || column \u003c 0 || column \u003e= board[0].length || \n\t     board[row][column] != word.charAt(index)) {\n\t     return false;\n\t}\n\t\t\n\tchar temp = board[row][column];\n\tboard[row][column] = '#';\n\t\t\n\tboolean res = searchWord(board, row, column + 1, word, index + 1) ||\n\t\t      searchWord(board, row, column - 1, word, index + 1) ||\n\t\t      searchWord(board, row - 1, column, word, index + 1) ||\n\t\t      searchWord(board, row + 1, column, word, index + 1);\n\t\t\n\tboard[row][column] = temp;\n\treturn res;\t\t      \n}\n\n```\n\n## Important Note : OR\n```java\nboolean res = searchWord(board, row, column + 1, word, index + 1) ||\n\t\t      searchWord(board, row, column - 1, word, index + 1) ||\n\t\t      searchWord(board, row - 1, column, word, index + 1) ||\n\t\t      searchWord(board, row + 1, column, word, index + 1);\n```\nwe are using OR operation, since we just want any one match. And we don't want to keep our search running if we found one match. This is very important.\nIf we don't use the OR operation, the search will keep running till it searches all possible combination.\n**If we want to return on the first match of the word in the grid, then OR operation is must.**\n\n\n## References :\nhttps://www.youtube.com/watch?v=vYYNp0Jrdv0\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fword-search","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fword-search","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fword-search/lists"}