|
| 1 | +// Given a dictionary of distinct words and an M x N board where every cell has one character. Find all possible words from the dictionary that can be formed by a sequence of adjacent characters on the board. We can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell. |
| 2 | +// Example 1: |
| 3 | + |
| 4 | +// Input: |
| 5 | +// N = 1 |
| 6 | +// dictionary = {"CAT"} |
| 7 | +// R = 3, C = 3 |
| 8 | +// board = {{C,A,P},{A,N,D},{T,I,E}} |
| 9 | +// Output: |
| 10 | +// CAT |
| 11 | +// Explanation: |
| 12 | +// C A P |
| 13 | +// A N D |
| 14 | +// T I E |
| 15 | +// Words we got is denoted using same color. |
| 16 | +// Example 2: |
| 17 | + |
| 18 | +// Input: |
| 19 | +// N = 4 |
| 20 | +// dictionary = {"GEEKS","FOR","QUIZ","GO"} |
| 21 | +// R = 3, C = 3 |
| 22 | +// board = {{G,I,Z},{U,E,K},{Q,S,E}} |
| 23 | +// Output: |
| 24 | +// GEEKS QUIZ |
| 25 | +// Explanation: |
| 26 | +// G I Z |
| 27 | +// U E K |
| 28 | +// Q S E |
| 29 | +// Words we got is denoted using same color. |
| 30 | + |
| 31 | +// Companies |
| 32 | +// Amazon Directi Facebook Google MakeMyTrip Microsoft Nvidia Yahoo |
| 33 | + |
| 34 | +// Expected Time Complexity: O(NW + RC^2) |
| 35 | +// Expected Auxiliary Space: O(NW + RC) |
| 36 | +------------------------------------------------------------ |
| 37 | + |
| 38 | +#include<bits/stdc++.h> |
| 39 | +using namespace std; |
| 40 | + |
| 41 | +class Solution { |
| 42 | +public: |
| 43 | + |
| 44 | + bool isfullyfound(int r, int c, int index, vector<vector<char>>& board, vector<vector<bool>>&visited, string word){ |
| 45 | + if(index == word.size()){ |
| 46 | + return true; |
| 47 | + } |
| 48 | + if(r>=0 && r<board.size() && c>=0 && c<board[0].size() && !visited[r][c] && word[index] == board[r][c]){ |
| 49 | + visited[r][c] = true; |
| 50 | + if( isfullyfound(r-1,c,index+1,board,visited,word) || |
| 51 | + isfullyfound(r+1,c,index+1,board,visited,word) || |
| 52 | + isfullyfound(r,c-1,index+1,board,visited,word) || |
| 53 | + isfullyfound(r,c+1,index+1,board,visited,word) || |
| 54 | + isfullyfound(r+1,c+1,index+1,board,visited,word) || |
| 55 | + isfullyfound(r+1,c-1,index+1,board,visited,word) || |
| 56 | + isfullyfound(r-1,c-1,index+1,board,visited,word) || |
| 57 | + isfullyfound(r-1,c+1,index+1,board,visited,word) |
| 58 | + )return true; |
| 59 | + |
| 60 | + visited[r][c] = false; |
| 61 | + } |
| 62 | + return false; |
| 63 | + } |
| 64 | + bool wordfound(string word,vector<vector<char>>& board){ |
| 65 | + int index = 0; |
| 66 | + vector<vector<bool>>visited(board.size(), vector<bool>(board[0].size(), false)); |
| 67 | + for(int i = 0;i<board.size();i++){ |
| 68 | + for(int j = 0;j<board[0].size();j++){ |
| 69 | + if(word[index] == board[i][j]){ |
| 70 | + if(isfullyfound(i,j,index,board,visited,word)){ |
| 71 | + return true; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return false; |
| 77 | + |
| 78 | + } |
| 79 | + vector<string> wordBoggle(vector<vector<char> >& board, vector<string>& dictionary) { |
| 80 | + vector<string>v; |
| 81 | + for(auto i: dictionary){ |
| 82 | + if(wordfound(i,board)){ |
| 83 | + v.push_back(i); |
| 84 | + } |
| 85 | + } |
| 86 | + return v; |
| 87 | + } |
| 88 | +}; |
| 89 | + |
0 commit comments