The terms substring, subsequence, and subset, all refer to different ways of selecting elements from a larger string or set.
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
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 codeint 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;}
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
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 stringsvoid find_subSequences(string & str, string result, int i){// base caseif (i == str.length()){if(result != "")cout << result << endl;return;}find_subSequences(str, result + str[i], i+1);find_subSequences(str, result, i+1);}// Driver codeint 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;}
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
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 resultsvoid 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 caseif (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 codeint 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;}
Let's summarize all the above concepts in a comparison table:
Free Resources