Subarray, Substring, Subsequence, and Subset

Foundation12 min

Subarray

In [2, 5, 1, 4], a subarray selects one uninterrupted range of positions. Choosing the first and last selected index fixes every value between them.

Does the selection occupy one uninterrupted range?

1 / 2
Step 1 of 2.
[5, 1, 4] is a subarray because it uses every position from index 1 through index 3.
  • valid selection
  • rule is broken

A subarray is fixed by one left boundary and one right boundary. Sliding windows move those boundaries, while prefix sums answer questions about the range between them.

Substring

A substring applies the same uninterrupted-range rule to a string. In "code", the chosen characters must occupy consecutive indices.

Are the chosen characters consecutive?

1 / 2
Step 1 of 2.
"od" is a substring of "code" because the characters occupy consecutive positions.
  • valid selection
  • rule is broken

The data type changes from an array to a string, but the candidate shape does not. A substring is a contiguous slice.

Subsequence

A subsequence may skip positions. The selected values must still appear in the same relative order as the source.

Do selected values keep their source order?

1 / 2
Step 1 of 2.
[2, 1, 4] is a subsequence. It skips 5 while keeping the selected values in source order.
  • valid selection
  • rule is broken

Skipping positions is allowed, but moving backward is not. Once a value is selected, every later selected value must come from a later source position.

Subset

A subset asks which input elements are chosen, without treating their written order as part of the answer. The chosen positions do not need to be adjacent, and changing the order in which the same elements are written does not create a different subset.

For example, choosing 2 and 1 from [2, 5, 1, 4] gives one subset containing those two values. Writing it as [2, 1] or [1, 2] does not describe two different choices.

Which elements are chosen?

1 / 2
Step 1 of 2.
Choosing the values 2 and 1 forms the subset {2, 1}. Their positions do not need to be adjacent.
  • valid selection
  • rule is broken

Keeping values in input order gives each subset a consistent representation, but that order is not part of the subset’s identity.

A common generation method decides for each input position whether to include it or skip it.

If the input contains equal values, different input positions may produce identical value lists. The problem statement determines whether those duplicate results must be removed.

StructureMust positions be contiguous?May positions be skipped?Does selection order affect identity?
SubarrayYesNoYes
SubstringYesNoYes
SubsequenceNoYesYes
SubsetNoYesNo
Why is [2, 1, 4] a subsequence of [2, 5, 1, 4] but not a subarray?