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-05-21 · 2 min

on power and the only invariants that overrule it

power gives you leverage to Exploit, Shun Noise, Control the narrative, Extra Perks like Money. but Death and class-agnostic suffering are the only invariants no one can keep their power through.

essay · 2025-12-26 · 1 min

The Seller's Disguise: How Open Platforms Blur Intent

Open platforms reward engagement, not intent. The result? Sellers disguised as curious contributors thrive, while authentic voices struggle to compete. We've built marketplaces that look like forums.