Know the Difference: Subarray vs Substring vs Subsequence vs Subset

Today we are going to make a comparison of subarray vs substring vs subsequence vs subset. These are all similar concepts but have important differences. For instance, if you have a requirement to write an algorithm to find the subsets of a certain data and you come up with something that only finds the subsequences, you will only be half done. Or in an interview situation, you need to be extra careful about your choice of wording. If a question asks you to return a subsequence and you return a subset, you might fail the interview. Let me start by describing each concept with examples. Finally, I will give you a comparison table.
If you can read the article version of this video at:
• quanticdev.com/algorithms/primitives/subarray-vs-substring-vs-subsequence-vs-subset
My "Algorithms" Playlist for all other algorithm questions & answers:
• kzread.info/head/PLlPRnMzqjADqDZFBqdwzbIjf71h8xYs4x
- - - - - - - - - - -
quanticdev
quantic_dev
quanticdev.com
- - - - - - - - - - -
Abstract:
Subarray
A subarray is a contiguous sequence of elements within an array. For instance, the subarrays of the array {1, 2, 1} would be {1}, {2}, {1, 2}, {2, 1}, {1, 2, 1}, {}. Things to note:
• You can use braces (aka curly brackets) {} or square brackets [] to denote arrays.
• A subarray should be a contiguous subsequence of the parent array. As a result, {1, 1} is not a valid subarray of the array {1, 2, 1}, since {2} in the middle is skipped, so it is not a contiguous subsequence anymore.
• The full array itself is a subarray of itself.
• More details in the video.
Substring
A substring is exactly the same thing as a subarray but in the context of strings. For instance, the substrings of the string "ara" would be "a", "r", "ar", "ra", "ara", "". Things to note:
• A substring is just a subarray that is made up of only characters.
• You can use single ' or double quotes " to denote substrings.
• More details in the video.
Subsequence
Both in mathematics and computer science, a subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. This means a subsequence is a generalized subarray, where the rule of contiguity does not apply. For instance, the subsequences of the sequence [A, B, A] would be [A], [B], [A, B], [B, A], [A, A], [A, B, A], [].
• In math, it is customary to use angle brackets to denote subsequences. In programming, you can use whatever your programming language uses for arrays and lists.
• Unlike subarrays, subsequences do not need to be contiguous so [A, A] is a perfectly valid subsequence of [A, B, A] whereas it is not a valid subarray.
• More details in the video.
Subset
A set is subset of another set if all its elements are contained by that set. This means, neither contiguity nor ordering of elements matter. For instance, the subsets of the set {1, 2, 3} would be {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}, {}.
• Subsets do not need to be contiguous. {1, 3} is a perfectly valid subset.
• More details in the video.
Comparison
Subarrays and substrings need to be made up of contiguous sequence of elements of their parents, while subsequences and subsets do not have to be. In addition, all of subarrays, substrings and subsequences should preserve element order, meaning their elements should appear in the same order that they appear in their parents, while subsets can have their elements appear in any order.

Пікірлер: 18

  • @QuanticDev
    @QuanticDev4 жыл бұрын

    Toss a sub to your programmer... o valley of silicon... o' o' o'

  • @SR-tg4mw

    @SR-tg4mw

    4 жыл бұрын

    Around 4:30 you mentioned a link to in-depth description of data structures in the description I don't see it. I only see your playlist for algorithms. Could you please provide the video playlist link for data structures. Thank you

  • @QuanticDev

    @QuanticDev

    4 жыл бұрын

    ​@@SR-tg4mw I didn't publish it yet. Don't worry though, I didn't forget it, I will put the link in description when I publish it and let you know. I plan to start publishing broader topic videos after couple more interview question videos.

  • @ankitdhote57

    @ankitdhote57

    2 жыл бұрын

    @@QuanticDev pls publish DS video if possible

  • @shaileshkaiwart7190

    @shaileshkaiwart7190

    Жыл бұрын

    Why are you not making more videos on algorithm?? Your videos are very very very helpful in understanding an algorithm and its variations. Please make more videos. These kind of videos are not present in youtube.

  • @cloudmaxx3140
    @cloudmaxx3140 Жыл бұрын

    Thanks for the simple and clear explanations !

  • @migueldejesustavares4168
    @migueldejesustavares41682 жыл бұрын

    You have a gift for teaching! Thanks for these videos

  • @ShibaniDas-uv3tt
    @ShibaniDas-uv3tt Жыл бұрын

    Hey loved the explanation ! I have a question, for subsequence of Are duplicates allowed here ?, I guess YES. Then the sebquences would be , , , , , , , You have missed an extra in the video.

  • @ganeshjaggineni4097
    @ganeshjaggineni4097Ай бұрын

    NICE SUPER EXCELLENT MOTIVATED

  • @devbhattacharya153
    @devbhattacharya15311 ай бұрын

    Thanks man!

  • @smartannu
    @smartannu2 жыл бұрын

    such a awesome content

  • @genki316
    @genki3163 жыл бұрын

    I'm confused about the rule; you cannot have duplicates in a subarray. that doesn't make sense if the original array is [1,1,2,1] a valid subarray for that would be [1,1,2]

  • @QuanticDev

    @QuanticDev

    3 жыл бұрын

    Rule is "you cannot have duplicate subarray members". [[1], [1, 1, 2]] is a valid subarray list, [[1], [1]] is not. I will clarify the wording there. Thanks for bringing it up.

  • @shaileshkaiwart7190
    @shaileshkaiwart7190 Жыл бұрын

    Why are you not making more videos on algorithm?? Your videos are very very very helpful in understanding an algorithm and its variations. Please make more videos. These kind of videos are not present in youtube.

  • @mriganktandon_5877
    @mriganktandon_58773 жыл бұрын

    really helped

  • @solar679
    @solar6792 жыл бұрын

    Great video

  • @QuanticDev

    @QuanticDev

    Жыл бұрын

    Glad to help.

  • @gamesgotppal
    @gamesgotppal Жыл бұрын

    explain what substring is....... all i heard is substring is just like sub array ..i dont want to understand subarry ....