Count of number of given string in 2D character array in C++


The following problem is an example of daily newspaper crossword, here we are given a 2-Dimensional character array and the problem statement is to find out the given word from the 2-Dimensional character array maze.The search algorithm includes finding the individual characters from Top-to-Bottom,Right-to-Left and vice-versa but not diagonally.

Let us understand with examples

Input- String word-LAYS

2D String[ ] -  { "LOAPYS", "KAYSOT", "LAYSST",  "MLVAYS", "LAYSAA", "LAOYLS" };

Output- Count of number of given strings in 2D character array: 7

Explanation - As we are given with the string array of words and from them, we have to find the word “LAYS” which can be searched in any direction like Top-Bottom, Right-Left, Bottom-Top, and Left-Right. The counter flag in the code adds up every time the given search string is found, and the count is returned at end for the result. In the example, we can see LAYS is formed 7 times i.e.

1->LOAPYS -LAYS->  Left to Right

2 ->SAYAOL-LAYS(Right to Left)

3->LAYSST-LAYS(Left to Right)

4->MLVAYS-LAYS(Left to Right)

5->LAYSAA-LAYS(Left to Right) 

6->LAOYLS-LAYS(Left to Right)

7->(Bottom to Top) in Red LAYS

Input- String word- CAMP

2D String[ ] - { "BLOOKS", "BPOOLK", "KOHPKB", "BOLKOK", "LKIOOB", "LAHYBL" }

Output-Count of number of given string in 2D character array: 0

Explanation-: As we are given with the string array of words and from them, we have to find the word “LAYS” which can be searched in any direction like Top-Bottom, Right-Left, Bottom-Top, and Left-Right. The counter flag in the code adds up every time the given search string is found, and the count is returned at end for the result. In the example, we can see BOOK is formed times. 

Approach used in the below program is as follows

  • We are given with a String(word) and String array which along with some utility variables are passed into findString() for further processing.
  • The characters in the matrix are then traversed and a character is picked up to start the string.
  • For the character that has been picked up we find for the the given string recursively in all the possible direction according to the algorithm
  • If a match is found the counter is then incremented
  • After we are done with the first start character, the process is then repeated for next character
  • The sum of counts is then calculated with the corresponding matches
  • The final answer is then captured, and the result is printed. 

Example

#include <bits/stdc++.h>
using namespace std;
int utilitySearch(string word, int r, int c, string arr[], int maxR, int maxC, int index) {
   int count = 0;
   if (r >= 0 && r <= maxR && c >= 0) {
      if (c <= maxC && word[index] == arr[r][c]) {
         char res = word[index];
         index = index + 1;
         arr[r][c] = 0;
         if (word[index] == 0) {
            count = 1;
         } else {
            count = count + utilitySearch(word, r, c + 1, arr, maxR, maxC, index);
            count = count + utilitySearch(word, r, c - 1, arr, maxR, maxC, index);
            count = count + utilitySearch(word, r + 1, c, arr, maxR, maxC, index);
            count = count + utilitySearch(word, r - 1, c, arr, maxR, maxC, index);
         }
         arr[r][c] = res;
      }
   }
   return count;
}

int findString(string word, int r, int c, string str[], int countR, int countC) {
   int count = 0;
   for (int i = 0; i < countR; ++i) {
      for (int j = 0; j < countC; ++j) {
         count = count + utilitySearch(word, i, j, str, countR - 1, countC - 1, 0);
      }
   }
   return count;
}

int main() {
      string word = "FLOOD";
      string inp[] = {"FPLIOKOD","FLOODYUT","YFLOODPU","FMLOSODT","FILPOYOD", FLOOOODE " };
         string str[(sizeof(inp) / sizeof( * inp))];
         for (int i = 0; i < (sizeof(inp) / sizeof( * inp)); ++i) {
            str[i] = inp[i];
         }
         cout << "Count of number of given string in 2D character array: " << findString(word, 0, 0, str, (sizeof(inp) / sizeof( * inp)), str[0].size());
         return 0;
}

If we run the above code it will generate the following output −

Output

Count of number of given string in 2D character array: 6

Updated on: 29-Jan-2021

428 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements