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
Programming Articles
Page 125 of 2547
Program to enclose pattern into bold tag in Python?
When working with text processing, you often need to highlight specific patterns by wrapping them in HTML tags. This problem involves finding all occurrences of given patterns in a text and enclosing them in tags, while merging overlapping or adjacent patterns. Problem Understanding Given a text string and a list of patterns, we need to: Find all substrings that match any pattern Wrap matching substrings in and tags Merge overlapping or adjacent bold regions Algorithm Steps The solution uses a boolean array to track which characters should be bold: ...
Read MoreProgram to find number of distinct coin sums we can make with coins and quantities in Python?
Suppose we have a list of values called coins and another list called quantities of the same length. The value of ith coin is coins[i] and we currently have quantities[i] number of ith coin. We have to find number of distinct coin sum values we can get by using non-empty group of these coins. So, if the input is like coins = [1, 2, 5] and quantities = [1, 2, 1], then the output will be 10, as we can have the following distinct coin sums: [1] = 1, [2] = 2, [1, 2] = 3, [2, 2] = ...
Read MoreProgram to find a pair (i, j) where nums[i] + nums[j] + (i -j) is maximized in Python?
Given a list of numbers, we need to find a pair (i, j) where i < j such that nums[i] + nums[j] + (i - j) is maximized. Since i < j, the term (i - j) will always be negative, so we're essentially looking for nums[i] + nums[j] - (j - i). For example, if nums = [6, 6, 2, 2, 2, 8], selecting indices i=0 and j=1 (both values are 6) gives us: 6 + 6 + (0 - 1) = 11. Algorithm Approach The key insight is to rearrange the expression nums[i] + nums[j] ...
Read MoreProgram to find length of longest diminishing word chain in Python?
Suppose we have a list of valid words and a string s. We need to find the length of the longest chain of diminishing words that can be generated by starting at s and removing single letters while keeping valid words. A diminishing word chain is formed by repeatedly removing one character from a word to create another valid word from the given list. Example If the input is words = ["lii", "limit", "limi", "li", "coffee", "jug", "pool", "type"] and s = "limit", the output will be 4. We can form the chain: "limit" → "limi" → ...
Read MoreProgram to print maximum number of characters by copy pasting in n steps in Python?
Suppose we have a number n; we have to find the maximum number of characters we can enter using n operations where each operation is like: Inserting the character "x". Copy all characters. Paste So, if the input is like n = 12, then the output will be 81. Understanding the Problem This is a dynamic programming problem where we need to find the optimal strategy to maximize characters. The key insight is that after typing some characters, we should copy-paste to multiply them efficiently. The pattern involves: Type characters individually when ...
Read MoreProgram to count minimum number of operations required to make numbers non coprime in Python?
Two numbers are coprime if their greatest common divisor (GCD) is 1. This program finds the minimum number of increment/decrement operations needed to make two numbers non-coprime (GCD > 1). Problem Understanding Given two numbers A and B, we can perform operations to increment or decrement either number by 1. The goal is to find the minimum operations required so that GCD(A, B) ≠ 1. Example If A = 8 and B = 9, we can increment B to 10. Then GCD(8, 10) = 2, which is not 1, so they become non-coprime in just 1 ...
Read MoreProgram to find next state of next cell matrix state in Python?
This problem implements Conway's Game of Life, where we need to find the next state of a 2D binary matrix. A cell's neighbors are its immediate horizontal, vertical and diagonal cells (8 neighbors total). The rules are: Any living cell with two or three living neighbors survives Any dead cell with exactly three living neighbors becomes alive All other cells die or remain dead Example Input and Output Given this input matrix − ...
Read MoreProgram to remove all nodes with only one child from a binary tree in Python?
Suppose we have a binary tree root; we have to remove all nodes with only one child. A node with only one child is a node that has either a left child or a right child, but not both. So, if the input is like ? 1 2 3 4 5 ...
Read MoreProgram to find length of longest possible stick in Python?
Suppose we have a list of integers representing sticks. Each element in the list represents a stick with two ends, where values are between 1 and 6. We can connect two sticks together if any of their ends are the same. The resulting stick's ends will be the leftover ends and its length is increased. We have to find the length of the longest stick possible. So, if the input is like sticks = [[2, 3], [2, 4], [3, 5], [6, 6]], then the output will be 3. We can connect [2, 3] and [2, 4] to get [3, ...
Read MoreProgram to find a matrix for each condominium's height is increased to the maximum possible height in Python?
Suppose we have a 2D matrix where matrix[r][c] represents the height of a condominium in a city. The west-east skyline is visible by taking the maximum of each row in the matrix, and the north-south skyline can be visible by taking the maximum of each column. We have to find a new matrix where each condominium's height is increased to the maximum possible height while keeping the same west-east and north-south skyline. Understanding the Problem Given a matrix like this: 2 3 4 5 6 7 8 9 10 ...
Read More