Learnitweb

Category: Data structures and algorithms

  • Longest Common Substring Coding Pattern

    1. Introduction The Longest Common Substring (LCS) problem is a classic string problem in computer science. It involves finding the longest contiguous substring that appears in both strings. Example: Note: Problem Statement (Typical Coding Problem): Given two strings s1 and s2, find the length of the longest common substring. Optionally, return the substring itself. 2.…

  • Palindromic Subsequence Coding Pattern

    1. Introduction A palindromic subsequence is a sequence of characters in a string that: Example: Typical Problems: This pattern is a classic Dynamic Programming (DP) problem because the solution depends on substrings and overlapping subproblems. 2. When to Use Palindromic Subsequence Pattern Use this pattern when: Problem Indicators: 3. Core Idea For a string s[i..j],…

  • Fibonacci Numbers Coding pattern

    1. Introduction The Fibonacci sequence is a classic sequence where each number is the sum of the two preceding ones: Example sequence: Problem Statement (Basic Coding Problem): Given an integer n, compute F(n) – the n-th Fibonacci number. Fibonacci problems are fundamental because they teach recursion, dynamic programming, and optimization. 2. When to Use Fibonacci…

  • 0/1 Knapsack Coding Pattern

    1. Introduction The 0/1 Knapsack problem is a classic Dynamic Programming (DP) problem used to teach decision-based optimization. Problem Statement: Given N items, each with a weight w[i] and a value v[i], and a knapsack with capacity W, select a subset of items to maximize the total value without exceeding the capacity.Each item can be…

  • K-Way Merge Coding Pattern

    1. Introduction The K-Way Merge pattern is a common coding technique used to merge K sorted arrays or lists into a single sorted array efficiently. Problem Statement: Given K sorted arrays, merge them into one sorted array. This pattern is widely used in: Example: 2. When to Use K-Way Merge Pattern Use this pattern when:…

  • Top K Elements Coding Pattern

    1. Introduction The Top K Elements pattern is a common coding pattern used to find the largest or smallest K elements from a dataset, array, or stream. This pattern is widely used in: Problem Statement: Given an array or dataset, find the K largest or K smallest elements efficiently. 2. When to Use the Top…

  • Bitwise XOR Coding Pattern

    1. Introduction The Bitwise XOR coding pattern is a frequently used technique in algorithmic problems where bit manipulation provides efficient solutions. XOR Truth Table A B A ^ B 0 0 0 0 1 1 1 0 1 1 1 0 2. Important Properties of XOR These properties make XOR extremely useful in arrays, streams,…

  • Subsets Coding Pattern

    1. Introduction The Subsets pattern is a fundamental backtracking/DFS-based pattern used to generate all possible subsets (also called the power set) of a given set or array. Definition: A subset is any selection of elements from the original set, including the empty set and the set itself. For example: This pattern is widely used in…

  • Two Heaps Coding Pattern

    1. Introduction The Two Heaps pattern is a powerful approach used for solving problems that require efficient retrieval of extreme elements (like the minimum or maximum) in a dynamic dataset, often in real-time. The idea is to use two heaps (priority queues): By carefully balancing the two heaps, we can efficiently solve problems like: 2.…

  • In-Place Reversal of a Linked List

    1. Introduction Reversing a linked list in-place is a core data structure operation and a fundamental coding pattern.The goal is simple: Given a linked list, reverse the order of nodes without using extra memory. For example: Original list: After in-place reversal: This operation is widely used in real-world applications and algorithmic problems: The key insight…