Skip to main content

Substrings / Subsequences

Learn Substrings / Subsequences (Strings, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Substring vs Subsequence These two terms get confused constantly, and interviewers test the distinction directly: Substring - a CONTIGUOUS block of characters. "abc" has substrings "a", "ab", "abc", "b", "bc", "c" (n(n+1)/2 total). Subsequence - characters in the SAME relative order, but NOT necessarily contiguous (can skip characters). "abc" has subsequences "a", "b", "c", "ab", "ac", "bc", "abc", "" (2ⁿ total). "ac" is a subsequence of "abc" (skip the b) but NOT a substring (not contiguous).

More lessons in Strings