HUNAR Jain
ISSUE No. 47 · BAY AREA · VOL. MMXXVI
← back to the writing desk
LEARNING · 2025-11-16 · 2 min · debugging · - views

When Subsets != Substrings: A Costly Confusion

A 30-minute debugging journey caused by confusing subsets (2^n) with substrings (n(n+1)/2) - a costly mental model error that created a 20x performance gap.

by Hunar Jain ~2 min read

The Bug That Bit Me

Today I hit a classic case of conceptual confusion that cost me time and frustration. I was working on a problem requiring all substrings of a string, and I confidently wrote:

num_substrings = (1 << n) - 1  # 2^n - 1

This is wrong. This formula gives the number of subsets, not substrings.

The Correct Approach

For substrings, the formula should be:

num_substrings = (n * (n + 1)) // 2

The Impact

The difference is staggering. For a string of length n = 10:

That’s a 20x difference. My code was creating way more iterations than needed, leading to timeouts and confusing behavior that had me chasing ghosts in my logic.

Understanding the Mental Model Error

Subsets (2^n): Choose any combination of characters from any positions

Substrings (n(n+1)/2): Contiguous sequences only

The Key Lesson

Always pause and ask: “Am I dealing with contiguous elements or arbitrary combinations?”

This simple question would have saved me 30 minutes of debugging why my “optimized” solution was actually slower than brute force. The irony of making code worse while trying to make it better is not lost on me.

Going Forward

When approaching string/array problems, I need to be explicit about:

  1. Can elements be rearranged? → Likely subsets/combinations
  2. Must elements be contiguous? → Substrings/subarrays
  3. Does order matter? → Permutations vs combinations

This isn’t just about formulas. It’s about precision in problem understanding before writing a single line of code.


Takeaway: Slow down on the mental model. Fast code starts with correct thinking.

Interactive Visualization: Subsets vs Substrings

Read next

essay · 2026-10-11 · 4 min

Steering Without a Map

A working model for one person learning in a world that keeps changing while they learn it.

essay · 2026-10-07 · 1 min

When Output Stops Measuring Agency

Text and code are nearly free now, so output no longer works as a proxy for high agency, and the real thing is harder to tell apart from the performance.