Posts

Showing posts from November, 2026

Length of Last Word in Java (LeetCode 58): Strings

Image
At first glance, LeetCode 58 (Length of Last Word) looks like it needs a full split() call, an array of tokens, and some bounds-checking. It doesn't. Because you only care about the word at the very end of the string, you can walk backward from the last index and stop the moment you hit a space after counting letters. No splitting, no extra array, just one backward pass. In this post I'll break down why scanning from the back works, dry-run it against a few tricky inputs (trailing spaces included), and then share clean Java for it. There's also a small interactive walker below so you can step through the backward scan character by character. Video Walkthrough Prefer to watch it instead? Here's the full walkthrough, from reading the problem to submitting the solution: 1. What the Problem Asks You're given a string s made of letters and spaces. A "word" is a maximal run of non-space characters. The task is to return the ...

Check if the Sentence Is Pangram in Java (Leetcode 1832): Strings

Image
When you first see LeetCode 1832 (Check if the Sentence Is Pangram) , your first thought might be to throw a HashSet at it or reach for a map. That works, but it's overkill. Because the English alphabet is locked at exactly 26 lowercase characters, we can solve this using a fixed 26-slot boolean checklist and simple ASCII subtraction. In this post, I will walk you through how this mapping works under the hood, how to dry run it across test cases, and how to write the code in clean Java. I have also built an interactive letter-tracker widget right below so you can watch each letter flip from false to true as you step through a sentence.

Min Cost Climbing Stairs in Java (Leetcode 746): DP Approach

Image
When I first moved from the classic Climbing Stairs to Min Cost Climbing Stairs , I overcomplicated it. I started wondering if I should greedily pick whichever step was cheaper right in front of me. That falls apart almost immediately. A cheap step now can easily strand you in front of a massive toll right after. The turning point for me was framing the problem backwards: "To stand at step i , what was the cheapest possible route that could have brought me here?" Once you treat each stair as a landing pad with only two ways to reach it, the Dynamic Programming recurrence practically writes itself. In this guide, we will break down the DP intuition visually, interact with a live step-by-step simulator, inspect the recurrence table by hand, and write clean Java solutions in both O(n) and O(1) space.

Maximum Subarray Sum in Java (Leetcode 53): DP Approach

Image
I had already solved Maximum Subarray on LeetCode once before, but going back to it with Dynamic Programming makes the logic much easier to trust when you are under interview pressure. Instead of memorizing a trick, you build a small rule at every index and let the array do the work. Below, I walk through the DP approach step by step—first with a live simulator so you can watch dp[i] update at every element, then with the full Java code using a dp array, and finally the two-variable version that saves memory.

House Robber in Java (Leetcode 198): DP Approach

Image
In this post we will go from the problem to the recurrence, watch it run in the simulator below, and then write the Java solution twice: first with a DP array (the easiest to understand), then with just two variables (the version you would write in an interview).

Unique Paths in Java (LeetCode 62): DP Approach

Image
Dynamic Programming (DP) on 2D grids is one of the most common problem patterns in technical coding interviews. The classic problem that introduces this concept is LeetCode 62: Unique Paths . In this guide, we will break down the problem intuitively, visualize how overlapping subproblems lead to the dynamic programming transition, build an optimized solution, and provide a full Java implementation. 1. Understanding the Problem There is a robot on an m x n grid. The robot is initially positioned at the top-left corner ( grid[0][0] ). The robot tries to move to the bottom-right corner ( grid[m - 1][n - 1] ). Movement Rules: At each step, the robot can only move either down or right . The robot cannot move diagonally or backward. Goal: Return the total number of unique paths the robot can take to reach the destination. Examples Input: m = 3, n = 7 Output: 28 Input: m = 3, n = 2 Output: ...

Longest Valid Parentheses in Java (LeetCode 32): Stack Approach

Image
Parentheses matching is a cornerstone category of coding interview problems. While basic problems like Valid Parentheses (LeetCode 20) only ask whether an entire sequence is balanced, LeetCode 32: Longest Valid Parentheses steps up the difficulty to Hard by asking for the length of the longest contiguous valid substring. Let's break down the intuition behind tracking boundaries, explore why pushing -1 to the stack works so elegantly, and walk through a clean Java implementation. 1. Problem Statement You are given a string s containing only the characters '(' and ')' . Your goal is to find the length of the longest valid (well-formed) parentheses substring . Key Rules: A substring must be contiguous (no skipping characters). Every open bracket '(' must have a corresponding closing bracket ')' in the correct order. Examples Example 1: Input: s = "(()" Outp...

Koko Eating Bananas in Java (LeetCode 875): Binary Search

Image
When preparing for technical coding interviews, binary search problems frequently appear. While traditional binary search is performed on sorted arrays to locate an element, a very popular variation asks us to search over a range of possible answers. LeetCode 875: Koko Eating Bananas is the classic textbook problem for this technique. Let’s break it down intuitively from problem understanding to a complete Java solution. Prefer Watching? Here is the Video Explanation 1. Understanding the Problem Koko loves eating bananas. There are n piles of bananas, where the i -th pile has piles[i] bananas. The guards will return in h hours. Rules Koko must follow: Koko decides her eating speed: k bananas per hour . Each hour, she picks one pile and eats k bananas from it. If the pile has fewer than k bananas, she eats all of them and does not eat any more bananas during that hour (she rests). Koko w...

Climbing Stairs in Java (LeetCode 70): DP Approach

Image
The Climbing Stairs problem (LeetCode 70) is one of the classic introductory problems for mastering Dynamic Programming (DP) . The premise is simple: you are climbing a staircase with n steps, and you can take either 1 step or 2 steps at a time. The goal is to determine the total number of distinct ways to reach the top. In this guide, we'll break down the intuition, trace the pattern, build both the array-based DP solution and an optimal $O(1)$ space solution in Java.