Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Program to find length of shortest supersequence in Python
A supersequence is a string that contains both input strings as subsequences. The shortest supersequence problem finds the minimum length string that includes all characters from both strings while preserving their order. For example, if s = "pipe" and t = "people", then "pieople" is a supersequence with length 7. Algorithm Approach The solution uses dynamic programming to find the Longest Common Subsequence (LCS), then calculates the shortest supersequence length using the formula: Shortest Supersequence Length = len(s) + len(t) - LCS_length Step-by-Step Process Create a 2D table to store LCS lengths Fill ...
Read MoreProgram to find distance of shortest bridge between islands in Python
Suppose we have a binary matrix, where 0 represents water and 1 represents the land. An island is a group of connecting 1s in 4 directions. Islands are either surrounded by 0s (water) or by the edges. We have to find the length of shortest bridge that connects two islands. So, if the input is like: 0 0 ...
Read MoreProgram to evaluate s-expression as string in Python
Suppose we have a string s as S-expression. We have to evaluate that S-expression and return result as integer. As we know that the S-expression is an expression which is either one number, or a recursive expression wrapped in parentheses like (+ (- 3 2) (* 3 3)), which indicates (3 - 2) + (3 * 3) = 10. Here valid operators are +, -, *, and /. So, if the input is like s = "(- (+ 3 2) 2)", then the output will be 3, as ((3 + 2) - 2) = 3. Algorithm Steps ...
Read MoreProgram to check regular expression pattern is matching with string or not in Python
Regular expression pattern matching is a common programming problem where we need to check if a string matches a given pattern. In Python, we can implement this using dynamic programming with recursive approach. Regular Expression Rules The pattern matching follows these rules ? . (period) matches any single character * (asterisk) matches zero or more of the preceding element Algorithm Steps To solve this problem, we follow these steps ? Get the length of string s and pattern p Create a recursive function dp(i, j) where i is the current position ...
Read MoreProgram to count number of unique paths that includes given edges in Python
Given a list of edges representing a tree, we need to find the total number of unique paths that include each edge. A path in a tree connects any two nodes, and we count how many such paths pass through each given edge. Problem Understanding For the input edges = [[0, 1], [0, 2], [1, 3], [1, 4]], we have a tree structure where removing an edge splits the tree into two subtrees. The number of unique paths through an edge equals the product of nodes in each subtree. 0 ...
Read MoreProgram to count number of points that lie on a line in Python
Given a list of coordinates representing points on a Cartesian plane, we need to find the maximum number of points that lie on a single line. This problem requires calculating slopes between points and grouping them accordingly. So, if the input is like coordinates = [[6, 2], [8, 3], [10, 4], [1, 1], [2, 2], [6, 6], [7, 7]], then the output will be 4, as the points [1, 1], [2, 2], [6, 6], [7, 7] lie on the same line with slope 1. Algorithm To solve this problem, we follow these steps ? Initialize ...
Read MoreProgram to find minimum cost to pick up gold in given two locations in Python
Suppose we have a 2D matrix and some other values like row, col, erow0, ecol0, erow1, and ecol1. If our current position is matrix[row, col] and we want to pick up gold that is at matrix[erow0, ecol0] and matrix[erow1, ecol1]. We can go up, down, left, and right but when we are at a cell (r, c), we have to pay cost matrix[r, c]. However, if we land at a cell more than once, we do not need to pay cost for that cell again. We have to find the minimum cost to pick up gold at both locations. ...
Read MoreProgram to find size of each partition of a list where each letter appears at most one piece in Python
Suppose we have a lowercase string s, we can partition s into as many pieces as possible such that each letter appears in at most one piece and find the sizes of the partitions as a list. So, if the input is like s = "momoplaykae", then the output will be [4, 1, 1, 4, 1], as the string is split into ["momo", "p", "l", "ayka", "e"]. Algorithm To solve this, we will follow these steps − count := a map that contains characters in s and their occurrences ...
Read MoreProgram to count number of paths with cost k from start to end point in Python
Suppose we have a 2D binary matrix and another value k. Starting from the top-left cell, we need to reach the bottom-right cell. In one step, we can only move down or right. The score of a path is the sum of values in all cells on that path. We have to find the number of paths from start to end with score exactly k. If there are many possible paths, we return the result modulo 10^9 + 7. Example Consider this matrix with k = 2: 0 0 1 1 0 ...
Read MoreProgram to find minimum cost to paint fences with k different colors in Python
Suppose we want to paint a row of N fences with K different colors. We want to minimize the cost while ensuring that no two neighboring fences are of the same color. So if we have an N x K matrix where the nth row and kth column represents the cost to paint the nth fence with kth color, we have to find the minimum cost which achieves this goal. So, if the input is like ? 6 4 5 3 2 7 3 4 5 5 4 4 ...
Read More