Account Merging with Union-Find Data Structure

Problem Description Given a list of accounts where each account is represented as a list of strings, the first element being a name and the remaining elements being email addresses associated with that account. The task is to merge these accounts based on shared email addresses. If two accounts have at least one email in common, they belong to ...

Posted on Wed, 05 Aug 2026 16:32:12 +0000 by arya202

Binary Tree Traversal Algorithms: Preorder, Inorder, Postorder, and Level Order

Binary tree traversal is a fundamental operation in computer science, visiting each node in the tree in a specific order. This article covers four essential traversal methods with both recursive and iterative implementations. Preorder Traversal (Root-Left-Right) Preorder traversal visits the root node first, then the left subtree, followed by t ...

Posted on Wed, 05 Aug 2026 16:31:07 +0000 by centered effect

Popular LeetCode Problems and Solutions

Two Sum Given an array of integers nums and a target value target, find the indices of two numbers that add up to target. Return the indices as a pair. Solution 1: Brute Force class Solution { public int[] twoSum(int[] nums, int target) { for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.lengt ...

Posted on Wed, 05 Aug 2026 16:23:22 +0000 by Banacek

Designing a Linked List (LeetCode 707)

get(index): Get the value of the index-th node in the linked list. If the index is invalid, return -1. addAtHead(val): Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. addAtTail(val): Append a node of value val as the last element of the linked lis ...

Posted on Sun, 02 Aug 2026 16:56:14 +0000 by quikone

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