Skip to content

Commit 2ccfd71

Browse files
authored
Merge pull request #241 from paurush11/master
WordBoggle.cpp
2 parents e9b6680 + f55925b commit 2ccfd71

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

Data Structures/Graphs/WordBoggle.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Leetcode problem for Binary Tree
2+
// 105. Construct Binary Tree from Preorder and Inorder Traversal
3+
4+
// Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
5+
6+
// Example 1:
7+
8+
// Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
9+
// Output: [3,9,20,null,null,15,7]
10+
// Example 2:
11+
12+
// Input: preorder = [-1], inorder = [-1]
13+
// Output: [-1]
14+
----------------------------------------------------------------------------
15+
/**
16+
* Definition for a binary tree node.
17+
* struct TreeNode {
18+
* int val;
19+
* TreeNode *left;
20+
* TreeNode *right;
21+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
22+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
23+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
24+
* };
25+
*/
26+
class Solution {
27+
public:
28+
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
29+
int pre = 0;
30+
return maketree(pre,0,preorder.size()-1,inorder,preorder);
31+
}
32+
33+
TreeNode* maketree(int &pre, int start, int end,vector<int>& inorder, vector<int>& postorder){
34+
if(start>end || pre>postorder.size())return NULL;
35+
TreeNode* root = new TreeNode(postorder[pre]);
36+
int index = -1;
37+
for(int i = start;i<=end;i++){
38+
if(inorder[i] == postorder[pre]){
39+
index = i;
40+
break;
41+
}
42+
}
43+
pre++;
44+
root->left = maketree(pre,start, index-1,inorder,postorder);
45+
root->right = maketree(pre,index+1, end,inorder,postorder);
46+
return root;
47+
}
48+
};

0 commit comments

Comments
 (0)