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
Server Side Programming Articles
Page 560 of 2109
Minimum Add to Make Parentheses Valid in Python
When we have a string of parentheses, we need to find the minimum number of parentheses to add to make it valid. A valid parentheses string follows these rules ? It is the empty string It can be written as XY (X concatenated with Y), where X and Y are valid strings It can be written as (A), where A is a valid string For example, if the string is "()))((", we need to add 4 more parentheses to make it valid. Algorithm We use a stack-based approach to track unmatched parentheses ? ...
Read MoreFind and Replace Pattern in Python
Finding and replacing patterns in Python involves identifying words that match a given character pattern structure. A word matches a pattern if there exists a character mapping where each unique character in the pattern maps consistently to characters in the word. For example, with pattern "abb" and words ["abc", "deq", "mee", "aqq", "dkd", "ccc"], the words "mee" and "aqq" match because they follow the same structure: first character is unique, second and third characters are the same. Algorithm Approach The solution converts each word and the pattern into a normalized format representing the character structure. Characters are ...
Read MoreConstruct Binary Tree from Preorder and Postorder Traversal in Python
Binary tree construction from preorder and postorder traversals is a fundamental algorithm problem. Given two traversal sequences, we can reconstruct the original binary tree using a stack-based approach. Understanding the Problem Given preorder traversal [1, 2, 4, 5, 3, 6, 7] and postorder traversal [4, 5, 2, 6, 7, 3, 1], we need to construct the binary tree. The preorder visits root first, while postorder visits root last. 1 2 3 ...
Read MoreDecoded String at Index in Python
The Decoded String at Index problem involves finding a character at a specific position in a decoded string without actually creating the full decoded string. This approach saves memory when dealing with very long decoded strings. Problem Understanding Given an encoded string, we decode it using these rules ? If the character is a letter, write it to the tape. If the character is a digit, repeat the entire current tape digit − 1 more times. For example, "hello2World3" becomes "hellohelloWorldhellohelloWorldhellohelloWorld". Algorithm Overview Instead of creating the full decoded string, we use ...
Read MoreKth Smallest Element in a Sorted Matrix in Python
Finding the Kth smallest element in a sorted matrix is a classic problem that can be solved efficiently using binary search. In this problem, we have an n x n matrix where each row and column is sorted in increasing order, and we need to find the kth smallest element in the entire matrix. Problem Understanding Given a matrix where rows and columns are sorted in ascending order, we need to find the kth smallest element. For example, in the matrix [[1, 5, 9], [10, 11, 13], [12, 13, 15]], if k=8, the answer is 13 because when ...
Read MoreSurrounded Regions in Python
The Surrounded Regions problem involves capturing all regions of 'O' that are completely surrounded by 'X' on a 2D board. A region is considered surrounded if all 'O' cells are enclosed by 'X' cells and cannot reach the board's border. Problem Understanding Given a 2D board with 'X' and 'O' characters, we need to capture surrounded regions by flipping all 'O' to 'X'. However, 'O' cells connected to the border should remain unchanged ? Input Board Output Board XXXX XOOX XXOX XOXX XXXX XXXX XXXX XOXX ...
Read MoreMinimum Path Sum in Python
The minimum path sum problem involves finding a path from the top-left corner to the bottom-right corner of a matrix that minimizes the sum of all numbers along the path. You can only move down or right at any point. Problem Example Given this matrix: 1 3 1 1 5 1 4 2 1 The optimal path is: 1 → 3 → 1 → 1 → 1, giving a minimum sum of 7. Algorithm Steps The dynamic programming approach works as follows: Fill the ...
Read MoreCalculating Wind Chill Factor(WCF) or Wind Chill Index(WCI) in Python Program
In this tutorial, we will learn how to calculate the Wind Chill Factor (WCF) or Wind Chill Index (WCI) in Python. The wind chill index measures how cold it feels when wind is factored in with the actual air temperature. Wind Chill Formula We use the following standard formula to calculate the Wind Chill Index ? Twc = 13.12 + 0.6215Ta − 11.37v0.16 + 0.3965Tav0.16 Where: Twc = Wind Chill Index (in degrees Celsius) Ta = Air Temperature (in degrees Celsius) v = Wind Speed (in kilometers per hour) Implementation Steps Follow ...
Read MoreLambda expression in Python Program to rearrange positive and negative numbers
In this tutorial, we will write an anonymous function using lambda to rearrange positive and negative numbers in a list. The goal is to place all negative numbers first, followed by all positive numbers. Algorithm Let's see how to solve the problem step by step ? 1. Initialize a list with negative and positive numbers. 2. Write a lambda expression that takes a list as an argument. 2.1. Iterate over the list and get negative numbers 2.2. Same for positive numbers 2.3. Combine both using concatenation operator. ...
Read MorePython Dictionary Comprehension
In this tutorial, we are going to learn how to use dictionary comprehensions in Python. If you are already familiar with list comprehension, then it won't take much time to learn dictionary comprehensions. Dictionary comprehension provides a concise way to create dictionaries using a single line of code. We need key-value pairs to create a dictionary. The general syntax of dictionary comprehension is: {key: value for item in iterable} Basic Dictionary Comprehension Let's see how to generate numbers as keys and their squares as values within the range of 10. Our result should look ...
Read More