Banana-Leopard/TASTY huggingface dataset:
// CPP Program to find the "GFG" subsequence in
// the given string
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
 
// Print the count of "GFG" subsequence in the string
void countSubsequence(char s[], int n)
{
    int cntG = 0, cntF = 0, result = 0, C=0;
 
    // Traversing the given string
    for (int i = 0; i < n; i++) {
        switch (s[i]) {
 
        // If the character is 'G', increment
        // the count of 'G', increase the result
        // and update the array.
        case 'G':
            cntG++;
            result+=C;
            break;
 
        // If the character is 'F', increment
        // the count of 'F' and update the array.
        case 'F':
            cntF++;
            C+=cntG;
            break;
 
        // Ignore other character.
        default:
            continue;
        }
    }
 
    cout << result << endl;
}
 
// Driven Program
int main()
{
    char s[] = "GFGFG";
    int n = strlen(s);
    countSubsequence(s, n);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to print distinct
// subsequences of a given string
#include <bits/stdc++.h>
using namespace std;
 
// Create an empty set to store the subsequences
unordered_set<string> sn;
 
// Function for generating the subsequences
void subsequences(char s[], char op[], int i, int j)
{
 
    // Base Case
    if (s[i] == '\0') {
        op[j] = '\0';
 
        // Insert each generated
        // subsequence into the set
        sn.insert(op);
        return;
    }
 
    // Recursive Case
    else {
        // When a particular character is taken
        op[j] = s[i];
        subsequences(s, op, i + 1, j + 1);
 
        // When a particular character isn't taken
        subsequences(s, op, i + 1, j);
        return;
    }
}
 
// Driver Code
int main()
{
    char str[] = "ggg";
    int m = sizeof(str) / sizeof(char);
    int n = pow(2, m) + 1;
 
    // Output array for storing
    // the generating subsequences
    // in each call
    char op[m+1]; //extra one for having \0 at the end
 
    // Function Call
    subsequences(str, op, 0, 0);
 
    // Output will be the number
    // of elements in the set
    cout << sn.size();
    sn.clear();
    return 0;
 
    // This code is contributed by Kishan Mishra
}

Time complexity: np
Space complexity: linear
// C++ program to count number of distinct
// subsequences of a given string.
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 256;
 
// Returns count of distinct subsequences of str.
int countSub(string str)
{
    // Create an array to store index
    // of last
    vector<int> last(MAX_CHAR, -1);
 
    // Length of input string
    int n = str.length();
 
    // dp[i] is going to store count of distinct
    // subsequences of length i.
    int dp[n + 1];
 
    // Empty substring has only one subsequence
    dp[0] = 1;
 
    // Traverse through all lengths from 1 to n.
    for (int i = 1; i <= n; i++) {
        // Number of subsequences with substring
        // str[0..i-1]
        dp[i] = 2 * dp[i - 1];
 
        // If current character has appeared
        // before, then remove all subsequences
        // ending with previous occurrence.
        if (last[str[i - 1]] != -1)
            dp[i] = dp[i] - dp[last[str[i - 1]]];
 
        // Mark occurrence of current character
        last[str[i - 1]] = (i - 1);
    }
 
    return dp[n];
}
 
// Driver code
int main()
{
    cout << countSub("gfg");
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
 
// Returns count of distinct
// subsequences of str.
int countSub(string s)
{
    map<char, int> Map;
 
    // Iterate from 0 to s.length()
    for(int i = 0; i < s.length(); i++)
    {
        Map[s[i]] = -1;
    }
      
    int allCount = 0;
    int levelCount = 0;
      
    // Iterate from 0 to s.length()
    for(int i = 0; i < s.length(); i++)
    {
        char c = s[i];
         
        // Check if i equal to 0
        if (i == 0)
        {
            allCount = 1;
            Map = 1;
            levelCount = 1;
            continue;
        }
         
        // Replace levelCount with
        // allCount + 1
        levelCount = allCount + 1;
         
        // If map is less than 0
        if (Map < 0)
        {
            allCount = allCount + levelCount;
        }
        else
        {
            allCount = allCount +
                     levelCount - Map;
        }
        Map = levelCount;
    }
     
    // Return answer
    return allCount;
}
 
// Driver code
int main()
{
    string list[] = { "abab", "gfg" };
      
    for(string s : list)
    {
        int cnt = countSub(s);
        int withEmptyString = cnt + 1;
         
        cout << "With empty string count for "
             << s << " is " << withEmptyString
             << endl;
        cout << "Without empty string count for "
             << s << " is " << cnt << endl;
    }
    return 0;
}
 
// This code is contributed by divyeshrabadiya07

Time complexity: linear
Space complexity: constant
// C++ program to find LCS with permutations allowed
#include<bits/stdc++.h>
using namespace std;
 
// Function to calculate longest string
// str1     --> first string
// str2     --> second string
// count1[] --> hash array to calculate frequency
//             of characters in str1
// count[2] --> hash array to calculate frequency
//             of characters in str2
// result --> resultant longest string whose
// permutations are sub-sequence of given two strings
void longestString(string str1, string str2)
{
    int count1[26] = {0}, count2[26]= {0};
 
    // calculate frequency of characters
    for (int i=0; i<str1.length(); i++)
        count1[str1[i]-'a']++;
    for (int i=0; i<str2.length(); i++)
        count2[str2[i]-'a']++;
 
    // Now traverse hash array
    string result;
    for (int i=0; i<26; i++)
 
        // append character ('a'+i) in resultant
        // string 'result' by min(count1[i],count2i])
        // times
        for (int j=1; j<=min(count1[i],count2[i]); j++)
            result.push_back('a' + i);
 
    cout << result;
}
 
// Driver program to run the case
int main()
{
    string str1 = "geeks", str2 = "cake";
    longestString(str1, str2);
    return 0;
}

Time complexity: linear
Space complexity: constant
// A dynamic programming based C++ program
// to find shortest uncommon subsequence.
#include<bits/stdc++.h>
using namespace std;
 
#define MAX 1005
 
// Returns length of shortest common subsequence
int shortestSeq(char *S, char *T)
{
    int m = strlen(S), n = strlen(T);
 
    // declaring 2D array of m + 1 rows and
    // n + 1 columns dynamically
    int dp[m+1][n+1];
 
    // T string is empty
    for (int i = 0; i <= m; i++)
        dp[i][0] = 1;
 
    // S string is empty
    for (int i = 0; i <= n; i++)
        dp[0][i] = MAX;
 
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            char ch = S[i-1];
            int k;
            for (k = j-1; k >= 0; k--)
                if (T[k] == ch)
                    break;
 
            // char not present in T
            if (k == -1)
                dp[i][j] = 1;
            else
               dp[i][j] = min(dp[i-1][j], dp[i-1][k] + 1);
        }
    }
 
    int ans = dp[m][n];
    if (ans >= MAX)
        ans = -1;
 
    return ans;
}
 
// Driver program to test the above function
int main()
{
    char S[] = "babab";
    char T[] = "babba";
    int m = strlen(S), n = strlen(T);
    cout << "Length of shortest subsequence is : "
         << shortestSeq(S, T) << endl;
}

Time complexity: cubic
Space complexity: quadratic
// C++ program to sort a string according to the
// order defined by a pattern string
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
 
// Sort str according to the order defined by pattern.
void sortByPattern(string& str, string pat)
{
    // Create a count array store count of characters in str.
    int count[MAX_CHAR] = { 0 };
 
    // Count number of occurrences of each character
    // in str.
    for (int i = 0; i < str.length(); i++)
        count[str[i] - 'a']++;
 
    // Traverse the pattern and print every characters
    // same number of times as it appears in str. This
    // loop takes O(m + n) time where m is length of
    // pattern and n is length of str.
    int index = 0;
    for (int i = 0; i < pat.length(); i++)
        for (int j = 0; j < count[pat[i] - 'a']; j++)
            str[index++] = pat[i];
}
 
// Driver code
int main()
{
    string pat = "bca";
    string str = "abc";
    sortByPattern(str, pat);
    cout << str;
    return 0;
}

Time complexity: linear
Space complexity: constant
#include <bits/stdc++.h>
using namespace std;
 
// Declaring a vector globally that stores which character
// is occurring first
vector<int> position(26, -1);
 
//Comparator function
bool cmp(char& char1, char& char2)
{
    return position[char1 - 'a'] < position[char2 - 'a'];
}
 
int main()
{
 
    // Pattern
    string pat = "wcyuogmlrdfphitxjakqvzbnes";
 
    for (int i = 0; i < pat.length(); i++) {
        if (position[pat[i] - 'a'] == -1)
            position[pat[i] - 'a'] = i;
    }
 
    // String to be sorted
    string str = "jcdokai";
 
    // Passing a comparator to sort function
    sort(str.begin(), str.end(), cmp);
    cout << str;
}

Time complexity: nlogn
Space complexity: constant
// C++ program to find largest word in Dictionary
// by deleting some characters of given string
#include <bits/stdc++.h>
using namespace std;
 
// Returns true if str1[] is a subsequence of str2[].
// m is length of str1 and n is length of str2
bool isSubSequence(string str1, string str2)
{
    int m = str1.length(), n = str2.length();
 
    int j = 0; // For index of str1 (or subsequence
 
    // Traverse str2 and str1, and compare current
    // character of str2 with first unmatched char
    // of str1, if matched then move ahead in str1
    for (int i = 0; i < n && j < m; i++)
        if (str1[j] == str2[i])
            j++;
 
    // If all characters of str1 were found in str2
    return (j == m);
}
 
// Returns the longest string in dictionary which is a
// subsequence of str.
string findLongestString(vector<string> dict, string str)
{
    string result = "";
    int length = 0;
 
    // Traverse through all words of dictionary
    for (string word : dict) {
        // If current word is subsequence of str and is
        // largest such word so far.
        if (length < word.length()
            && isSubSequence(word, str)) {
            result = word;
            length = word.length();
        }
    }
 
    // Return longest string
    return result;
}
 
// Driver program to test above function
int main()
{
    vector<string> dict
        = { "ale", "apple", "monkey", "plea" };
    string str = "abpcplea";
    cout << findLongestString(dict, str) << endl;
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to check if a string is perfect
// reversible or nor
#include<bits/stdc++.h>
using namespace std;
 
// This function basically checks if string is
// palindrome or not
bool isReversible(string str)
{
     int i = 0, j = str.length()-1;
 
     // iterate from left and right
     while (i < j)
     {
        if (str[i] != str[j])
            return false;
        i++;
        j--;
     }
     return true;
}
 
// Driver program to run the case
int main()
{
  string str="aba";
  if (isReversible(str))
      cout << "YES";
  else
      cout << "NO";
  return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to reverse an equation
#include <bits/stdc++.h>
using namespace std;
 
// Function to reverse order of words
string reverseEquation(string s)
{
    // Resultant string
    string result;
    int j = 0;
    for (int i = 0; i < s.length(); i++) {
         
        // A space marks the end of the word
        if (s[i] == '+' || s[i] == '-' ||
            s[i] == '/' || s[i] == '*') {
             
            // insert the word at the beginning
            // of the result string
            result.insert(result.begin(),
                s.begin() + j, s.begin() + i);
            j = i + 1;
             
            // insert the symbol
            result.insert(result.begin(), s[i]);
        }
    }
     
    // insert the last word in the string
    // to the result string
    result.insert(result.begin(), s.begin() + j,
                                     s.end());
    return result;
}
 
// driver code
int main()
{
    string s = "a+b*c-d/e";
    cout << reverseEquation(s) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// C program for Left Rotation and Right
// Rotation of a String
#include<bits/stdc++.h>
using namespace std;
 
// In-place rotates s towards left by d
void leftrotate(string &s, int d)
{
    reverse(s.begin(), s.begin()+d);
    reverse(s.begin()+d, s.end());
    reverse(s.begin(), s.end());
}
 
// In-place rotates s towards right by d
void rightrotate(string &s, int d)
{
   leftrotate(s, s.length()-d);
}
 
// Driver code
int main()
{
    string str1 = "GeeksforGeeks";
    leftrotate(str1, 2);
    cout << str1 << endl;
 
    string str2 = "GeeksforGeeks";
    rightrotate(str2, 2);
    cout << str2 << endl;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program for Left Rotation and Right
// Rotation of a String
#include <bits/stdc++.h>
using namespace std;
 
// Rotating the string using extended string
string leftrotate(string str1, int n)
{
 
    // creating extended string and index for new rotated
    // string
    string temp = str1 + str1;
    int l1 = str1.size();
 
    string Lfirst = temp.substr(n, l1);
 
    //      now returning  string
    return Lfirst;
}
// Rotating the string using extended string
string rightrotate(string str1, int n)
{
 
    return leftrotate(str1, str1.size() - n);
}
 
// Driver code
int main()
{
    string str1 = leftrotate("GeeksforGeeks", 2);
    cout << str1 << endl;
 
    string str2 = rightrotate("GeeksforGeeks", 2);
    cout << str2 << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// A simple C++ program to generate all rotations
// of a given string
#include<bits/stdc++.h>
using namespace std;
 
// Print all the rotated string.
void printRotatedString(char str[])
{
    int len = strlen(str);
 
    // Generate all rotations one by one and print
    char temp[len];
    for (int i = 0; i < len; i++)
    {
        int j = i;  // Current index in str
        int k = 0;  // Current index in temp
 
        // Copying the second part from the point
        // of rotation.
        while (str[j] != '\0')
        {
            temp[k] = str[j];
            k++;
            j++;
        }
 
        // Copying the first part from the point
        // of rotation.
        j = 0;
        while (j < i)
        {
            temp[k] = str[j];
            j++;
            k++;
        }
 
        printf("%s\n", temp);
    }
}
 
// Driven Program
int main()
{
    char str[] = "geeks";
    printRotatedString(str);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// An efficient C++ program to print all
// rotations of a string.
#include<bits/stdc++.h>
using namespace std;
 
// Print all the rotated string.
void printRotatedString(char str[])
{
    int n = strlen(str);
 
    // Concatenate str with itself
    char temp[2*n + 1];
    strcpy(temp, str);
    strcat(temp, str);
 
    // Print all substrings of size n.
    // Note that size of temp is 2n
    for (int i = 0; i < n; i++)
    {
        for (int j=0; j != n; j++)
            printf("%c",temp[i + j]);
        printf("\n");
    }
}
 
// Driven Program
int main()
{
    char str[] = "geeks";
    printRotatedString(str);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to determine minimum number
// of rotations required to yield same
// string.
#include <iostream>
using namespace std;
 
// Returns count of rotations to get the
// same string back.
int findRotations(string str)
{
    // tmp is the concatenated string.
    string tmp = str + str;
    int n = str.length();
 
    for (int i = 1; i <= n; i++) {
 
        // substring from i index of original
        // string size.
        string substring = tmp.substr(i, str.size());
 
        // if substring matches with original string
        // then we will come out of the loop.
        if (str == substring)
            return i;
    }
    return n;
}
 
// Driver code
int main()
{
    string str = "abc";
    cout << findRotations(str) << endl;
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to determine minimum number
// of rotations required to yield same
// string.
#include <iostream>
using namespace std;
 
// Returns count of rotations to get the
// same string back.
int findRotations(string str)
{
    int ans = 0; //to store the answer
      int n = str.length(); //length of the string
       
      //All the length where we can partition
      for(int i=1;i<n-1;i++)
    {
          //right part + left part = rotated string
          // we are checking whether the rotated string is equal to
        //original string
          if(str.substr(i,n-i) + str.substr(0,i)  == str)
        {
             ans = i;
              break;
        }
    }
      if(ans == 0)
      return n;
      return ans;
}
 
// Driver code
int main()
{
    string str = "abc";
    cout << findRotations(str) << endl;
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to determine minimum
// number of rotations required to yield
// same string.
#include <iostream>
using namespace std;
 
// Driver program
int main()
{
    string String = "aaaa";
    string check = "";
 
    for(int r = 1; r < String.length() + 1; r++)
    {
        
        // checking the input after each rotation
        check = String.substr(0, r) + String.substr(r, String.length()-r);
     
        // following if statement checks if input is
        // equals to check , if yes it will print r and
        // break out of the loop
        if(check == String){
            cout<<r;
            break;
        }
    }
    return 0;
}
 
// This code is contributed by shinjanpatra

Time complexity: quadratic
Space complexity: linear
// C++ program to check if
// two strings are rotations
// of each other
#include<bits/stdc++.h>
using namespace std;
bool isRotation(string a,
                string b)
{
  int n = a.length();
  int m = b.length();
  if (n != m)
    return false;
 
  // create lps[] that
  // will hold the longest
  // prefix suffix values
  // for pattern
  int lps[n];
 
  // length of the previous
  // longest prefix suffix
  int len = 0;
  int i = 1;
   
  // lps[0] is always 0
  lps[0] = 0;
 
  // the loop calculates
  // lps[i] for i = 1 to n-1
  while (i < n)
  {
    if (a[i] == b[len])
    {
      lps[i] = ++len;
      ++i;
    }
    else
    {
      if (len == 0)
      {
        lps[i] = 0;
        ++i;
      }
      else
      {
        len = lps[len - 1];
      }
    }
  }
 
  i = 0;
 
  // Match from that rotating
  // point
  for (int k = lps[n - 1];
           k < m; ++k)
  {
    if (b[k] != a[i++])
      return false;
  }
  return true;
}
 
// Driver code
int main()
{
  string s1 = "ABACD";
  string s2 = "CDABA";
  cout << (isRotation(s1, s2) ?
           "1" : "0");
}
 
// This code is contributed by Chitranayal

Time complexity: linear
Space complexity: linear
// C++ program to check if a string is two time
// rotation of another string.
#include<bits/stdc++.h>
using namespace std;
 
// Function to check if string2 is obtained by
// string 1
bool isRotated(string str1, string str2)
{
    if (str1.length() != str2.length())
        return false;
    if(str1.length()<2){
      return str1.compare(str2) == 0;
    }
    string clock_rot = "";
    string anticlock_rot = "";
    int len = str2.length();
 
    // Initialize string as anti-clockwise rotation
    anticlock_rot = anticlock_rot +
                    str2.substr(len-2, 2) +
                    str2.substr(0, len-2) ;
 
    // Initialize string as clock wise rotation
    clock_rot = clock_rot +
                str2.substr(2) +
                str2.substr(0, 2) ;
 
    // check if any of them is equal to string1
    return (str1.compare(clock_rot) == 0 ||
            str1.compare(anticlock_rot) == 0);
}
 
// Driver code
int main()
{
    string str1 = "geeks";
    string str2 = "eksge";
 
    isRotated(str1, str2) ? cout << "Yes"
                          : cout << "No";
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to count all rotation divisible
// by 4.
#include <bits/stdc++.h>
using namespace std;
 
// Returns count of all rotations divisible
// by 4
int countRotations(string n)
{
    int len = n.length();
 
    // For single digit number
    if (len == 1)
    {
        int oneDigit = n.at(0)-'0';
        if (oneDigit%4 == 0)
            return 1;
        return 0;
    }
 
    // At-least 2 digit number (considering all
    // pairs)
    int twoDigit, count = 0;
    for (int i=0; i<(len-1); i++)
    {
        twoDigit = (n.at(i)-'0')*10 + (n.at(i+1)-'0');
        if (twoDigit%4 == 0)
            count++;
    }
 
    // Considering the number formed by the pair of
    // last digit and 1st digit
    twoDigit = (n.at(len-1)-'0')*10 + (n.at(0)-'0');
    if (twoDigit%4 == 0)
        count++;
 
    return count;
}
 
//Driver program
int main()
{
    string n = "4834";
    cout << "Rotations: " << countRotations(n) << endl;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to print reverse
// of words in a string.
#include <iostream>
 
using namespace std;
 
string wordReverse(string str)
{
    int i = str.length() - 1;
    int start, end = i + 1;
    string result = "";
 
    while (i >= 0) {
        if (str[i] == ' ') {
            start = i + 1;
            while (start != end)
                result += str[start++];
 
            result += ' ';
 
            end = i;
        }
        i--;
    }
    start = 0;
    while (start != end)
        result += str[start++];
 
    return result;
}
 
// Driver code
int main()
{
    string str = "I AM A GEEK";
 
    cout << wordReverse(str);
 
    return 0;
}
 
// This code is contributed
// by Imam

Time complexity: linear
Space complexity: linear
#include <algorithm>
#include <iostream>
#include <string>
 
using namespace std;
 
string reverse_words(string s)
{
    int left = 0, i = 0, n = s.length();
    while (s[i] == ' '){
    i++;
    }
 
    left = i;
 
    while (i < n)
    {
    if (i + 1 == n || s[i] == ' ')
    {
        int j = i - 1;
        if (i + 1 == n)
        j++;
       
        reverse(s.begin()+left, s.begin()+j+1);
 
        left = i + 1;
    }
    if (left < n && s[left] == ' ' && i > left)
        left = i;
 
    i++;
    }
    // reversing the string
    reverse(s.begin(), s.end());
 
    return s;
}
 
int main()
{
 
    string str = "I AM A GEEK";
 
    str = reverse_words(str);
 
    cout << str;
 
    return 0;
// This code is contributed
// by Gatea David
}

Time complexity: linear
Space complexity: constant
// C++ program to reverse a string using stack
#include <bits/stdc++.h>
using namespace std;
 
void recursiveReverse(string &str)
{
   stack<char> st;
   for (int i=0; i<str.length(); i++)
       st.push(str[i]);
 
   for (int i=0; i<str.length(); i++) {
       str[i] = st.top();
       st.pop();
   }      
}
 
// Driver program
int main()
{
    string str = "geeksforgeeks";
    recursiveReverse(str);
    cout << str;
    return 0;
}

Time complexity: linear
Space complexity: linear
// A Simple Iterative C++ program to reverse
// a string
#include <bits/stdc++.h>
using namespace std;
 
// Function to reverse a string
void reverseStr(string& str)
{
    int n = str.length();
 
    // Swap character starting from two
    // corners
    for (int i = 0; i < n / 2; i++)
        swap(str[i], str[n - i - 1]);
}
 
// Driver program
int main()
{
    string str = "geeksforgeeks";
    reverseStr(str);
    cout << str;
    return 0;
}

Time complexity: linear
Space complexity: constant