Differentiate between a substring, a subsequence, and a subset

The terms substring, subsequence, and subset, all refer to different ways of selecting elements from a larger string or set.

What is a substring?

A substring is a contiguous sequence of characters that belong to a larger string.

The total possible number of substrings for a string of length nn is given by:

Example

The strings "fruit" and "cake" are substrings for the string "fruitcakes."

// C++ program to print all possible
// substrings of a string
#include<iostream>
using namespace std;
void find_subStrings(string & str, int start, int end){
if (start > end) {
return;
}
for (int i = start; i <= end; i++){
cout << str.substr(start, i - start + 1) << endl;
}
find_subStrings(str, start + 1, end);
}
// Driver code
int main(){
string str = "fruitcakes";
int n = str.length();
cout << "The total number of elements in the string: " << str.length() << endl;
find_subStrings(str,0,n-1);
cout << "The total number of substrings = " << n * (n + 1)/2 << endl;
return 0;
}

What is a subsequence?

A subsequence is a string formed by removing some characters from the original string while maintaining the relative position of the remaining characters. A subsequence can be non-contiguous.

For a string of length nn, the total number of possible subsequences are 2n12^n - 1.

Example

The sequence "take" is a subsequence of the string "fruitcakes."

// C++ program to print all possible
// subsequences of a string
#include<iostream>
#include<math.h>
using namespace std;
// Function to print all sub strings
void find_subSequences(string & str, string result, int i){
// base case
if (i == str.length()){
if(result != "")
cout << result << endl;
return;
}
find_subSequences(str, result + str[i], i+1);
find_subSequences(str, result, i+1);
}
// Driver code
int main(){
string str = "fruitcakes";
string result = "";
cout << "The total number of elements in the string: " << str.length() << endl;
find_subSequences(str, result, 0);
cout << "The total number of subsequences = " << pow(2,str.length()) - 1 << endl;
return 0;
}

What is a subset?

A subset is a set of elements that belong to a larger set. The subset does not have to be in any specific order and can be non-contiguous.

The total number of subsets of a set containing nn elements is given by 2n2^n.

Example

The set {Ruby, Java} is a subset of the set {Java, Python, React, Ruby, Solidity}.

// C++ program to print all possible
// subsets of a set
#include<iostream>
#include <vector>
using namespace std;
// Helper function to print results
void PrintList(vector<string>& subset){
string out = "[";
for (int i = 0; i < subset.size(); i++) out+= subset[i] + ", ";
out = out.substr(0, out.length() -2);
out += "]";
cout << out << endl;
}
void find_subSets(vector<string>& set, vector<vector<string>>& subsets, vector<string>& subset, int index){
// base case
if (index == set.size()){
subsets.push_back(subset);
return;
}
find_subSets(set, subsets, subset, index + 1);
subset.push_back(set[index]);
find_subSets(set, subsets, subset, index + 1);
subset.pop_back();
}
vector<vector<string>> getAllSubsets(vector<string>& set){
vector<vector<string>> subsets;
vector<string> subset;
find_subSets(set, subsets, subset, 0);
return subsets;
}
// Driver code
int main(){
vector<string> set = {"java", "python", "react", "ruby", "solidity"};
cout << "The total number of elements in the set: " << set.size() << endl;
vector<vector<string>> result = getAllSubsets(set);
for (auto it : result){
PrintList(it);
}
cout << "The total number of subsets = " << result.size() << endl;
return 0;
}

Summary

Let's summarize all the above concepts in a comparison table:

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved