The Word Search II problem involves finding all words from a dictionary that can be formed in a 2D character board. Each word must be constructed from letters of sequentially adjacent cells (horizontally or vertically neighboring), and the same letter cell cannot be used more than once in a single word. This problem is efficiently solved using a Trie (prefix tree) data structure combined with backtracking. The Trie allows us to prune search paths early when no dictionary word has the current prefix. Algorithm Overview The solution follows these key steps ? Build a Trie ... Read More
Binary tree postorder traversal visits nodes in the order: left subtree, right subtree, then root. This article demonstrates an iterative approach using a stack with node-state pairs to achieve postorder traversal without recursion. -10 9 10 15 7 ... Read More
The Word Break II problem involves breaking a string into valid dictionary words and returning all possible sentence combinations. Given a string and a dictionary of valid words, we need to find all ways to add spaces to form valid sentences. For example, with string "appleraincoat" and dictionary ["app", "apple", "rain", "coat", "raincoat"], we can form: "apple rain coat" and "apple raincoat". Algorithm Approach We use dynamic programming with memoization to avoid redundant calculations ? Create a memoization map to store results for substrings For each position, ... Read More
The Longest Consecutive Sequence problem asks us to find the length of the longest sequence of consecutive integers in an unsorted array. For example, in the array [100, 4, 250, 1, 3, 2], the longest consecutive sequence is [1, 2, 3, 4] with length 4. Algorithm Approach We use a set-based approach for O(n) time complexity ? Convert the array to a set for O(1) lookup operations For each number, check if it's the start of a sequence (i.e., number-1 is not in the set) If it's a sequence start, count consecutive numbers and track the ... Read More
The maximum path sum problem asks us to find the path in a binary tree with the largest sum of node values. A path can start and end at any nodes and must follow parent-child connections. The path doesn't need to pass through the root. -10 9 10 15 7 ... Read More
Wildcard pattern matching allows you to match strings using special characters. In wildcard matching, '?' matches any single character and '*' matches zero or more characters. This is useful for file pattern matching, search operations, and text processing. Wildcard Characters '?' − Matches exactly one character '*' − Matches zero or more characters Dynamic Programming Approach We can solve wildcard matching using dynamic programming. The idea is to build a 2D table where dp[i][j] represents whether the first i characters of the string match the first j characters of the pattern. Algorithm Steps ... Read More
The trapping rain water problem is a classic algorithmic challenge where we calculate how much water can be trapped after raining on an elevation map represented by an array of heights. Each element represents the height of a bar with width 1. ... Read More
The First Missing Positive problem asks us to find the smallest missing positive integer from an unsorted array. For example, given the array [4, -3, 1, -1], the result is 2 since 1 is present but 2 is missing. Algorithm Approach We use a cyclic sort approach to solve this efficiently: Add a 0 at the beginning to handle 1-based indexing Place each positive number at its correct index position Scan the array to find the first missing positive Example Implementation Here's the complete solution using cyclic sort ? class Solution: ... Read More
Finding the longest valid parentheses substring is a common problem that can be solved efficiently using a stack-based approach. Given a string containing only '(' and ')' characters, we need to find the length of the longest valid (well-formed) parentheses substring. For example, in the string "))(())())", the longest valid parentheses substring is "(())())" with length 6. Algorithm Approach We use a stack to track indices of unmatched parentheses ? Initialize a stack with −1 to handle edge cases For each character, if it's '(', push its index onto the stack If it's ')', check ... Read More
Merging k sorted lists is a classic algorithm problem. Given multiple sorted linked lists, we need to combine them into a single sorted list. Python's heapq module provides an efficient solution using a min-heap data structure. Problem Understanding Given k sorted linked lists like [1, 4, 5], [1, 3, 4], [2, 6], we need to merge them into one sorted list [1, 1, 2, 3, 4, 4, 5, 6]. Algorithm Steps Create a min-heap to store the smallest elements from each list Add the first node of each non-empty list to the heap Repeatedly extract ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance