Check if a Matrix can be superimposed on the given Matrix
Given a matrix letter[][] of size N * M, composed of ‘#’ and ‘*’ and another matrix stamp[][] of size X * Y containing only ‘$’. The task is to find if all the ‘*’ of the larger one can be replaced by ‘$’ by superimposing the stamp matrix on the letter matrix.
Note: In a superimpose operation only a area having all the characters as ‘*’ or ‘$’ can be considered.
Examples:
Input: N = 3, M =5, X = 2, Y=2
Letter Matrix:
#****
#****
#****
Stamp Matrix:
$$
$$
Output: Possible
Explanation:
1st Step: Superimpose letter matrix from (0,1) to (1,2) , So, letter will look like ( Remember the place where ‘#’ is placed can’t be imposed)
#$$**
#$$**
#****
2nd Step: Superimpose letter matrix from (0,3) to (1,4), Letter becomes
#$$$$
#$$$$
#****
3rd Step: Since superimpose over ‘$’ is also allowed, do overlapping. Hence stamping next from (1,1) to (2,3)
#$$$$
#$$$$
#$$**
Final step: Again do overlapping, and stamp from (1,3) to (3,4)
#$$$$
#$$$$
#$$$$
Input: N = 3, M =5, X = 2, Y=2
Letter Matrix:
#**#*
#****
#****
Stamp Matrix:
$$
$$
Output: Impossible
Explanation: The ‘#’ at (0,3) cannot be superimposed.
Approach: The problem can be solved by checking the number of ‘*’ characters between two ‘#’ or at the end and starting of a row. If any of these counts are less than the row length of stamp matrix then solution is not possible, Same is applicable for columns also. Follow the steps mentioned below to implement the approach:
- Loop over complete larger matrix row wise
- For each row, count the number of consecutive ‘*’ at the beginning or before end of row or between two ‘#’. If this count is smaller than stamp matrix row length then its impossible and return impossible as answer.
- Check the whole letter matrix in this manner.
- Apply the same process for the columns also.
- If the letter matrix can be superimposed return true. Otherwise, return false.
Below is the implementation of the above approach
Java
// Java code to implement the above approachimport java.util.*; class GFG { // Function to check if superimpose // is possible or not public static boolean solution(int n, int m, int x, int y, String[] letter) { // Looping over rows for (int i = 0; i < n; i++) { // Initialzing a length variable // for counting the number of * int len = 0; for (int j = 0; j < m; j++) { // If character encountered // is *, then we // incrment the length if (letter[i].charAt(j) == '*') len++; // If its not then there are // two cases else { // 1st case is length is // smaller than number of // rows in stamp matrix, // that would be impossible // to stamp so return false if (len != 0 && len < x) return false; // 2nd case is if its // possible, reset len else len = 0; } } } // Do imilarly for columns // Looping over columns for (int i = 0; i < m; i++) { // Initialzing length variable int len = 0; for (int j = 0; j < n; j++) { // If encountered character // is *, increment length if (letter[j].charAt(i) == '*') len++; else { // If its not possible // reutrn false if (len != 0 && len < y) return false; // Otherwise reset len else len = 0; } } } return true; } // Driver code public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = 3, x = 2, y = 2; String[] letter = { "#***", "#***", "#***" }; int m = letter[0].length(); /* Stamp Matrix: $$ $$ A 2*2 matrix ( x*y) */ if (solution(n, m, x, y, letter)) System.out.println("Possibe"); else System.out.println("Impossible"); // 2nd case String[] letter2 = { "#***", "#*#*", "#***" }; m = letter2[0].length(); if (solution(n, m, x, y, letter2)) System.out.println("Possibe"); else System.out.println("Impossible"); }} |
Possibe Impossible
Time Complexity: O(N*M)
Auxiliary Space: O(1)


