Banana-Leopard/TASTY huggingface dataset:
// CPP program to count substrings
// with odd decimal value
#include<iostream>
using namespace std;
 
// function to count number of substrings
// with odd decimal representation
int countSubstr(string s)
{  
    int n = s.length();
     
    // auxiliary array to store count
    // of 1's before ith index
    int auxArr[n] = {0};
     
    if (s[0] == '1')
        auxArr[0] = 1;
     
    // store  count of 1's before
    // i-th  index
    for (int i=1; i<n; i++)
    {
        if (s[i] == '1')
            auxArr[i] = auxArr[i-1]+1;
        else
            auxArr[i] = auxArr[i-1];
    }
     
    // variable to store answer
    int count = 0;
     
    // traverse the string reversely to
    // calculate number of odd substrings
    // before i-th index
    for (int i=n-1; i>=0; i--)   
        if (s[i] == '1')
            count += auxArr[i];   
     
    return count;
}
 
// Driver code
int main()
{
    string s = "1101";   
    cout << countSubstr(s);   
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to generate n-bit Gray codes
#include <iostream>
#include <string>
#include <vector>
using namespace std;
 
// This function generates all n bit Gray codes and prints the
// generated codes
void generateGrayarr(int n)
{
    // base case
    if (n <= 0)
        return;
 
    // 'arr' will store all generated codes
    vector<string> arr;
 
    // start with one-bit pattern
    arr.push_back("0");
    arr.push_back("1");
 
    // Every iteration of this loop generates 2*i codes from previously
    // generated i codes.
    int i, j;
    for (i = 2; i < (1<<n); i = i<<1)
    {
        // Enter the previously generated codes again in arr[] in reverse
        // order. Nor arr[] has double number of codes.
        for (j = i-1 ; j >= 0 ; j--)
            arr.push_back(arr[j]);
 
        // append 0 to the first half
        for (j = 0 ; j < i ; j++)
            arr[j] = "0" + arr[j];
 
        // append 1 to the second half
        for (j = i ; j < 2*i ; j++)
            arr[j] = "1" + arr[j];
    }
 
    // print contents of arr[]
    for (i = 0 ; i < arr.size() ; i++ )
        cout << arr[i] << endl;
}
 
// Driver program to test above function
int main()
{
    generateGrayarr(3);
    return 0;
}

Time complexity: np
Space complexity: np
// C++ program to generate
// n-bit Gray codes
 
#include <bits/stdc++.h>
using namespace std;
 
// This function generates all n
// bit Gray codes and prints the
// generated codes
vector<string> generateGray(int n)
{
    // Base case
    if (n <= 0)
        return {"0"};
 
    if (n == 1)
    {
      return {"0","1"};
    }
 
    //Recursive case
    vector<string> recAns=
          generateGray(n-1);
    vector<string> mainAns;
     
    // Append 0 to the first half
    for(int i=0;i<recAns.size();i++)
    {
      string s=recAns[i];
      mainAns.push_back("0"+s);
    }
     
     // Append 1 to the second half
    for(int i=recAns.size()-1;i>=0;i--)
    {
       string s=recAns[i];
       mainAns.push_back("1"+s);
    }
    return mainAns;
}
 
// Function to generate the
// Gray code of N bits
void generateGrayarr(int n)
{
    vector<string> arr;
    arr=generateGray(n);
    // print contents of arr
    for (int i = 0 ; i < arr.size();
         i++ )
        cout << arr[i] << endl;
}
 
// Driver Code
int main()
{
    generateGrayarr(3);
    return 0;
}

Time complexity: np
Space complexity: np
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
void GreyCode(int n)
{
     // power of 2
    for (int i = 0; i < (1 << n); i++)
    {
        // Generating the decimal
        // values of gray code then using
        // bitset to convert them to binary form
        int val = (i ^ (i >> 1));
         
        // Using bitset
        bitset<32> r(val);
         
        // Converting to string
        string s = r.to_string();
        cout << s.substr(32 - n) << " ";
    }
}
 
 
// Driver Code
int main()
{
    int n;
    n = 4;
   
    // Function call
    GreyCode(n);
    
    return 0;
}

Time complexity: np
Space complexity: linear
// C++ program to print all N-bit binary
#include <bits/stdc++.h>
using namespace std;
 
/* function to generate n  digit numbers*/
void printRec(string number, int extraOnes,
              int remainingPlaces)
{
    /* if number generated */
    if (0 == remainingPlaces) {
        cout << number << " ";
        return;
    }
 
    /* Append 1 at the current number and reduce
       the remaining places by one */
    printRec(number + "1", extraOnes + 1,
             remainingPlaces - 1);
 
    /* If more ones than zeros, append 0 to the
       current number and reduce the remaining
       places by one*/
    if (0 < extraOnes)
        printRec(number + "0", extraOnes - 1,
                 remainingPlaces - 1);
}
 
void printNums(int n)
{
    string str = "";
    printRec(str, 0, n);
}
 
// Driver code
int main()
{
    int n = 4;
   
    // Function call
    printNums(n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to print all N-bit binary
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
 
// Function to get the binary representation
// of the number N
string getBinaryRep(int N, int num_of_bits)
{
    string r = "";
    num_of_bits--;
   
    // loop for each bit
    while (num_of_bits >= 0)
    {
        if (N & (1 << num_of_bits))
            r.append("1");
        else
            r.append("0");
        num_of_bits--;
    }
    return r;
}
 
vector<string> NBitBinary(int N)
{
    vector<string> r;
    int first = 1 << (N - 1);
    int last = first * 2;
     
    // generate numbers in the range of (2^N)-1 to 2^(N-1)
    // inclusive
    for (int i = last - 1; i >= first; --i)
    {
        int zero_cnt = 0;
        int one_cnt = 0;
        int t = i;
        int num_of_bits = 0;
         
        // longest prefix check
        while (t)
        {
            if (t & 1)
                one_cnt++;
            else
                zero_cnt++;
            num_of_bits++;
            t = t >> 1;
        }
       
        // if counts of 1 is greater than
        // counts of zero
        if (one_cnt >= zero_cnt)
        {
            // do sub-prefixes check
            bool all_prefix_match = true;
            int msk = (1 << num_of_bits) - 2;
            int prefix_shift = 1;
            while (msk)
            {
 
                int prefix = (msk & i) >> prefix_shift;
                int prefix_one_cnt = 0;
                int prefix_zero_cnt = 0;
                while (prefix)
                {
                    if (prefix & 1)
                        prefix_one_cnt++;
                    else
                        prefix_zero_cnt++;
                    prefix = prefix >> 1;
                }
                if (prefix_zero_cnt > prefix_one_cnt)
                {
                    all_prefix_match = false;
                    break;
                }
                prefix_shift++;
                msk = msk & (msk << 1);
            }
            if (all_prefix_match)
            {
                r.push_back(getBinaryRep(i, num_of_bits));
            }
        }
    }
    return r;
}
 
// Driver code
int main()
{
    int n = 4;
   
    // Function call
    vector<string> results = NBitBinary(n);
    for (int i = 0; i < results.size(); ++i)
        cout << results[i] << " ";
    cout << endl;
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to add n binary strings
#include <bits/stdc++.h>
using namespace std;
 
// This function adds two binary strings and return
// result as a third string
string addBinaryUtil(string a, string b)
{
    string result = ""; // Initialize result
    int s = 0; // Initialize digit sum
 
    // Traverse both strings starting from last
    // characters
    int i = a.size() - 1, j = b.size() - 1;
    while (i >= 0 || j >= 0 || s == 1) {
 
        // Compute sum of last digits and carry
        s += ((i >= 0) ? a[i] - '0' : 0);
        s += ((j >= 0) ? b[j] - '0' : 0);
 
        // If current digit sum is 1 or 3,
        // add 1 to result
        result = char(s % 2 + '0') + result;
 
        // Compute carry
        s /= 2;
 
        // Move to next digits
        i--;
        j--;
    }
    return result;
}
 
// function to add n binary strings
string addBinary(string arr[], int n)
{
    string result = "";
    for (int i = 0; i < n; i++)
        result = addBinaryUtil(result, arr[i]);
    return result;
}
 
// Driver program
int main()
{
    string arr[] = { "1", "10", "11" };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << addBinary(arr, n) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to generate power set in
// lexicographic order.
#include <bits/stdc++.h>
using namespace std;
 
// str : Stores input string
// n : Length of str.
void func(string s, vector<string>& str, int n, int pow_set)
{
    int i, j;
    for (i = 0; i < pow_set; i++) {
        string x;
        for (j = 0; j < n; j++) {
            if (i & 1 << j) {
                x = x + s[j];
            }
        }
        if (i != 0)
            str.push_back(x);
    }
}
int main()
{
    int n;
    string s;
    vector<string> str;
    s = "cab";
    n = s.length();
    int pow_set = pow(2, n);
    func(s, str, n, pow_set);
    sort(str.begin(), str.end());
    for (int i = 0; i < str.size(); i++)
        cout << str[i] << " ";
    cout << endl;
 
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to print nth permutation with
// using next_permute()
#include <bits/stdc++.h>
using namespace std;
 
// Function to print nth permutation
// using next_permute()
void nPermute(string str, long int n)
{
    // Sort the string in lexicographically
    // ascending order
    sort(str.begin(), str.end());
 
    // Keep iterating until
    // we reach nth position
    long int i = 1;
    do {
        // check for nth iteration
        if (i == n)
            break;
 
        i++;
    } while (next_permutation(str.begin(), str.end()));
 
    // print string after nth iteration
    cout << str;
}
 
// Driver code
int main()
{
    string str = "GEEKSFORGEEKS";
    long int n = 100;
    nPermute(str, n);
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// C++ program to print rank of
// string using next_permute()
#include <bits/stdc++.h>
using namespace std;
 
// Function to print rank of string
// using next_permute()
int findRank(string str)
{
    // store original string
    string orgStr = str;
 
    // Sort the string in lexicographically
    // ascending order
    sort(str.begin(), str.end());
 
    // Keep iterating until
    // we reach equality condition
    long int i = 1;
    do {
        // check for nth iteration
        if (str == orgStr)
            break;
 
        i++;
    } while (next_permutation(str.begin(), str.end()));
 
    // return iterator value
    return i;
}
 
// Driver code
int main()
{
    string str = "GEEKS";
    cout << findRank(str);
    return 0;
}

Time complexity: nlogn
Space complexity: constant
// A simple C++ program to find lexicographically minimum rotation
// of a given string
#include <iostream>
#include <algorithm>
using namespace std;
 
// This functionr return lexicographically minimum
// rotation of str
string minLexRotation(string str)
{
    // Find length of given string
    int n = str.length();
 
    // Create an array of strings to store all rotations
    string arr[n];
 
    // Create a concatenation of string with itself
    string concat = str + str;
 
    // One by one store all rotations of str in array.
    // A rotation is obtained by getting a substring of concat
    for (int i = 0; i < n; i++)
        arr[i] = concat.substr(i, n);
 
    // Sort all rotations
    sort(arr, arr+n);
 
    // Return the first rotation from the sorted array
    return arr[0];
}
 
// Driver program to test above function
int main()
{
    cout << minLexRotation("GEEKSFORGEEKS") << endl;
    cout << minLexRotation("GEEKSQUIZ") << endl;
    cout << minLexRotation("BCABDADAB") << endl;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to print all distinct subsequences
// of a string.
#include <bits/stdc++.h>
using namespace std;
 
// Finds and stores result in st for a given
// string s.
void generate(set<string>& st, string s)
{
    if (s.size() == 0)
        return;
 
    // If current string is not already present.
    if (st.find(s) == st.end()) {
        st.insert(s);
 
        // Traverse current string, one by one
        // remove every character and recur.
        for (int i = 0; i < s.size(); i++) {
            string t = s;
            t.erase(i, 1);
            generate(st, t);
        }
    }
    return;
}
 
// Driver code
int main()
{
    string s = "xyz";
    set<string> st;
    set<string>::iterator it;
    generate(st, s);
    for (auto it = st.begin(); it != st.end(); it++)
        cout << *it << endl;
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// CPP code to find the lexicographically
// smallest string
#include <bits/stdc++.h>
using namespace std;
 
// Compares two strings by checking if
// which of the two concatenations causes
// lexicographically smaller string.
bool compare(string a, string b)
{
    return (a+b < b+a);
}
 
string lexSmallest(string a[], int n)
{
    // Sort strings using above compare()
    sort(a, a+n, compare);
 
    // Concatenating sorted strings
    string answer = "";
    for (int i = 0; i < n; i++)
        answer += a[i];
 
    return answer;
}
 
// Driver code
int main()
{
    string a[] = { "c", "cb", "cba" };
    int n = sizeof(a)/sizeof(a[0]);
    cout << lexSmallest(a, n);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// CPP Program to create concatenation of all
// substrings in lexicographic order.
#include <bits/stdc++.h>
using namespace std;
 
string lexicographicSubConcat(string s)
{
    int n = s.length();
 
    // Creating an array to store substrings
    int sub_count = n*(n+1)/2;
    string arr[sub_count];    
 
    // finding all substrings of string
    int index = 0;
    for (int i = 0; i < n; i++)
        for (int len = 1; len <= n - i; len++)
            arr[index++] = s.substr(i, len);
     
    // Sort all substrings in lexicographic
    // order
    sort(arr, arr + sub_count);
 
    // Concatenating all substrings
    string res = "";
    for (int i = 0; i < sub_count; i++)
        res += arr[i];    
         
    return res;   
}
 
int main()
{
    string s = "abc";
    cout << lexicographicSubConcat(s);
    return 0;
}

Time complexity: cubic
Space complexity: linear
// CPP for constructing smallest palindrome
#include <bits/stdc++.h>
using namespace std;
 
// function for printing palindrome
string constructPalin(string str, int len)
{
    int i = 0, j = len - 1;
 
    // iterate till i<j
    for (; i < j; i++, j--) {
 
        // continue if str[i]==str[j]
        if (str[i] == str[j] && str[i] != '*')
            continue;
 
        // update str[i]=str[j]='a' if both are '*'
        else if (str[i] == str[j] && str[i] == '*') {
            str[i] = 'a';
            str[j] = 'a';
            continue;
        }
 
        // update str[i]=str[j] if only str[i]='*'
        else if (str[i] == '*') {
            str[i] = str[j];
            continue;
        }
 
        // update str[j]=str[i] if only str[j]='*'
        else if (str[j] == '*') {
            str[j] = str[i];
            continue;
        }
 
        // else print not possible and return
        cout << "Not Possible";
        return "";
    }
    return str;
}
 
// driver program
int main()
{
    string str = "bca*xc**b";
    int len = str.size();
    cout << constructPalin(str, len);
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to find Lexicographically
// smallest string whose hamming distance
// from given string is exactly K
#include <bits/stdc++.h>
using namespace std;
 
// function to find Lexicographically
// smallest string with hamming distance k
void findString(string str, int n, int k)
{
    // If k is 0, output input string
    if (k == 0) {
        cout << str << endl;
        return;
    }
 
    // Copying input string into output
    // string
    string str2 = str;
    int p = 0;
 
    // Traverse all the character of the
    // string
    for (int i = 0; i < n; i++) {
     
        // If current character is not 'a'
        if (str2[i] != 'a') {
     
            // copy character 'a' to
            // output string
            str2[i] = 'a';
            p++;
 
            // If hamming distance became k,
            // break;
            if (p == k)
                break;
        }
    }
 
    // If k is less than p
    if (p < k) {
         
        // Traversing string in reverse
        // order
        for (int i = n - 1; i >= 0; i--)
            if (str[i] == 'a') {
                str2[i] = 'b';
                p++;
 
                if (p == k)
                    break;
            }
    }
 
    cout << str2 << endl;
}
 
// Driven Program
int main()
{
    string str = "pqrs";
    int n = str.length();
    int k = 2;
 
    findString(str, n, k);
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find lexicographically next
// string
#include <bits/stdc++.h>
using namespace std;
 
string nextWord(string s)
{
    // If string is empty.
    if (s == "")
        return "a";
 
    // Find first character from right
    // which is not z.
     
    int i = s.length() - 1;
    while (s[i] == 'z' && i >= 0)
        i--;
 
    // If all characters are 'z', append
    // an 'a' at the end.
    if (i == -1)
        s = s + 'a';
 
    // If there are some non-z characters
    else
        s[i]++;
 
    return s;
}
 
// Driver code
int main()
{
    string str = "samez";
    cout << nextWord(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find lexicographically largest
// subsequence where every character appears at
// least k times.
#include <bits/stdc++.h>
using namespace std;
 
// Find lexicographically largest subsequence of
// s[0..n-1] such that every character appears
// at least k times. The result is filled in t[]
void subsequence(char s[], char t[], int n, int k)
{
    int last = 0, cnt = 0, new_last = 0, size = 0;
 
    // Starting from largest character 'z' to 'a'
    for (char ch = 'z'; ch >= 'a'; ch--) {
        cnt = 0;
 
        // Counting the frequency of the character
        for (int i = last; i < n; i++) {
            if (s[i] == ch)
                cnt++;
        }
 
        // If frequency is greater than k
        if (cnt >= k) {
 
            // From the last point we leave
            for (int i = last; i < n; i++) {
 
                // check if string contain ch
                if (s[i] == ch) {
 
                    // If yes, append to output string
                    t[size++] = ch;
                    new_last = i;
                }
            }
 
            // Update the last point.
            last = new_last;
        }
    }
    t[size] = '\0';
}
 
// Driver code
int main()
{
    char s[] = "banana";
    int n = sizeof(s);
    int k = 2;
    char t[n];
    subsequence(s, t, n - 1, k);
    cout << t << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to find the string
// in lexicographic order which is
// in between given two strings
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the lexicographically 
// next string
string lexNext(string s, int n)
{  
    // Iterate from last character
    for (int i = n - 1; i >= 0; i--)
    {  
        // If not 'z', increase by one
        if (s[i] != 'z')
        {
            s[i]++;
            return s;
        }
         
        // if 'z', change it to 'a'
        s[i] = 'a';
    }
}
 
// Driver Code
int main()
{
    string S = "abcdeg", T = "abcfgh";
    int n = S.length();
    string res = lexNext(S, n);
 
    // If not equal, print the
    // resultant string
    if (res != T)
        cout << res << endl;   
    else
        cout << "-1" << endl;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to demonstrate working of
// prev_permutation()
#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    string str = "4321";
    if (prev_permutation(str.begin(), str.end()))
        cout << "Previous permutation is " << str;
    else
        cout << "Previous permutation doesn't exist";
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to print all permutations with
// duplicates allowed using prev_permutation()
#include <bits/stdc++.h>
using namespace std;
 
// Function to compute the previous permutation
bool prevPermutation(string& str)
{
    // Find index of the last element of the string
    int n = str.length() - 1;
 
    // Find largest index i such that str[i - 1] >
    // str[i]
    int i = n;
    while (i > 0 && str[i - 1] <= str[i])
        i--;
 
    // if string is sorted in ascending order
    // we're at the last permutation
    if (i <= 0)
        return false;
 
    // Note - str[i..n] is sorted in ascending order
 
    // Find rightmost element's index that is less
    // than str[i - 1]
    int j = i - 1;
    while (j + 1 <= n && str[j + 1] < str[i - 1])
        j++;
 
    // Swap character at i-1 with j
    swap(str[i - 1], str[j]);
 
    // Reverse the substring [i..n]
    reverse(str.begin() + i, str.end());
 
    return true;
}
 
// Driver code
int main()
{
    string str = "4321";
    if (prevPermutation(str))
        cout << "Previous permutation is " << str;
    else
        cout << "Previous permutation doesn't exist";
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to print
// n-th permutation
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long int
 
const int MAX_CHAR = 26;
const int MAX_FACT = 20;
ll fact[MAX_FACT];
 
// Utility for calculating factorials
void precomputeFactorials()
{
    fact[0] = 1;
    for (int i = 1; i < MAX_FACT; i++)
        fact[i] = fact[i - 1] * i;
}
 
// Function for nth permutation
void nPermute(char str[], int n)
{
    precomputeFactorials();
 
    // Length of given string
    int len = strlen(str);
 
    // Count frequencies of all
    // characters
    int freq[MAX_CHAR] = { 0 };
    for (int i = 0; i < len; i++)
        freq[str[i] - 'a']++;
 
    // Out string for output string
    char out[MAX_CHAR];
 
    // Iterate till sum equals n
    int sum = 0;
    int k = 0;
 
    // We update both n and sum in this
    // loop.
    while (sum != n) {
 
        sum = 0;
        // Check for characters present in freq[]
        for (int i = 0; i < MAX_CHAR; i++) {
            if (freq[i] == 0)
                continue;
 
            // Remove character
            freq[i]--;
 
            // Calculate sum after fixing
            // a particular char
            int xsum = fact[len - 1 - k];
            for (int j = 0; j < MAX_CHAR; j++)
                xsum /= fact[freq[j]];
            sum += xsum;
 
            // if sum > n fix that char as
            // present char and update sum
            // and required nth after fixing
            // char at that position
            if (sum >= n) {
                out[k++] = i + 'a';
                n -= (sum - xsum);
                break;
            }
 
            // if sum < n, add character back
            if (sum < n)
                freq[i]++;
        }
    }
 
    // if sum == n means this
    // char will provide its
    // greatest permutation
    // as nth permutation
    for (int i = MAX_CHAR - 1;
         k < len && i >= 0; i--)
        if (freq[i]) {
            out[k++] = i + 'a';
            freq[i++]--;
        }
 
    // append string termination
    // character and print result
    out[k] = '\0';
    cout << out;
}
 
// Driver program
int main()
{
    int n = 2;
    char str[] = "geeksquiz";
 
    nPermute(str, n);
    return 0;
}

Time complexity: linear
Space complexity: linear