2dbi

Generate All Substrings of a String

viaLeetCode

Problem Generate all contiguous substrings of a string — n(n+1)/2 of them.

Input / Output

  • Input: string s. Output: all substrings (list them; clarify if duplicates should collapse).

Constraints

  • |s| <= ~100 (output is O(n^2) strings totaling O(n^3) chars — state this before coding).

Example

  • "abc" → a, ab, abc, b, bc, c.

Expected approach

  • Double loop over (start, end) pairs emitting s.substring(start, end) — O(n^2) substrings, O(n^3) total character work (each substring copy is O(n); note the copy cost, and StringBuilder-extension per start as the micro-optimization). Dedupe via a set when required. This is usually a warm-up probing loop-boundary precision and complexity articulation — get the n(n+1)/2 count and the substring-vs-subsequence distinction (2^n) crisp.
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account