You are on page 1of 11

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


COMPLEX PROBLEMS

Student Name: Sanidhya Prakash UID: 21BCS11746


Branch: CSE Section/Group: 21BCS_KRG-CC-2-B
Semester: 06 Subject Name: Advance Programming-2

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>

using namespace std;

bool isMatch(string s, string p) {


int m = s.length();
int n = p.length();

vector<vector<bool>> dp(m + 1, vector<bool>(n + 1, false));

dp[0][0] = true;

for (int j = 1; j <= n; j++) {


if (p[j - 1] == '*') {
dp[0][j] = dp[0][j - 2];

}
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];
}

int main() { string


s, p;

cout << "Enter the string S: "; cin


>> s;

cout << "Enter the pattern P: ";


cin >> p;

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>

using namespace std;


struct Cell { int row;
int col; int distance;

};
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 numCols = matrix[0].size(); if


(numCols == 0) return -1;

vector<vector<bool>> visited(numRows, vector<bool>(numCols, false));


queue<Cell> q;

q.push({source.first, source.second, 0});


visited[source.first][source.second] = true;

int dr[] = {-1, 1, 0, 0};


int dc[] = {0, 0, -1, 1};

while (!q.empty()) { Cell current = q.front(); q.pop(); if (current.row ==


destination.first && current.col == destination.second) { return
current.distance;
} for (int i = 0; i < 4; ++i) { int
newRow = current.row + dr[i]; int
newCol = current.col + dc[i];

if (isValid(newRow, newCol, numRows, numCols, matrix, visited)) {


q.push({newRow, newCol, current.distance + 1});
visited[newRow][newCol] = true;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}}
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}
};

pair<int, int> source = {0, 0};


pair<int, int> destination = {3, 4};

int shortestPathLength = shortestPath(matrix, source, destination);

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>

using namespace std;

void buildGraph(const vector<string>& words, vector<vector<int>>& graph, vector<int>&


indegree) { for (const string& word : words) { for (char c : word) { graph[c - 'a'].clear();
indegree[c - 'a'] = 0;
}}
for (int i = 0; i < words.size() - 1; ++i) { const string&
word1 = words[i]; const string& word2 = words[i
+ 1]; int minLen = min(word1.length(),
word2.length()); for (int j = 0; j < minLen; ++j) {
if (word1[j] != word2[j]) { int from = word1[j] - 'a';
int to = word2[j] - 'a'; graph[from].push_back(to);
indegree[to]++; break;
}
}
}
}

string alienOrder(const vector<string>& words) {


vector<vector<int>> graph(26);
vector<int> indegree(26, -1);
buildGraph(words, graph,
indegree); queue<int> q; string
result = ""; for (int i = 0; i < 26; ++i)
{
if (indegree[i] == 0) {
q.push(i); result +=
(char)(i + 'a');
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
} } while
(!q.empty()) {
int curr = q.front(); q.pop(); for
(int neighbor : graph[curr]) {
indegree[neighbor]--; if
(indegree[neighbor] == 0) {
q.push(neighbor); result +=
(char)(neighbor + 'a');
}
} } for (int i = 0; i < 26;
++i) {
if (indegree[i] > 0) {
return "";
}
}

return result;
}

int main() { vector<string> words1 = {"baa", "abcd", "abca", "cab", "cad"};


cout << "Order of characters for words1: " << alienOrder(words1) << endl;
vector<string> words2 = {"caa", "aaa", "aab"}; cout << "Order of characters
for words2: " << alienOrder(words2) << endl;

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>

using namespace std;

int minEditDistance(string str1, string str2) { int m =

str1.length(); int n = str2.length();

vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));

for (int i = 0; i <= m; ++i) { dp[i][0]


= i;
} for (int j = 0; j <= n; ++j)
{ dp[0][j] = j;
} for (int i = 1; i <= m; ++i) { for
(int j = 1; j <= n; ++j) { if (str1[i
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
- 1] == str2[j - 1]) { dp[i][j] =
dp[i - 1][j - 1];
} else { dp[i][j] = 1 + min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j
- 1]});
}
}
}

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

You might also like