Professional Documents
Culture Documents
Problem 1: Given two strings S and P where S consists of only lowercase English alphabets
while P consists of lowercase English alphabets as well as special characters ‘.’ and ‘*’, the task
is to implement a function to test regular expression such that:
'.' Matches any single character.
'*' Matches zero or more of the preceding elements.
Note: For each appearance of the character ‘*', there will be a previous valid character
to match. Examples:
Input: s = "aaa", p = "a"
Output: false
Code:
#include <iostream>
#include <vector>
#include <string>
dp[0][0] = true;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
} for (int i = 1; i <= m; i++) { for (int j =
1; j <= n; j++) { if (p[j - 1] == '.' || p[j - 1]
== s[i - 1]) { dp[i][j] = dp[i - 1][j - 1];
} else if (p[j - 1] == '*') { dp[i][j] = dp[i][j - 2] || (dp[i - 1][j] && (s[i - 1]
== p[j - 2] || p[j - 2] == '.'));
} else { dp[i][j] =
false;
}
} } return
dp[m][n];
}
cout << "Does pattern P match string S? " << std::boolalpha << isMatch(s, p) << endl;
return 0;
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Problem 3: Given an MxN matrix where each element can either be 0 or 1. We need to find the shortest
path between a given source cell to a destination cell. The path can only be created out of a cell if its
value is 1.
Example:
Input: mat[ROW][COL] = {{1, 0, 1, 1, 1, 1, 0, 1,
1, 1 },
{1, 0, 1, 0, 1, 1, 1, 0, 1, 1 },
{1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },
{0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{1, 1, 1, 0, 1, 1, 1, 0, 1, 0 },
{1, 0, 1, 1, 1, 1, 0, 1, 0, 0 },
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
{1, 1, 0, 0, 0, 0, 1, 0, 0, 1 }};
Source = {0, 0};
Destination = {3, 4};
Output:
Shortest Path is 11
Code:
#include <iostream>
#include <queue>
#include <vector>
#include <climits>
};
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
bool isValid(int row, int col, int numRows, int numCols, const vector<vector<int>>& matrix,
vector<vector<bool>>& visited) { return (row >= 0 && row < numRows && col >= 0 && col <
numCols && matrix[row][col] == 1 && !visited[row][col]);
} int shortestPath(const vector<vector<int>>& matrix, pair<int, int> source, pair<int, int>
destination)
{ int numRows = matrix.size();
if (numRows == 0) return -1;
int main() {
vector<vector<int>> matrix = {
{1, 0, 1, 1, 1, 1, 0, 1, 1, 1},
{1, 0, 1, 0, 1, 1, 1, 0, 1, 1},
{1, 1, 1, 0, 1, 1, 0, 1, 0, 1},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
{1, 1, 1, 0, 1, 1, 1, 0, 1, 0},
{1, 0, 1, 1, 1, 1, 0, 1, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 1, 1, 1},
{1, 1, 0, 0, 0, 0, 1, 0, 0, 1}
};
if (shortestPathLength != -1) {
cout << "Shortest Path is " << shortestPathLength << endl;
} else { cout << "Destination is unreachable" <<
endl;
}
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
Problem 7: - Given an array points [][] of size N, where points[i] represents a balloon over the area
of X-coordinates from points[i][0] to points[i][1]. The Y-coordinates don’t matter. All the balloons
are required to be burst. To burst a balloon, an arrow can be launched at point (x, 0) and it travels
vertically upwards and bursts all the balloons which satisfy the condition points[i][0] <= x <=
points[i][1]. The task is to find the minimum number of arrows required to burst all the balloons.
Examples:
Input: N = 4, points = {{10, 16}, {2, 8}, {1, 6}, {7, 12}}
Output: 2
Explanation: One way is to shoot one arrow for example at x = 6 (bursting the balloons [2, 8] and [1, 6])
and another arrow at x = 11 (bursting the other two balloons).
Input: N = 1, points = {{1, 6}}
Output: 1
Explanation: One single arrow can burst the balloon.
Code:
#include <iostream>
#include <vector> #include <algorithm> using namespace std; int
minArrowsToBurstBalloons(vector<vector<int>>& points) { if (points.empty())
return 0; sort(points.begin(), points.end(), [](const vector<int>& a, const
vector<int>& b) { return a[1] < b[1];
});
int arrows = 1;
int end_point = points[0][1]; for (int
i = 1; i < points.size(); ++i) { if
(points[i][0] > end_point) {
arrows++; end_point = points[i][1];
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
return arrows;
} int main() { vector<vector<int>> points1 = {{10, 16}, {2, 8}, {1,
6}, {7, 12}}; cout << "Minimum number of arrows required for
Example 1: " <<
minArrowsToBurstBalloons(points1) << endl; vector<vector<int>>
points2 = {{1, 6}}; cout << "Minimum number of arrows required
for Example 2: " <<
minArrowsToBurstBalloons(points2) << endl;
return 0;
}
Output:
Problem 8: - Given a sorted dictionary (array of words) of an alien language, find the order of
characters in the language.
Examples:
Input: words[] = {“baa”, “abcd”, “abca”, “cab”, “cad”}
Output: Order of characters is ‘b’, ‘d’, ‘a’, ‘c’
Explanation: Note that words are sorted and in the given language “baa” comes before “abcd”, therefore
‘b’ is before ‘a’ in output. Similarly we can find other orders.
Input: words[] = {“caa”, “aaa”, “aab”}
Output: Order of characters is ‘c’, ‘a’, ‘b’
Code:
#include <iostream>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
#include <vector>
#include <unordered_set>
#include <queue>
return result;
}
return 0;
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Problem 9: - Given two strings str1 and str2 of length M and N respectively and below operations
that can be performed on str1. Find the minimum number of edits (operations) to convert ‘str1‘
into ‘str2‘.
Operation 1 (INSERT): Insert any character before or after any index of str1
Operation 2 (REMOVE): Remove a character of str1
Operation 3 (Replace): Replace a character at any index of str1 with some other character.
Note: All of the above operations are of equal cost. Examples:
Input: str1 = “geek”, str2 = “gesek”
Output: 1
Explanation: We can convert str1 into str2 by inserting a ‘s’ between two consecutive ‘e’ in str2.
Input: str1 = “cat”, str2 = “cut” Output:
1
Explanation: We can convert str1 into str2 by replacing ‘a’ with ‘u’.
Input: str1 = “sunday”, str2 = “saturday”
Output: 3
Explanation: Last three and first characters are same. We basically need to convert “un” to “atur”. This
can be done using below three operations. Replace ‘n’ with ‘r’, insert t, insert a
Code:
#include <iostream>
#include <vector>
#include <algorithm>
return dp[m][n];
}
int main() { string str1_1 = "geek"; string str2_1 = "gesek"; cout << "Minimum number of edits
for Input 1: " << minEditDistance(str1_1, str2_1) << endl; string str1_2 = "cat"; string str2_2
= "cut";
cout << "Minimum number of edits for Input 2: " << minEditDistance(str1_2, str2_2) << endl;
string str1_3 = "sunday"; string str2_3 = "saturday"; cout << "Minimum number of edits for
Input 3: " << minEditDistance(str1_3, str2_3) << endl;
return 0;
}
Output:
Learning Objectives:
How to solve complex problems