Backtracking Algorithms for Combination Problems in LeetCode

Overview of Backtracking Backtracking is a systematic way to explore all potential solutions by building combinations incrementally and backtracking when a path fails to meet constraints. It's particularly useful for problems like combinations, permutations, subsets, and other combinatorial searches. Common problem types solved with backtrackin ...

Posted on Sat, 01 Aug 2026 17:00:22 +0000 by saras

Merging Two Sorted Linked Lists

Merge two ascending sorted linked lists into a new sorted linked list. The new list is constructed by splicing together all nodes from the two input linked lists. Example 1: Input: l1 = [1,2,4], l2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2: Input: l1 = [], l2 = [0] Output: [0] class Solution: def mergeTwoLists(self, list1: ListNode, list ...

Posted on Mon, 27 Jul 2026 16:31:20 +0000 by ChrisMartino

Reversing Linked Lists Using Stack-Based Approach

LeetCode 92. Reverse Linked List II Problem Statement Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes from position left to position right and return the modified list. Solution Strategy A stack provides an elegant mechanism to reverse elements in-place without complex pointer man ...

Posted on Sun, 26 Jul 2026 16:07:07 +0000 by bakigkgz

LeetCode 2923. Find Champion I [Array Matrix]

Problem Description: There are n teams numbered from 0 to n - 1 in a tournament. You are given a 0-indexed 2D boolean matrix grid of size n * n. For all i, j such that 0 <= i, j <= n - 1 and i != j, if grid[i][j] == 1, then team i is stronger than team j; otherwise, team j is stronger than team i. If there is no team that is stronger than ...

Posted on Wed, 22 Jul 2026 17:05:46 +0000 by haku87

Mastering KMP for String Matching: Implementing strStr and Detecting Repeated Substrings

Implementing strStr() with the KMP Algorithm Given a haystack string and a needle string, locate the index of the first occurrence of the needle. The Knuth–Morris–Pratt (KMP) algorithm avoids redundant comparisons by precomputing a prefix table (often called the LPS – Longest Proper Prefix which is also Suffix – array). First, construct the LPS ...

Posted on Wed, 22 Jul 2026 16:42:34 +0000 by wizhippo

String Modification by Inserting Spaces at Given Indices

Problem Description Given a string s and an integer array spaces, you need to insert a space before the character at each index specified in the spaces array. The spaces array is sorted in strictly increasing order, and all indices are valid (within the bounds of the string). Return the modified string after inserting the spaces. Examples Examp ...

Posted on Thu, 16 Jul 2026 16:20:28 +0000 by bob1660

LeetCode SQL Problem Solutions (Part 4)

1907. Categorize Salary Counts Table: Accounts +-------------+------+ | Column | Type | +-------------+------+ | account_id | int | | income | int | +-------------+------+ account_id is the primary key. Each row has monthly income of a bank account. Query the number of accounts in each salary category: Low Salary: income < 200 ...

Posted on Wed, 15 Jul 2026 16:44:03 +0000 by Bozebo

Remove Duplicates from Sorted Array

Problem Description Given a non-strictly increasing (sorted with possible duplicates) integer aray nums, remove the duplicates in-place succh that each unique element appears only once. Maintain the relative order of the unique elements and return the number of unique elements in nums. Let k be the count of unique elements. To pass the test cas ...

Posted on Wed, 15 Jul 2026 16:32:25 +0000 by Zallus

Removing Elements from Arrays In-Place: LeetCode Problem 27 Analysis

Problem Understanding The challenge requires removing specific values from an array while meeting these constraints: Use only O(1) additional space and modify the input array in-place Element ordering can be changed Focus only on elements within the new length boundary The solution will be validated using code similar to: int result_length = ...

Posted on Tue, 14 Jul 2026 16:26:28 +0000 by draco2317

Finding Unique Triplets That Sum to Zero Using Two-Pointer Technique

The three-sum problem requires finding all unique triplets in an array that add up to zero. While depth-first search can solve this after sorting, a more efficient approach uses the two-pointer technique on a sorted array. Key Points Use two pointers, not a single pointer—common mistake to avoid Recommended solving time: 20 minutes Problem De ...

Posted on Tue, 14 Jul 2026 16:24:10 +0000 by lozza1978