Posts

Majority Element in Java(LeetCode 169): Arrays

Image
If you ask someone how to find the most common number in an array, they will almost always say "count everything with a HashMap." That is a fine answer, and it works. But LeetCode 169 (Majority Element) gives you a special guarantee that lets you do much better: the answer appears more than half the time . Because of that one detail, you can solve it with just two variables and a single loop, using no extra memory at all. In this post I'll explain the idea behind it (it's called the Boyer-Moore voting algorithm), trace it by hand on a small array, and then show the Java code I used in my video. There is also a small step-by-step simulator below, so you can type in your own array and watch the candidate and the counter change. Video Walkthrough If you'd rather watch than read, the full explanation is here: 1. What the Problem Asks You get an integer array nums of size n . Return the majority element , which is the value t...

Valid Anagram in Java (LeetCode 242): Arrays

Image
The first instinct most people have for LeetCode 242 (Valid Anagram) is to sort both strings and compare them. That works, but sorting costs you O(n log n) when the problem can actually be solved in a single straight pass through each string. Since both words only use lowercase English letters, a 26-slot counting array does the job in linear time, with no sorting step at all. This post walks through why counting beats sorting here, dry-runs the logic on a couple of examples (including a sneaky one where the lengths differ), and then shares a clean Java implementation. There's also an interactive counter below so you can watch the 26 slots go up and down as each string is scanned. 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 two strings, s and t . The task is to check whether t is an anagram of s — meaning it uses e...

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).