Given file1, file2 you need to print the modification made in file 1 to get file2. That is if an element is removed from file1 to make file2, you need to append -ve sign before it, if new element is added in file1 to make file2, you need to append +ve sign before it. (Similar to git diff)
Example:
File 1: A B C
File 2: B C D
Output:
-A B C +D
You are given an m x n binary matrix grid where each cell is either 0 (empty) or 1 (occupied).
You are then given stamps of size stampHeight x stampWidth. We want to fit the stamps such that they follow the given restrictions and requirements:
Return true if it is possible to fit the stamps while following the given restrictions and requirements. Otherwise, return false.
Example 1:
Input:
grid = [[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0]], stampHeight = 4, stampWidth = 3
Output:
true
Explanation: We have two overlapping stamps (labeled 1 and 2 in the image) that are able to cover all the empty cells.
Example 2
Input:
grid = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]], stampHeight = 2, stampWidth = 2
Output:
false
Explanation: There is no way to fit the stamps onto all the empty cells without the stamps going outside the grid.
Question 2
Pre-Knowledge
Calculate the sub-matrix sum
Intituion/Algorithm
How to check if we can put a stamp at a position (say [i,j]):
Covering Grid With Stamps
Now at each location we will check
Time Complexity:O(M*N)
Code
int n = grid.size(), m = grid[0].size();
vector<vector<int>> suff(n+1, vector<int>(m+1));
for(int i = n-1; i >= 0; --i){
for(int j = m-1; j >= 0; --j){
suff[i][j] = grid[i][j]+ (j+1 < m ? suff[i][j+1] : 0 )+ (i+1 < n ? suff[i+1][j] : 0 )
- (j+1 < m && i+1 < n ? suff[i+1][j+1] : 0);
//Check if we can put a stamp starting at this cell, that is, All h * w cells starting at (i,j) are empty.
if(grid[i][j] == 0 && n - i >= h && m - j >= w && (suff[i][j] - suff[i+h][j] - suff[i][j+w] + suff[i+h][j+w]) == 0)
grid[i][j] = 2; //2 indicates start a stamp from here
}
}
int total = n*m - suff[0][0]; //total empty spots
for(int i = 0; i < n; ++i){
for(int till = -1, j = 0, flow = 1; j < m; ++j){
if(grid[i][j] == 2){
//start a stamp here
till = min(m-1, j + w - 1);
flow = -h + 1 ;
total--;
}
else if(till >= j) {
flow = -h + 1;
total--;
}
else if(grid[i][j] < 0){
flow = grid[i][j]+1;
total--;
}
if(i+1 < n && grid[i+1][j] != 2 && grid[i+1][j] != 1 && flow < 1)
grid[i+1][j] = flow;
}
}
return total == 0;
Question 1
Overview
We are given two strings str1 and str2 and we have to convert string 1 to string 2 by following two types of operation
Insert a character
We are not given that we have to minimize the number of moves also we are not given the size of str1 and str2 let it be 10^5.
Approach/Solution
one
and two
and start traversing the string1 and check if the str1[one]==str2[two]
then we can increase both pointers and print str[one]
.str1[one]!=str2[two]
then we print the negative sign and str[one]
as we need to delete that string and increase only one++
.str1.size() or str2.size()
we will break the loop.two
is equal to str2.size()
or not. If not we can print all the characters with a positive sign.Pseudo Code
int i = 0, j = 0;
while (i != s1.size() && j != s2.size())
{
if (s1[i] == s2[j])
{
cout << s1[i] << " ";
i++;
j++;
}
else
{
cout << "-" << s1[i] << " ";
i++;
}
}
if (j != s2.size())
{
for (int k = j; k < s2.size(); k++)
{
cout << "+" << s2[k] << " ";
}
}