Banana-Leopard/TASTY huggingface dataset:
// C++ program to get minimum cost to sort
// strings by reversal operation
#include <bits/stdc++.h>
using namespace std;
 
// Returns minimum cost for sorting arr[]
// using reverse operation. This function
// returns -1 if it is not possible to sort.
int minCost(string arr[], int cost[], int N)
{
    // dp[i][j] represents the minimum cost to
    // make first i strings sorted.
    // j = 1 means i'th string is reversed.
    // j = 0 means i'th string is not reversed.
    int dp[N][2];
 
    // initializing dp array for first string
    dp[0][0] = 0;
    dp[0][1] = cost[0];
 
    // getting array of reversed strings
    string revStr[N];
    for (int i = 0; i < N; i++)
    {
        revStr[i] = arr[i];
        reverse(revStr[i].begin(), revStr[i].end());
    }
 
    string curStr;
    int curCost;
 
    // looping for all strings
    for (int i = 1; i < N; i++)
    {
        // Looping twice, once for string and once
        // for reversed string
        for (int j = 0; j < 2; j++)
        {
            dp[i][j] = INT_MAX;
 
            // getting current string and current
            // cost according to j
            curStr = (j == 0) ? arr[i] : revStr[i];
            curCost = (j == 0) ? 0 : cost[i];
 
            // Update dp value only if current string
            // is lexicographically larger
            if (curStr >= arr[i - 1])
                dp[i][j] = min(dp[i][j], dp[i-1][0] + curCost);
            if (curStr >= revStr[i - 1])
                dp[i][j] = min(dp[i][j], dp[i-1][1] + curCost);
        }
    }
 
    // getting minimum from both entries of last index
    int res = min(dp[N-1][0], dp[N-1][1]);
 
    return (res == INT_MAX)? -1 : res;
}
 
// Driver code to test above methods
int main()
{
    string arr[] = {"aa", "ba", "ac"};
    int cost[] = {1, 3, 1};
    int N = sizeof(arr) / sizeof(arr[0]);
 
    int res = minCost(arr, cost, N);
    if (res == -1)
        cout << "Sorting not possible\n";
    else
        cout << "Minimum cost to sort strings is "
            << res;
}

Time complexity: linear
Space complexity: linear
// CPP program to print all number containing
// 1, 2 and 3 in any order.
#include <bits/stdc++.h>
using namespace std;
 
// convert the number to string and find
// if it contains 1, 2 & 3.
bool findContainsOneTwoThree(int number)
{
    string str = to_string(number);
    int countOnes = 0, countTwo = 0, countThree = 0;
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == '1')
            countOnes++;
        else if (str[i] == '2')
            countTwo++;
        else if (str[i] == '3')
            countThree++;
    }
    return (countOnes && countTwo && countThree);
}
// prints all the number containing 1, 2, 3
string printNumbers(int numbers[], int n)
{
    vector<int> oneTwoThree;
    for (int i = 0; i < n; i++) {
        // check if the number contains 1,
        // 2 & 3 in any order
        if (findContainsOneTwoThree(numbers[i]))
            oneTwoThree.push_back(numbers[i]);
    }
 
    // sort all the numbers
    sort(oneTwoThree.begin(), oneTwoThree.end());
 
    string result = "";
    for (auto number : oneTwoThree) {
        int value = number;
        if (result.length() > 0)
            result += ", ";
 
        result += to_string(value);
    }
 
    return (result.length() > 0) ? result : "-1";
}
 
// Driver Code
int main()
{
    int numbers[] = { 123, 1232, 456, 234, 32145 };
 
    int n = sizeof(numbers) / sizeof(numbers[0]);
 
    string result = printNumbers(numbers, n);
    cout << result;
    return 0;
}
// This code is contributed
// by Sirjan13

Time complexity: nlogn
Space complexity: linear
// C++ program to implement binary search
// in a sparse array.
#include <bits/stdc++.h>
using namespace std;
 
/* Binary Search in an array with blanks */
int binarySearch(string *arr, int low, int high, string x) {
  if (low > high)
    return -1;
 
  int mid = (low + high) / 2;
 
  /*Modified Part*/
  if (arr[mid] == "") {
    int left = mid - 1;
    int right = mid + 1;
 
    /*Search for both side for a non empty string*/
    while (1) {
 
      /* No non-empty string on both sides */
      if (left < low && right > high)
        return -1;
 
      if (left >= low && arr[left] != "") {
        mid = left;
        break;
      }
 
      else if (right <= high && arr[right] != "") {
        mid = right;
        break;
      }
 
      left--;
      right++;
    }
  }
 
  /* Normal Binary Search */
  if (arr[mid] == x)
    return mid;
  else if (arr[mid] > x)
    return binarySearch(arr, low, mid - 1, x);
  else
    return binarySearch(arr, mid + 1, high, x);
}
 
int sparseSearch(string arr[], string x, int n) {
  return binarySearch(arr, 0, n - 1, x);
}
 
int main() {
  ios_base::sync_with_stdio(false);
 
  string arr[] = {"for", "geeks", "", "", "", "", "ide",
                      "practice", "", "", "", "quiz"};
  string x = "geeks";
  int n = sizeof(arr) / sizeof(arr[0]);
  int index = sparseSearch(arr, x, n);
  if (index != -1)
    cout << x << " found at index " << index << "\n";
  else
    cout << x << " not found\n";
  return 0;
}

Time complexity: logn
Space complexity: logn
// C++ program to toggle cases of a given
// string.
#include <iostream>
#include <cstring>
using namespace std;
 
// function to toggle cases of a string
void toggle(string& str)
{
    int length = str.length();
    for (int i = 0; i < length; i++) {
        int c = str[i];
        if (islower(c))
            str[i] = toupper(c);
        else if (isupper(c))
            str[i] = tolower(c);       
    }
}
 
// Driver Code
int main()
{
    string str = "GeekS";
    toggle(str);
    cout << str;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to convert a string from
// lower to upper case.
#include<stdio.h>
 
const int x = 32;
 
// Converts a string to uppercase
char *toUpperCase(char *a)
{
    for (int i=0; a[i]!='\0'; i++)
        a[i] = a[i] & ~x;
 
    return a;
}
 
// Driver Code
int main()
{
    char str[] = "SanjaYKannA";
 
    //Here it's recommended to use character array
    //as it's stored in read-write area.
    //If a pointer is used it's stored
    //in read-only memory as a string literal.
 
    printf("%s", toUpperCase(str));
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to convert a string from
// upper to lower case.
#include<stdio.h>
const int x = 32;
 
// Converts a string to lowercase
char * toLowerCase(char *a)
{
    for (int i=0; a[i]!='\0'; i++)
        a[i] = a[i] | x;
 
    return a;
}
 
// Driver Code
int main()
{
    char str[] = "SanjaYKannA";
    printf("%s", toLowerCase(str));
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP Program to find maximum
// lowercase alphabets present
// between two uppercase alphabets
#include <bits/stdc++.h>
using namespace std;
 
#define MAX_CHAR 26
 
// Function which computes the
// maximum number of distinct
// lowercase alphabets between
// two uppercase alphabets
int maxLower(string str)
{
    int n = str.length();
 
    // Ignoring lowercase characters in the
    // beginning.
    int i = 0;
    for (; i < n; i++) {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            i++;
            break;
        }
    }
 
    // We start from next of first capital letter
    // and traverse through remaining character.
    int maxCount = 0;
    int count[MAX_CHAR] = { 0 };
    for (; i < n; i++) {
 
        // If character is in uppercase,
        if (str[i] >= 'A' && str[i] <= 'Z') {
 
            // Count all distinct lower case
            // characters
            int currCount = 0;
            for (int j = 0; j < MAX_CHAR; j++)
                if (count[j] > 0)
                    currCount++;
 
            // Update maximum count
            maxCount = max(maxCount, currCount);
 
            // Reset count array
            memset(count, 0, sizeof(count));
        }
 
        // If character is in lowercase
        if (str[i] >= 'a' && str[i] <= 'z')
            count[str[i] - 'a']++;
    }
 
    return maxCount;
}
 
// Driver function
int main()
{
    string str = "zACaAbbaazzC";
    cout << maxLower(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP Program to find maximum
// lowercase alphabets present
// between two uppercase alphabets
#include <bits/stdc++.h>
using namespace std;
 
// Function which computes the
// maximum number of distinct
// lowercase alphabets between
// two uppercase alphabets
int maxLower(string str)
{
    int n = str.length();
 
    // Ignoring lowercase characters in the
    // beginning.
    int i = 0;
    for (; i < n; i++) {
        if (str[i] >= 'A' && str[i] <= 'Z') {
            i++;
            break;
        }
    }
 
    // We start from next of first capital letter
    // and traverse through remaining character.
    int maxCount = 0;
    unordered_set<int> s;
    for (; i < n; i++) {
 
        // If character is in uppercase,
        if (str[i] >= 'A' && str[i] <= 'Z') {
 
            // Update maximum count if lowercase
            // character before this is more.
            maxCount = max(maxCount, (int)s.size());
 
            // clear the set
            s.clear();
        }
 
        // If character is in lowercase
        if (str[i] >= 'a' && str[i] <= 'z')
            s.insert(str[i]);
    }
 
    return maxCount;
}
 
// Driver function
int main()
{
    string str = "zACaAbbaazzC";
    cout << maxLower(str);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to convert a
// sentence to gOOGLE cASE.
#include <bits/stdc++.h>
using namespace std;
 
string convert(string str)
{
     
    // Empty strings
    string w = "", z = "";
 
    // Convert input string to upper case
    transform(str.begin(), str.end(),
              str.begin(), ::toupper);
    str += " ";
    for(int i = 0; i < str.length(); i++)
    {
        
       // Check if character is not a space
       // and adding it to string w
       char ch = str[i];
       if (ch != ' ')
       {
           w = w + ch;
       }
       else
       {
            
           // Converting first character
           // to lower case and subsequent
           // initial letter of another
           // word to lower case
           z = z + char(tolower(w[0])) +
                           w.substr(1) + " ";
           w = "";
       }
    }
    return z;
}
 
// Driver code
int main()
{
    string str = "I got intern at geeksforgeeks";
     
    cout << convert(str) << endl;
    return 0;
}
 
// This code is contributed by rutvik_56

Time complexity: linear
Space complexity: linear
// CPP program to convert given
// sentence to camel case.
#include <bits/stdc++.h>
using namespace std;
 
// Function to remove spaces and
// convert into camel case
string convert(string s)
{
    int n = s.length();
    s[0] = tolower(s[0]);
 
    for (int i = 1; i < n; i++)
    {
 
        // check for spaces in the sentence
        if (s[i] == ' ' && i < n)
        {
 
            // conversion into upper case
            s[i + 1] = tolower(s[i + 1]);
            i++;
        }
 
        // If not space, copy character
        else
            s[i] = toupper(s[i]);
    }
 
    // return string to main
    return s;
}
 
// Driver Code
int main()
{
    string str = "I get intern at geeksforgeeks";
    cout << convert(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to convert given sentence
/// to camel case.
#include <bits/stdc++.h>
using namespace std;
 
// Function to remove spaces and convert
// into camel case
string convert(string s)
{
    int n = s.length();
 
    int res_ind = 0;
 
    for (int i = 0; i < n; i++) {
 
        // check for spaces in the sentence
        if (s[i] == ' ') {
 
            // conversion into upper case
            s[i + 1] = toupper(s[i + 1]);
            continue;
        }
 
        // If not space, copy character
        else
            s[res_ind++] = s[i];        
    }
 
    // return string to main
    return s.substr(0, res_ind);
}
 
// Driver program
int main()
{
    string str = "I get intern at geeksforgeeks";
    cout << convert(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP code to print all permutations
// with respect to cases
#include <bits/stdc++.h>
using namespace std;
 
// Function to generate permutations
void permute(string ip, string op)
{
    // base case
    if (ip.size() == 0) {
        cout << op << " ";
        return;
    }
    // pick lower and uppercase
    char ch = tolower(ip[0]);
    char ch2 = toupper(ip[0]);
    ip = ip.substr(1);
 
    permute(ip, op + ch);
    permute(ip, op + ch2);
}
 
// Driver code
int main()
{
    string ip = "aB";
    permute(ip, "");
    return 0;
}

Time complexity: np
Space complexity: linear
// CPP code to print all permutations
// with respect to cases
#include <bits/stdc++.h>
using namespace std;
 
// Function to generate permutations
void permute(string input)
{
    int n = input.length();
 
    // Number of permutations is 2^n
    int max = 1 << n;
 
    // Converting string to lower case
    transform(input.begin(), input.end(), input.begin(),
              ::tolower);
    // Using all subsequences and permuting them
    for (int i = 0; i < max; i++) {
 
        // If j-th bit is set, we convert it to upper case
        string combination = input;
        for (int j = 0; j < n; j++)
            if (((i >> j) & 1) == 1)
                combination[j] = toupper(input.at(j));
 
        // Printing current combination
        cout << combination << " ";
    }
}
 
// Driver code
int main()
{
    permute("ABC");
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to get toggle case of a string
#include <bits/stdc++.h>
using namespace std;
 
// tOGGLE cASE = swaps CAPS to lower
// case and lower case to CAPS
char *toggleCase(char *a)
{
    for(int i = 0; a[i] != '\0'; i++)
    {
         
        // Bitwise EXOR with 32
        a[i] ^= 32;
    }
    return a;
}
 
// Driver Code
int main()
{
    char str[] = "CheRrY";
    cout << "Toggle case: "
         << toggleCase(str) << endl;
    cout << "Original string: "
         << toggleCase(str) << endl;
    return 0;
}
 
// This code is contributed by noob2000

Time complexity: linear
Space complexity: constant
// C++ program to print all permutations
// with repetition of characters
#include <bits/stdc++.h>
#include<string.h>
using namespace std;
 
 
/* Following function is used by
the library qsort() function
to sort an array of chars */
int compare (const void * a, const void * b);
 
/* The main function that recursively
prints all repeated permutations of
the given string. It uses data[] to store all
permutations one by one */
void allLexicographicRecur (char *str, char* data,
                            int last, int index)
{
    int i, len = strlen(str);
 
    // One by one fix all characters at
    // the given index and recur for
    // the/ subsequent indexes
    for ( i = 0; i < len; i++ )
    {
        // Fix the ith character at index
        // and if this is not the last
        // index then recursively call
        // for higher indexes
        data[index] = str[i] ;
 
        // If this is the last index then
        // print the string stored in
        // data[]
        if (index == last)
            cout << data << endl;
        else // Recur for higher indexes
            allLexicographicRecur (str, data, last, index+1);
    }
}
 
/* This function sorts input string,
allocate memory for data (needed
for allLexicographicRecur()) and
calls allLexicographicRecur() for
printing all permutations */
void allLexicographic(char *str)
{
    int len = strlen (str) ;
 
    // Create a temp array that will
    // be used by allLexicographicRecur()
    char *data = (char *) malloc (sizeof(char) * (len + 1)) ;
    data[len] = '\0';
 
    // Sort the input string so that
    // we get all output strings in
    // lexicographically sorted order
    qsort(str, len, sizeof(char), compare);
 
    // Now print all permutations
    allLexicographicRecur (str, data, len-1, 0);
 
    // Free data to avoid memory leak
    free(data);
}
 
// Needed for library function qsort()
int compare (const void * a, const void * b)
{
    return ( *(char *)a - *(char *)b );
}
 
// Driver code
int main()
{
    char str[] = "ABC";
    cout << "All permutations with repetition of "<<
                                str <<" are: "<<endl ;
    allLexicographic(str);
    return 0;
}
 
// This code is contributed by rathbhupendra

Time complexity: np
Space complexity: linear
// C++ program to check if a string can be converted to
// a string that has repeated substrings of length k.
#include<bits/stdc++.h>
using namespace std;
 
// Returns true if str can be converted to a string
// with k repeated substrings after replacing k
// characters.
bool checkString(string str, long k)
{
    // Length of string must be a multiple of k
    int n = str.length();
    if (n%k != 0)
        return false;
 
    // Map to store strings of length k and their counts
    unordered_map<string, int> mp;
    for (int i=0; i<n; i+=k)
        mp[str.substr(i, k)]++;
 
    // If string is already a repetition of k substrings,
    // return true.
    if (mp.size() == 1)
        return true;
 
    // If number of distinct substrings is not 2, then
    // not possible to replace a string.
    if (mp.size() != 2)
        return false;
 
    // One of the two distinct must appear exactly once.
    // Either the first entry appears once, or it appears
    // n/k-1 times to make other substring appear once.
    if ((mp.begin()->second == (n/k - 1)) ||
                    mp.begin()->second == 1)
       return true;
 
    return false;
}
 
// Driver code
int main()
{
    checkString("abababcd", 2)? cout << "Yes" :
                                cout << "No";
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find smallest possible length
// of a string of only three characters
#include<bits/stdc++.h>
using namespace std;
 
// Program to find length of reduced string
// in a string made of three characters.
#define MAX_LEN 110
 
// To store results of subproblems
int DP[MAX_LEN][MAX_LEN][MAX_LEN];
 
// A memoized function find result recursively.
// a, b and c are counts of 'a's, 'b's and
// 'c's in str
int length(int a, int b, int c)
{
    // If this subproblem is already evaluated
    if(a < 0 or b < 0 or c < 0)
        if (DP[a][b] != -1)
            return DP[a][b];
 
    // If there is only one type of character
    if (a == 0 && b == 0)
        return (DP[a][b] = c);
    if (a == 0 && c == 0)
        return (DP[a][b] = b);
    if (b == 0 && c == 0)
        return (DP[a][b] = a);
 
    // If only two types of characters are present
    if (a == 0)
        return (DP[a][b] =
                    length(a + 1, b - 1, c - 1));
    if (b == 0)
        return (DP[a][b] =
                    length(a - 1, b + 1, c - 1));
    if (c == 0)
        return (DP[a][b] =
                    length(a - 1, b - 1, c + 1));
 
    // If all types of characters are present.
    // Try combining all pairs.
    return (DP[a][b] =
                min(length(a - 1, b - 1, c + 1),
                    min(length(a - 1, b + 1, c - 1),
                        length(a + 1, b - 1, c - 1))));
}
 
// Returns smallest possible length with given
// operation allowed.
int stringReduction(string str)
{
    int n = str.length();
 
    // Counting occurrences of three different
    // characters 'a', 'b' and 'c' in str
    int count[3] = {0};
    for (int i=0; i<n; ++i)
        count[str[i]-'a']++;
 
    // Initialize DP[][] entries as -1
    for (int i = 0; i <= count[0]; ++i)
        for (int j = 0; j < count[1]; ++j)
            for (int k = 0; k < count[2]; ++k)
                DP[i][j][k] = -1;
 
    return length(count[0], count[1], count[2]);
}
 
// Driver code
int main()
{
    string str = "abcbbaacb";
    cout << stringReduction(str);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find smallest possible length
// of a string of only three characters
#include<bits/stdc++.h>
using namespace std;
 
// Returns smallest possible length with given
// operation allowed.
int stringReduction(string str)
{
    int n = str.length();
 
    // Counint occurrences of three different
    // characters 'a', 'b' and 'c' in str
    int count[3] = {0};
    for (int i=0; i<n; ++i)
        count[str[i]-'a']++;
 
    // If all characters are same.
    if (count[0] == n || count[1] == n ||
        count[2] == n)
        return n;
 
    // If all characters are present even number
    // of times or all are present odd number of
    // times.
    if ((count[0] % 2) == (count[1] % 2) &&
        (count[1] % 2) == (count[2] % 2))
        return 2;
 
    // Answer is 1 for all other cases.
    return 1;
}
 
// Driver code
int main()
{
    string str = "abcbbaacb";
    cout << stringReduction(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP program to find if its possible to
// distribute balls without repetition
#include <bits/stdc++.h>
using namespace std;
 
const int MAX_CHAR = 26;
 
// function to find if its possible to
// distribute balls or not
bool distributingBalls(int k, int n, string str)
{  
    // count array to count how many times
    // each color has occurred
    int a[MAX_CHAR] = {0};
    for (int i = 0; i < n; i++){
         
        // increasing count of each color
        // every time it appears
        a[str[i] - 'a']++; 
    }
     
    for (int i = 0; i < MAX_CHAR; i++)  
 
        // to check if any color appears
        // more than K times if it does
        // we will print NO
        if (a[i] > k)
          return false;
 
    return true;
}
 
// Driver code
int main()
{
    long long int n = 6, k = 3;
    string str = "aacaab";
 
    if (distributingBalls(k, n, str))
        cout << "YES";
    else
        cout << "NO";
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find the maximum consecutive
// repeating character in given string
#include<bits/stdc++.h>
using namespace std;
 
// function to find out the maximum repeating
// character in given string
char maxRepeating(string str)
{
    int len = str.length();
    int count = 0;
 
    // Find the maximum repeating character
    // starting from str[i]
    char res = str[0];
    for (int i=0; i<len; i++)
    {
        int cur_count = 1;
        for (int j=i+1; j<len; j++)
        {
            if (str[i] != str[j])
                break;
            cur_count++;
        }
 
        // Update result if required
        if (cur_count > count)
        {
            count = cur_count;
            res = str[i];
        }
    }
    return res;
}
 
// Driver code
int main()
{
 
    string str = "aaaabbaaccde";
    cout << maxRepeating(str);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program to find the maximum consecutive
// repeating character in given string
#include<bits/stdc++.h>
using namespace std;
 
// Returns the maximum repeating character in a
// given string
char maxRepeating(string str)
{
    int n = str.length();
    int count = 0;
    char res = str[0];
    int cur_count = 1;
 
    // Traverse string except last character
    for (int i=0; i<n; i++)
    {
        // If current character matches with next
        if (i < n-1 && str[i] == str[i+1])
            cur_count++;
 
        // If doesn't match, update result
        // (if required) and reset count
        else
        {
            if (cur_count > count)
            {
                count = cur_count;
                res = str[i];
            }
            cur_count = 1;
        }
    }
 
    return res;
}
 
// Driver code
int main()
{
    string str = "aaaabbaaccde";
    cout << maxRepeating(str);
    return 0;
}

Time complexity: linear
Space complexity: constant