search
Log In
0 votes
166 views

Input

You are given a template in which you need to implement a function whose signature is given below.

C
int findWordInAGrid(char grid[128][128], int m, int n, char word[32])
/* return 0 for false, 1 for true. */

C++
bool findWordInAGrid(char grid[128][128], int m, int n, char word[32])

Java
static boolean findWordInAGrid(char[][] grid, int m, int n, String word)

grid[][] represents the characters that are in the given grid. Only the first m rows and the first n columns should be considered relevant. word is the word whose occurance has to be found in the grid. Remember, word may start from any location in the grid. word will never contain more than 30 characters. Return true if word is found in the grid, false otherwise.

Output

The function should return true, if you can find the word in the grid and false otherwise.

Example

Let's consider the grid given below:

a b c
d e f
g h i

And set of words to be searched are:

abc
abedhi
efgh

Output:

The output of the above example should be:

abc: true
abedhi: true
efgh: false

Constraints

1 ≤ m,n ≤ 100
in Puzzles 166 views

1 Answer

0 votes
int findWordInAGrid(char grid[128][128], int m, int n, char word[32]){
    char str[30];
    int c=0;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            if(grid[i][j]==word[c]){
                str[c]=grid[i][j];
                c++;
            }
        }
        if(strcmp(str,word)==0){
            return 1;
        }else{
            strcpy(str,"");
            c=0;
        }
    }
    return 0;
}

Related questions

0 votes
2 answers
1
202 views
Given f(n) = θ(n), g(n) = Ω(n), h(n) = O(n). Then f(n) + [g(n) ⋅ h(n)] = ? how to solve??
asked Jul 15, 2018 in Puzzles vijju532 202 views
1 vote
2 answers
2
1 vote
0 answers
3
417 views
Which of the following statements is/are correct? P:In Huffman Coding, the item with the second lowest probability is always at the leaf that is furthest from the root Q: In Huffman Coding, the item with the highest probability is always at the leaf that is closest to the root R: In ... at a leaf that is the child of the root Edit :Answer is P and Q R is not always true and always word i missed :(
asked Jan 27, 2018 in Algorithms sunil sarode 417 views
1 vote
3 answers
4
791 views
The following message is: GATE2018GAATTTEEEE22000011188 What is the average length of bits required for encoding each letter using Huffman coding___?
asked Nov 17, 2017 in Algorithms Parshu gate 791 views
...