Banana-Leopard/TASTY huggingface dataset:
// C++ program to find lexicographic rank
// of a string
 
#include <bits/stdc++.h>
using namespace std;
 
// A utility function to find
// factorial of n
int fact(int n) {
    return (n <= 1) ? 1 : n * fact(n - 1);
}
 
// A utility function to count
// smaller characters on right of arr[low]
int findSmallerInRight(string str, int low, int high)
{
    int countRight = 0, i;
 
    for (i = low + 1; i <= high; ++i)
        if (str[i] < str[low])
            ++countRight;
 
    return countRight;
}
 
// A function to find rank of a string
// in all permutations of characters
int findRank(string str)
{
    int len = str.size();
    int mul = fact(len);
    int rank = 1;
    int countRight;
 
    int i;
    for (i = 0; i < len; ++i) {
        mul /= len - i;
 
        // Count number of chars smaller than str[i]
        // from str[i+1] to str[len-1]
        countRight = findSmallerInRight(str, i, len - 1);
 
        rank += countRight * mul;
    }
 
    return rank;
}
 
// Driver code
int main()
{
    string str = "string";
 
    // Function call
    cout << findRank(str);
    return 0;
}
 
// This code is contributed by Akanksha Rai

Time complexity: quadratic
Space complexity: constant
// A O(n) solution for finding rank of string
 
#include <bits/stdc++.h>
using namespace std;
#define MAX_CHAR 256
 
// A utility function to find factorial of n
int fact(int n) {
    return (n <= 1) ? 1 : n * fact(n - 1);
}
 
// Construct a count array where value at every index
// contains count of smaller characters in whole string
void populateAndIncreaseCount(int* count, string str)
{
    int i;
 
    for (i = 0; str[i]; ++i)
        ++count[str[i]];
 
    for (i = 1; i < MAX_CHAR; ++i)
        count[i] += count[i - 1];
}
 
// Removes a character ch from count[] array
// constructed by populateAndIncreaseCount()
void updatecount(int* count, char ch)
{
    int i;
    for (i = ch; i < MAX_CHAR; ++i)
        --count[i];
}
 
// A function to find rank of a string in all permutations
// of characters
int findRank(string str)
{
    int len = str.size();
    int mul = fact(len);
    int rank = 1, i;
 
    // All elements of count[] are initialized with 0
    int count[MAX_CHAR] = { 0 };
 
    // Populate the count array such that count[i]
    // contains count of characters which are present
    // in str and are smaller than i
    populateAndIncreaseCount(count, str);
 
    for (i = 0; i < len; ++i) {
        mul /= len - i;
 
        // Count number of chars smaller than str[i]
        // from str[i+1] to str[len-1]
        rank += count[str[i] - 1] * mul;
 
        // Reduce count of characters greater than str[i]
        updatecount(count, str[i]);
    }
 
    return rank;
}
 
// Driver code
int main()
{
    string str = "string";
   
    // Function call
    cout << findRank(str);
    return 0;
}
 
// This is code is contributed by rathbhupendra

Time complexity: linear
Space complexity: constant
/* C++ Program for Bad Character Heuristic of Boyer
Moore String Matching Algorithm */
#include <bits/stdc++.h>
using namespace std;
# define NO_OF_CHARS 256
 
// The preprocessing function for Boyer Moore's
// bad character heuristic
void badCharHeuristic( string str, int size,
                        int badchar[NO_OF_CHARS])
{
    int i;
 
    // Initialize all occurrences as -1
    for (i = 0; i < NO_OF_CHARS; i++)
        badchar[i] = -1;
 
    // Fill the actual value of last occurrence
    // of a character
    for (i = 0; i < size; i++)
        badchar[(int) str[i]] = i;
}
 
/* A pattern searching function that uses Bad
Character Heuristic of Boyer Moore Algorithm */
void search( string txt, string pat)
{
    int m = pat.size();
    int n = txt.size();
 
    int badchar[NO_OF_CHARS];
 
    /* Fill the bad character array by calling
    the preprocessing function badCharHeuristic()
    for given pattern */
    badCharHeuristic(pat, m, badchar);
 
    int s = 0; // s is shift of the pattern with
                // respect to text
    while(s <= (n - m))
    {
        int j = m - 1;
 
        /* Keep reducing index j of pattern while
        characters of pattern and text are
        matching at this shift s */
        while(j >= 0 && pat[j] == txt[s + j])
            j--;
 
        /* If the pattern is present at current
        shift, then index j will become -1 after
        the above loop */
        if (j < 0)
        {
            cout << "pattern occurs at shift = " <<  s << endl;
 
            /* Shift the pattern so that the next
            character in text aligns with the last
            occurrence of it in pattern.
            The condition s+m < n is necessary for
            the case when pattern occurs at the end
            of text */
            s += (s + m < n)? m-badchar[txt[s + m]] : 1;
 
        }
 
        else
            /* Shift the pattern so that the bad character
            in text aligns with the last occurrence of
            it in pattern. The max function is used to
            make sure that we get a positive shift.
            We may get a negative shift if the last
            occurrence of bad character in pattern
            is on the right side of the current
            character. */
            s += max(1, j - badchar[txt[s + j]]);
    }
}
 
/* Driver code */
int main()
{
    string txt= "ABAAABCD";
    string pat = "ABC";
    search(txt, pat);
    return 0;
}
  
 // This code is contributed by rathbhupendra

Time complexity: quadratic
Space complexity: constant
// CPP program to print the
// string in 'plus' pattern
#include <bits/stdc++.h>
#define max 100
using namespace std;
 
// Function to make a cross in the matrix
void carveCross(string str)
{
    int n = str.length();
    if (n % 2 == 0)
    { 
        /* As, it is not possible to make
        the cross exactly in the middle of
        the matrix with an even length string.*/
        cout << "Not possible. Please enter "
             << "odd length string.\n";
    }
    else {
 
        // declaring a 2D array i.e a matrix
        char arr[max][max];
        int m = n / 2;
 
        /* Now, we will fill all the
        elements of the array with 'X'*/
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                arr[i][j] = 'X';
            }
        }
 
        /* Now, we will place the characters
        of the string in the matrix, such
        that a cross is formed in it.*/
        for (int i = 0; i < n; i++)
        {
            /* here the characters of the
            string will be added in the
            middle column of our array.*/
            arr[i][m] = str[i];
        }
         
        for (int i = 0; i < n; i++)
        {
            // here the characters of the
            // string will be added in the
            // middle row of our array.
            arr[m][i] = str[i];
        }
 
        /* Now finally, we will print
        the array*/
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cout << arr[i][j] << " ";
            }
            cout << "\n";
        }
    }
}
 
// driver code
int main()
{
    string str = "PICTURE";
    carveCross(str);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to implement wildcard
// pattern matching algorithm
#include <bits/stdc++.h>
using namespace std;
 
// Function that matches input str with
// given wildcard pattern
bool strmatch(char str[], char pattern[], int n, int m)
{
    // empty pattern can only match with
    // empty string
    if (m == 0)
        return (n == 0);
 
    // lookup table for storing results of
    // subproblems
    bool lookup[n + 1][m + 1];
 
    // initialize lookup table to false
    memset(lookup, false, sizeof(lookup));
 
    // empty pattern can match with empty string
    lookup[0][0] = true;
 
    // Only '*' can match with empty string
    for (int j = 1; j <= m; j++)
        if (pattern[j - 1] == '*')
            lookup[0][j] = lookup[0][j - 1];
 
    // fill the table in bottom-up fashion
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            // Two cases if we see a '*'
            // a) We ignore ‘*’ character and move
            //    to next  character in the pattern,
            //     i.e., ‘*’ indicates an empty sequence.
            // b) '*' character matches with ith
            //     character in input
            if (pattern[j - 1] == '*')
                lookup[i][j]
                    = lookup[i][j - 1] || lookup[i - 1][j];
 
            // Current characters are considered as
            // matching in two cases
            // (a) current character of pattern is '?'
            // (b) characters actually match
            else if (pattern[j - 1] == '?'
                     || str[i - 1] == pattern[j - 1])
                lookup[i][j] = lookup[i - 1][j - 1];
 
            // If characters don't match
            else
                lookup[i][j] = false;
        }
    }
 
    return lookup[n][m];
}
 
int main()
{
    char str[] = "baaabab";
    char pattern[] = "*****ba*****ab";
    // char pattern[] = "ba*****ab";
    // char pattern[] = "ba*ab";
    // char pattern[] = "a*ab";
    // char pattern[] = "a*****ab";
    // char pattern[] = "*a*****ab";
    // char pattern[] = "ba*ab****";
    // char pattern[] = "****";
    // char pattern[] = "*";
    // char pattern[] = "aa?ab";
    // char pattern[] = "b*b";
    // char pattern[] = "a*a";
    // char pattern[] = "baaabab";
    // char pattern[] = "?baaabab";
    // char pattern[] = "*baaaba*";
 
    if (strmatch(str, pattern, strlen(str),
                 strlen(pattern)))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
 
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// C++ program to implement wildcard
// pattern matching algorithm
 
#include <bits/stdc++.h>
 
using namespace std;
 
// Function that matches input str with
// given wildcard pattern
vector<vector<int> > dp;
int finding(string& s, string& p, int n, int m)
{
    // return 1 if n and m are negative
    if (n < 0 && m < 0)
        return 1;
   
    // return 0 if m is negative
    if (m < 0)
        return 0;
   
    // return n if n is negative
    if (n < 0)
    {
        // while m is positive
        while (m >= 0)
        {
            if (p[m] != '*')
                return 0;
            m--;
        }
        return 1;
    }
    
    // if dp state is not visited
    if (dp[n][m] == -1)
    {
        if (p[m] == '*')
        {
            return dp[n][m] = finding(s, p, n - 1, m)
                              || finding(s, p, n, m - 1);
        }
        else
        {
            if (p[m] != s[n] && p[m] != '?')
                return dp[n][m] = 0;
            else
                return dp[n][m]
                       = finding(s, p, n - 1, m - 1);
        }
    }
   
    // return dp[n][m] if dp state is previsited
    return dp[n][m];
}
 
 
bool isMatch(string s, string p)
{
    dp.clear();
     
    // resize the dp array
    dp.resize(s.size() + 1, vector<int>(p.size() + 1, -1));
    return dp[s.size()][p.size()]
           = finding(s, p, s.size() - 1, p.size() - 1);
}
 
// Driver code
int main()
{
    string str = "baaabab";
    string pattern = "*****ba*****ab";
    // char pattern[] = "ba*****ab";
    // char pattern[] = "ba*ab";
    // char pattern[] = "a*ab";
    // char pattern[] = "a*****ab";
    // char pattern[] = "*a*****ab";
    // char pattern[] = "ba*ab****";
    // char pattern[] = "****";
    // char pattern[] = "*";
    // char pattern[] = "aa?ab";
    // char pattern[] = "b*b";
    // char pattern[] = "a*a";
    // char pattern[] = "baaabab";
    // char pattern[] = "?baaabab";
    // char pattern[] = "*baaaba*";
 
    if (isMatch(str, pattern))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
 
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// C++ program to implement wildcard
// pattern matching algorithm
#include <bits/stdc++.h>
using namespace std;
 
// Function that matches input text
// with given wildcard pattern
bool strmatch(char txt[], char pat[],
              int n, int m)
{
     
    // empty pattern can only
    // match with empty string.
    // Base Case :
    if (m == 0)
        return (n == 0);
 
    // step-1 :
    // initialize markers :
    int i = 0, j = 0, index_txt = -1,
                       index_pat = -1;
 
    while (i < n)
    {
 
        // For step - (2, 5)
        if (j < m && txt[i] == pat[j])
        {
            i++;
            j++;
        }
 
        // For step - (3)
        else if (j < m && pat[j] == '?')
        {
            i++;
            j++;
        }
 
        // For step - (4)
        else if (j < m && pat[j] == '*')
        {
            index_txt = i;
            index_pat = j;
            j++;
        }
 
        // For step - (5)
        else if (index_pat != -1)
        {
            j = index_pat + 1;
            i = index_txt + 1;
            index_txt++;
        }
 
        // For step - (6)
        else
        {
            return false;
        }
    }
 
    // For step - (7)
    while (j < m && pat[j] == '*')
    {
        j++;
    }
 
    // Final Check
    if (j == m)
    {
        return true;
    }
 
    return false;
}
 
// Driver code
int main()
{
    
    char str[] = "baaabab";
    char pattern[] = "*****ba*****ab";
    // char pattern[] = "ba*****ab";
    // char pattern[] = "ba*ab";
    // char pattern[] = "a*ab";
 
    if (strmatch(str, pattern,
                 strlen(str), strlen(pattern)))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
 
    char pattern2[] = "a*****ab";
    if (strmatch(str, pattern2,
                 strlen(str), strlen(pattern2)))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to replace c1 with c2
// and c2 with c1
#include <bits/stdc++.h>
using namespace std;
string replace(string s, char c1, char c2)
{
    int l = s.length();
 
    // loop to traverse in the string
    for (int i = 0; i < l; i++) {
 
        // check for c1 and replace
        if (s[i] == c1)
            s[i] = c2;
 
        // check for c2 and replace
        else if (s[i] == c2)
            s[i] = c1;
    }
    return s;
}
 
// Driver code to check the above function
int main()
{
    string s = "grrksfoegrrks";
    char c1 = 'e', c2 = 'r';
    cout << replace(s, c1, c2);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to calculate number of times
// the pattern occurred in given string
#include<iostream>
using namespace std;
 
// Returns count of occurrences of "1(0+)1"
// int str.
int countPattern(string str)
{
    int len = str.size();
    bool oneSeen = 0;
 
    int count = 0;  // Initialize result
    for (int i = 0; i < len ; i++)
    {
 
        // Check if encountered '1' forms a valid
        // pattern as specified
        if (str[i] == '1' && oneSeen == 1)
            if (str[i - 1] == '0')
                count++;
 
        // if 1 encountered for first time
        // set oneSeen to 1
        if (str[i] == '1' && oneSeen == 0)
           {
            oneSeen = 1;
            continue;
           }
        // Check if there is any other character
        // other than '0' or '1'. If so then set
        // oneSeen to 0 to search again for new
        // pattern
        if (str[i] != '0' && str[i] != '1')
            oneSeen = 0;
 
 
    }
 
    return count;
}
 
// Driver program to test above function
int main()
{
    string str = "100001abc101";
    cout << countPattern(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to in-place replace multiple
// occurrences of a pattern by character ‘X’
#include <bits/stdc++.h>
using namespace std;
 
// Function to in-place replace multiple
// occurrences of a pattern by character ‘X’
void replacePattern(string str, string pattern)
{
 
    // making an iterator for string str
    string::iterator it = str.begin();
    // run this loop until iterator reaches end of string
    while (it != str.end()) {
        // searching the first index in string str where
        // the first occurrence of string pattern occurs
        it = search(str.begin(), str.end(), pattern.begin(), pattern.end());
        // checking if iterator is not pointing to end of the
        // string str
        if (it != str.end()) {
            // erasing the full pattern string from that iterator
            // position in string str
            str.erase(it, it + pattern.size());
            // inserting 'X' at that iterator position
            str.insert(it, 'X');
        }
    }
 
    // this loop removes consecutive 'X' in string s
    // Example: GeeksGeeksforGeeks was changed to 'XXforX'
    // running this loop will change it to 'XforX'
    for (int i = 0; i < str.size() - 1; i++) {
        if (str[i] == 'X' && str[i + 1] == 'X') {
            // removing 'X' at position i in string str
            str.erase(str.begin() + i);
            i--; // i-- because one character was deleted
            // so repositioning i
        }
    }
    cout << str;
}
 
// Driver code
int main()
{
    string str = "GeeksforGeeks";
    string pattern = "Geeks";
 
    replacePattern(str, pattern);
 
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to find if a string follows order
// defined by a given pattern.
#include <bits/stdc++.h>
using namespace std;
 
const int CHAR_SIZE = 256;
 
// Returns true if characters of str follow
// order defined by a given ptr.
bool checkPattern(string str, string pat)
{
    // Initialize all orders as -1
    vector<int> label(CHAR_SIZE, -1);
 
    // Assign an order to pattern characters
    // according to their appearance in pattern
    int order = 1;
    for (int i = 0; i < pat.length() ; i++)
    {
        // give the pattern characters order
        label[pat[i]] = order;
 
        // increment the order
        order++;
    }
 
    //  Now one by check if string characters
    // follow above order
    int last_order = -1;
    for (int i = 0; i < str.length(); i++)
    {
        if (label[str[i]] != -1)
        {
            // If order of this character is less
            // than order of previous, return false.
            if (label[str[i]] < last_order)
                return false;
 
            // Update last_order for next iteration
            last_order =  label[str[i]];
        }
    }
 
    // return that str followed pat
    return true;
}
 
// Driver code
int main()
{
    string str = "engineers rock";
    string pattern = "gsr";
 
    cout << boolalpha << checkPattern(str, pattern);
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ code for finding count
// of string in a given 2D
// character array.
#include <bits/stdc++.h>
using namespace std;
 
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a))
 
// utility function to search
// complete string from any
// given index of 2d char array
int internalSearch(string needle, int row,
                   int col, string hay[],
                   int row_max, int col_max, int xx)
{
    int found = 0;
 
    if (row >= 0 && row <= row_max && col >= 0 &&
        col <= col_max && needle[xx] == hay[row][col])
    {
        char match = needle[xx];
        xx += 1;
 
        hay[row][col] = 0;
 
        if (needle[xx] == 0)
        {
            found = 1;
        }
        else
        {
 
            // through Backtrack searching
            // in every directions
            found += internalSearch(needle, row,
                                    col + 1, hay,
                                    row_max, col_max,xx);
            found += internalSearch(needle, row, col - 1,
                                    hay, row_max, col_max,xx);
            found += internalSearch(needle, row + 1, col,
                                    hay, row_max, col_max,xx);
            found += internalSearch(needle, row - 1, col,
                                    hay, row_max, col_max,xx);
        }
        hay[row][col] = match;
    }
    return found;
}
 
// Function to search the string in 2d array
int searchString(string needle, int row, int col,
                  string str[], int row_count,
                                int col_count)
{
    int found = 0;
    int r, c;
 
    for (r = 0; r < row_count; ++r)
    {
        for (c = 0; c < col_count; ++c)
        {
            found += internalSearch(needle, r, c, str,
                                    row_count - 1,
                                    col_count - 1, 0);
        }
    }
    return found;
}
 
// Driver code
int main()
{
    string needle = "MAGIC";
    string input[] = { "BBABBM",
                       "CBMBBA",
                       "IBABBG",
                       "GOZBBI",
                       "ABBBBC",
                       "MCIGAM" };
    string str[ARRAY_SIZE(input)];
    int i;
    for (i = 0; i < ARRAY_SIZE(input); ++i)
    {
        str[i] = input[i];
    }
 
    cout << "count: " << searchString(needle, 0, 0, str,
                                      ARRAY_SIZE(str),
                                      str[0].size()) << endl;
    return 0;
}
 
 
// This code is contributed by SHUBHAMSINGH8410

Time complexity: quadratic
Space complexity: quadratic
// C++ program to print words in a sentence
#include <bits/stdc++.h>
using namespace std;
 
void removeDupWord(string str)
{
    // Used to split string around spaces.
    istringstream ss(str);
 
    string word; // for storing each word
 
    // Traverse through all words
    // while loop till we get
    // strings to store in string word
    while (ss >> word)
    {
        // print the read word
        cout << word << "\n";
    }
}
 
// Driver code
int main()
{
    string str = "Geeks for Geeks";
    removeDupWord(str);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to check if we can break a string
// into four distinct strings.
#include<bits/stdc++.h>
using namespace std;
 
// Return if the given string can be split or not.
bool check(string s)
{
    // We can always break a string of size 10 or
    // more into four distinct strings.
    if (s.size() >= 10)
        return true;
 
    // Brute Force
    for (int i =1; i < s.size(); i++)
    {
        for (int j = i + 1; j < s.size(); j++)
        {
            for (int k = j + 1; k < s.size(); k++)
            {
                // Making 4 string from the given string
                string s1 = s.substr(0, i);
                string s2 = s.substr(i, j - i);
                string s3 = s.substr(j, k - j);
                string s4 = s.substr(k, s.size() - k);
 
                // Checking if they are distinct or not.
                if (s1 != s2 && s1 != s3 && s1 != s4 &&
                        s2 != s3 && s2 != s4 && s3 != s4)
                    return true;
            }
        }
    }
 
    return false;
}
 
// Driven Program
int main()
{
    string str = "aaabb";
 
    (check(str))? (cout << "Yes" << endl):
                  (cout << "No" << endl);
 
    return 0;
}

Time complexity: np
Space complexity: linear
// CPP program to split an alphanumeric
// string using STL
#include<bits/stdc++.h>
using namespace std;
 
void splitString(string str)
{
    string alpha, num, special;
    for (int i=0; i<str.length(); i++)
    {
        if (isdigit(str[i]))
            num.push_back(str[i]);
        else if((str[i] >= 'A' && str[i] <= 'Z') ||
                (str[i] >= 'a' && str[i] <= 'z'))
            alpha.push_back(str[i]);
        else
            special.push_back(str[i]);
    }
 
    cout << alpha << endl;
    cout << num << endl;
    cout << special << endl;
}
 
// Driver code
int main()
{
    string str = "geeks01$$for02geeks03!@!!";
    splitString(str);
    return 0;
}

Time complexity: linear
Space complexity: linear