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
C++
#include <bits/stdc++.h>
using namespace std;
bool solution( int n, int m, int x, int y,
vector<string>& letter)
{
for ( int i = 0; i < n; i++) {
int len = 0;
for ( int j = 0; j < m; j++) {
if (letter[i][j] == '*' )
len++;
else {
if (len != 0 && len < x)
return false ;
else
len = 0;
}
}
}
for ( int i = 0; i < m; i++) {
int len = 0;
for ( int j = 0; j < n; j++) {
if (letter[j][i] == '*' )
len++;
else {
if (len != 0 && len < y)
return false ;
else
len = 0;
}
}
}
return true ;
}
int main()
{
int n = 3, x = 2, y = 2;
vector<string> letter = { "#***" , "#***" , "#***" };
int m = letter[0].size();
if (solution(n, m, x, y, letter))
cout << "Possible\n" ;
else
cout << "Impossible\n" ;
vector<string> letter2 = { "#***" , "#*#*" , "#***" };
m = letter2[0].size();
if (solution(n, m, x, y, letter2))
cout << "Possible\n" ;
else
cout << "Impossible\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static boolean solution( int n,
int m, int x,
int y,
String[] letter)
{
for ( int i = 0 ; i < n; i++) {
int len = 0 ;
for ( int j = 0 ; j < m; j++) {
if (letter[i].charAt(j)
== '*' )
len++;
else {
if (len != 0 && len < x)
return false ;
else
len = 0 ;
}
}
}
for ( int i = 0 ; i < m; i++) {
int len = 0 ;
for ( int j = 0 ; j < n; j++) {
if (letter[j].charAt(i)
== '*' )
len++;
else {
if (len != 0 && len < y)
return false ;
else
len = 0 ;
}
}
}
return true ;
}
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();
if (solution(n, m, x, y, letter))
System.out.println( "Possible" );
else
System.out.println( "Impossible" );
String[] letter2 = { "#***" ,
"#*#*" ,
"#***" };
m = letter2[ 0 ].length();
if (solution(n, m, x, y, letter2))
System.out.println( "Possible" );
else
System.out.println( "Impossible" );
}
}
|
Python3
def solution(n, m, x, y, letter):
for i in range (n):
len = 0
for j in range (m):
if (letter[i][j] = = '*' ):
len + = 1
else :
if ( len ! = 0 and len < x):
return False
else :
len = 0
for i in range (m):
len = 0
for j in range (n):
if (letter[j][i] = = '*' ):
len + = 1
else :
if ( len ! = 0 and len < y):
return False
else :
len = 0
return True
n = 3
x = 2
y = 2
letter = [ "#***" , "#***" , "#***" ]
m = len (letter[ 0 ])
if (solution(n, m, x, y, letter)):
print ( "Possible" )
else :
print ( "Impossible" )
letter2 = [ "#***" , "#*#*" , "#***" ]
m = len (letter2[ 0 ])
if (solution(n, m, x, y, letter2)):
print ( "Possible" )
else :
print ( "Impossible" )
|
C#
using System;
class GFG{
public static bool solution( int n, int m, int x, int y,
string [] letter)
{
for ( int i = 0; i < n; i++)
{
int len = 0;
for ( int j = 0; j < m; j++)
{
if (letter[i][j] == '*' )
len++;
else
{
if (len != 0 && len < x)
return false ;
else
len = 0;
}
}
}
for ( int i = 0; i < m; i++)
{
int len = 0;
for ( int j = 0; j < n; j++)
{
if (letter[j][i] == '*' )
len++;
else
{
if (len != 0 && len < y)
return false ;
else
len = 0;
}
}
}
return true ;
}
public static void Main( string [] args)
{
int n = 3, x = 2, y = 2;
string [] letter = { "#***" , "#***" , "#***" };
int m = letter.GetLength(0);
if (solution(n, m, x, y, letter))
Console.WriteLine( "Possible" );
else
Console.WriteLine( "Impossible" );
String[] letter2 = { "#***" , "#*#*" , "#***" };
m = letter2.GetLength(0);
if (solution(n, m, x, y, letter2))
Console.WriteLine( "Possible" );
else
Console.WriteLine( "Impossible" );
}
}
|
Javascript
<script>
function solution(n, m, x, y,letter)
{
for ( var i = 0; i < n; i++) {
var len = 0;
for ( var j = 0; j < m; j++) {
if (letter[i][j] == '*' )
len++;
else {
if (len != 0 && len < x)
return false ;
else
len = 0;
}
}
}
for ( var i = 0; i < m; i++) {
var len = 0;
for ( var j = 0; j < n; j++) {
if (letter[j][i] == '*' )
len++;
else {
if (len != 0 && len < y)
return false ;
else
len = 0;
}
}
}
return true ;
}
var n = 3, x = 2, y = 2;
var letter = [ "#***" , "#***" , "#***" ];
var m = letter[0].length;
if (solution(n, m, x, y, letter))
document.write( "Possible" + "<br>" );
else
document.write( "Impossible" + "<br>" );
var letter2 = [ "#***" , "#*#*" , "#***" ];
m = letter2[0].length;
if (solution(n, m, x, y, letter2))
document.write( "Possible" + "<br>" );
else
document.write( "Impossible" + "<br>" );
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
30 Mar, 2022
Like Article
Save Article