Count Strings

Moderate
0/80
Average time to solve is 20m
profile
Contributed by
7 upvotes
Asked in company
Ola

Problem statement

Peter Parker (Spider-Man) want to time travel to save Tony Stark.

So Peter came to Doctor Strange for help. Doctor strange told Peter that he would help him if he can solve Strange’s problem. Peter accepts the challenge.

Doctor Strange gave Peter a grid having ‘N’ strings of size ‘M’ each. Then he gave Peter a string ‘S’. The task is to count the total number of paths in grid starting from any coordinate in the grid by moving only in left, right, up and down directions such that the string formed by characters in the path will be equal to ‘S’. Also, visiting a coordinate twice in a path is not allowed.

As Peter wants to bring Tony back as soon as possible, he called you for help.

The fate of Tony and Peter lies in your hand.

Detailed explanation ( Input/output format, Notes, Images )
Input Format :
The first line of input contains an integer 'T' representing the number of test cases.

Each test case’s first line contains two space-separated integers ‘N’ and ‘M’. Here ‘N’ denotes the number of rows and ‘M’ represents the number of columns in the grid.
The next ‘N’ lines contain a single string of length ‘M’.
The next line contains a string ‘S’. 
Output Format :
For each test case, print a single integer that denotes the total number of unique paths in the string.
The output of each test case will be printed in a separate line.
Constraints :
1 <= T <= 5
1 <= N, M <= 10
1 <= |S| <= N * M 

Where ‘T’ is the number of test cases, ‘N’ denotes the number of rows and ‘M’ denotes the number of columns in the grid, and |S| denotes the length of string ‘S’.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Both ‘S’ and grid contain only uppercase English characters.
Sample Input 1 :
2
2 2
UP 
PO
UP
2 3
GOD 
ODG
GOD
Sample Output 1 :
2 
3
Explanation of Sample Input 1 :
Test Case 1 :  
Given string ‘S’ is “UP”. The grid contains 2 paths that have the same value as ‘S’.
The paths are shown below :

1

Test Case 2 : 
Given string ‘S’ is “GOD”. The grid contains 3 paths that have the same value as ‘S’.
The paths are shown below :

2

Sample Input 2 :
2
2 6
NINJAS
OPIKAT
NINJAS
2 1
C
C
CC
Sample Output 2 :
1
2
Explanation of Sample Input 2 :
Test Case 1 :  
Given string ‘S’ is “NINJAS”. The grid contains 1 path that has the same value as ‘S’.
The paths are shown below :

3

Test Case 2 : 
Given string ‘S’ is “CC”. The grid contains 2 paths that have the same value as ‘S’.
The paths are shown below :

4

Here first path is (0,0) -> (0,1) and second path is (0,1) -> (0,0)
Hint

For each grid coordinate, try to find all valid paths starting from this coordinate.

Approaches (1)
Recursive Approach

The idea here is to use brute force. For each coordinate in the grid, we will count the total number of paths starting from this coordinate. 

 

Algorithm:

  • Declare a variable to count the total number of paths having a value equal to ‘S’, say ‘answer’.
  • Declare a direction array to traverse in all four directions, i.e. left, right, down and up.
  • Declare a function ‘isValid’ to check whether coordinate (x, y) is out of bound or not, i.e. check if ‘x’ lies between 0 and ‘N – 1’ and ‘y’ lies between 0 and ‘M – 1’. Here ‘N’, ‘M’ denotes the number of rows and columns in grid respectively.
  • Run two nested loops to traverse all coordinates in the grid.
  • Declare a 2-D ‘visited’ array to keep track of visited coordinates in the grid and initialise all its elements with false.
  • Let us denote current coordinate in the grid by (x, y). Call ‘countPaths’ function to get the total number of valid paths starting at coordinate (x, y) and add it to ‘answer’.
  • Finally, return the ‘answer’.

 

Description of the ‘countPaths’ function:

This function will take 6 arguments: ‘curX’ and ‘curY’ to keep track of current coordinate in the grid, 2  D grid and the string ‘S’ given in the problem, ‘curIndex’ to keep track of current index-matched so far of string ‘S’ and a 2 – D ‘visited’ array to keep track of visited elements in the grid. 

int countPaths(curX, curY, grid,S, curIndex, visited)

  • If curIndex >= length of string ‘S’ then return 1.
  • If ‘S[curIndex]’  is not equal to ‘grid[curX][curY]’ then return 0 as all paths starting from this coordinate will be invalid.
  • Declare a variable ‘count’ to count the total number of valid paths starting from ‘curX’, ‘curY’ having a value equal to ‘S’ and initialise it with zero.
  • Traverse all four directional neighbours(up, down, left, right) of (curX, curY) using direction array.
  • Let us denote the current neighbour of (curX, curY) by (x, y).
  • If (x, y) is not out of bounds and we haven’t visited (x, y) then recursively call ‘countPath’ function by incrementing ‘curIndex’ by one and passing (x, y) and add the value return by this recursion in ‘count’.
  • Return ‘count’.
Time Complexity

O( (N * M) * (3 ^ (N * M) ), where ‘N’ is the number of rows and ‘M’ is the number of columns in the grid.

 

Here, ‘countPath’ function takes O( (3 ^ (N * M) ) time in the worst case when all characters of grid and string ‘S’ are same and length of ‘S’ is (N * M). In this case, every possible path will be a valid path. In each recursive call, we make three more recursive calls to visit the neighbours of the current coordinate. Note that we cannot go back to the previous coordinate from where we have reached the current coordinate so we will recursively visit only three neighbours of current coordinate. In the worst case, we need to visit O(N * M) so elements. So it will require O( 3 ^ (N * M ) ) time. Also, we call ‘countPath’ for all nodes of which are N * M. 

Hence, the overall time complexity will be O(N * M) * O( 3 ^ (N * M ) ) = O( (N * M) * (3 ^ (N * M) ).

Space Complexity

O(N * M), where ‘N’ is the number of rows, and ‘M’ is the number of columns in the grid.

 

We are using a visited array that takes O(N * M) space. In the worst case, we need O(N * M) recursive calls which require O(N * M) stack space. 

Hence, the overall space complexity will be O(N * M) + O(N * M) = O(N * M).

Code Solution
(100% EXP penalty)
Count Strings
Full screen
Console