Banana-Leopard/TASTY huggingface dataset:
#include <iostream>
#include <map>
#include <set>
#include <string>
 
int min(int a, int b) { return a < b ? a : b; }
 
using namespace std;
 
bool have_same_frequency(map<char, int>& freq, int k)
{
    for (auto& pair : freq) {
        if (pair.second != k && pair.second != 0) {
            return false;
        }
    }
    return true;
}
 
int count_substrings(string s, int k)
{
    int count = 0;
    int distinct = (set<char>(s.begin(), s.end())).size();
    for (int length = 1; length <= distinct; length++) {
        int window_length = length * k;
        map<char, int> freq;
        int window_start = 0;
        int window_end = window_start + window_length - 1;
        for (int i = window_start;
             i <= min(window_end, s.length() - 1); i++) {
            freq[s[i]]++;
        }
        while (window_end < s.length()) {
            if (have_same_frequency(freq, k)) {
                count++;
            }
            freq[s[window_start]]--;
            window_start++;
            window_end++;
            if (window_length < s.length()) {
                freq[s[window_end]]++;
            }
        }
    }
    return count;
}
 
int main()
{
    string s = "aabbcc";
    int k = 2;
    cout << count_substrings(s, k) << endl;
    s = "aabbc";
    k = 2;
    cout << count_substrings(s, k) << endl;
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// CPP program to construct a n length string
// with k distinct characters such that no two
// same characters are adjacent.
#include <iostream>
using namespace std;
 
// Function to find a string of length
// n with k distinct characters.
string findString(int n, int k)
{
    // Initialize result with first k
    // Latin letters
    string res = "";
    for (int i = 0; i < k; i++)
        res = res + (char)('a' + i);
 
    // Fill remaining n-k letters by
    // repeating k letters again and again.
    int count = 0;
    for (int i = 0; i < n - k; i++) {
        res = res + (char)('a' + count);
        count++;
        if (count == k)
            count = 0;
    }
    return res;
}
 
// Driver code
int main()
{
    int n = 5, k = 2;
    cout << findString(n, k);
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to count number of substrings
// of a string
#include <bits/stdc++.h>
using namespace std;
 
int countNonEmptySubstr(string str)
{
   int n = str.length();
   return n*(n+1)/2;
}
 
// driver code
int main()
{
    string s = "abcde";
    cout << countNonEmptySubstr(s);
    return 0;
}

Time complexity: constant
Space complexity: constant
#include<bits/stdc++.h>
using namespace std;
 
int MAX_CHAR = 26;
 
string encodeString(char str[], int m) {
    // hashEven stores the count of even indexed character
    // for each string hashOdd stores the count of odd
    // indexed characters for each string
    int hashEven[MAX_CHAR];
    int hashOdd[MAX_CHAR];
 
    memset(hashEven,0,sizeof(hashEven));
    memset(hashOdd,0,sizeof(hashOdd));
    // creating hash for each string
    for (int i = 0; i < m; i++) {
        char c = str[i];
        if ((i & 1) != 0) // If index of current character is odd
            hashOdd[c-'a']++;
        else
            hashEven[c-'a']++;
 
    }
 
 
    // For every character from 'a' to 'z', we store its
    // count at even position followed by a separator,
    // followed by count at odd position.
    string encoding = "";
    for (int i = 0; i < MAX_CHAR; i++) {
        encoding += (hashEven[i]);
        encoding += ('-');
        encoding += (hashOdd[i]);
        encoding += ('-');
    }
    return encoding;
}
 
// This function basically uses a hashing based set to
// store strings which are distinct according
// to criteria given in question.
int countDistinct(string input[], int n) {
    int countDist = 0; // Initialize result
 
    // Create an empty set and store all distinct
    // strings in it.
    set<string> s;
    for (int i = 0; i < n; i++) {
        // If this encoding appears first time, increment
        // count of distinct encodings.
        char char_array[input[i].length()];
        strcpy(char_array, input[i].c_str());
        if (s.find(encodeString(char_array, input[i].length())) == s.end()) {
            s.insert(encodeString(char_array,input[i].length()));
            countDist++;
        }
    }
 
    return countDist;
}
 
int main() {
    string input[] = {"abcd", "acbd", "adcb", "cdba",
            "bcda", "badc"};
    int n = sizeof(input)/sizeof(input[0]);
 
    cout << countDistinct(input, n) << "\n";
}
 
// This code is contributed by Harshit Sharma.

Time complexity: linear
Space complexity: constant
// C++ program to find K'th character in
// decrypted string
#include<bits/stdc++.h>
using namespace std;
  
// Function to find K'th character in Encoded String
char encodedChar(string str,int k)
{
    // expand string variable is used to
    // store final string after decompressing string str
    string expand = "";
  
    string temp;  // Current substring
    int freq = 0; // Count of current substring
  
    for (int i=0; str[i]!='\0'; )
    {
        temp = ""; // Current substring
        freq = 0; // count frequency of current substring
  
        // read characters until you find a number
        // or end of string
        while (str[i]>='a' && str[i]<='z')
        {
            // push character in temp
            temp.push_back(str[i]);
            i++;
        }
  
        // read number for how many times string temp
        // will be repeated in decompressed string
        while (str[i]>='1' && str[i]<='9')
        {
            // generating frequency of temp
            freq = freq*10 + str[i] - '0';
            i++;
        }
  
        // now append string temp into expand
        // equal to its frequency
        for (int j=1; j<=freq; j++)
            expand.append(temp);
    }
  
    // this condition is to handle the case
    // when string str is ended with alphabets
    // not with numeric value
    if (freq==0)
        expand.append(temp);
  
    return expand[k-1];
}
  
// Driver program to test the string
int main()
{
    string str = "ab4c12ed3";
    int k = 21;
    cout << encodedChar(str, k) << endl;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find number of characters at same
// position as in English alphabets
#include<bits/stdc++.h>
using namespace std;
 
int findCount(string str)
{
    int result = 0;
 
    // Traverse input string
    for (int i = 0 ; i < str.size(); i++)
 
        // Check that index of characters of string is
        // same as of English alphabets by using ASCII
        // values and the fact that all lower case
        // alphabetic characters come together in same
        // order in ASCII table.  And same is true for
        // upper case.
        if (i == (str[i] - 'a') || i == (str[i] - 'A'))
            result++;
 
 
    return result;
}
 
// Driver code
int main()
{
    string str = "AbgdeF";
    cout << findCount(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to count words  whose ith letter
// is either (i-1)th, ith, or (i+1)th letter
// of given word.
#include<bits/stdc++.h>
using namespace std;
  
// Return the count of words.
int countWords(char str[], int len)
{
    int count = 1;
  
    // If word contain single letter, return 1.
    if (len == 1)
        return count;
  
    // Checking for first letter.
    if (str[0] == str[1])
        count *= 1;
    else
        count *= 2;
  
    // Traversing the string and multiplying
    // for combinations.
    for (int j=1; j<len-1; j++)
    {
        // If all three letters are same.
        if (str[j] == str[j-1] && str[j] == str[j+1])
            count *= 1;
  
        // If two letter are distinct.
        else if (str[j] == str[j-1] ||
                 str[j] == str[j+1] ||
                 str[j-1] == str[j+1])
            count *= 2;
  
        // If all three letter are distinct.
        else
            count *= 3;
    }
  
    // Checking for last letter.
    if (str[len - 1] == str[len - 2])
        count *= 1;
    else
        count *= 2;
  
    return count;
}
  
// Driven Program
int main()
{
    char str[] = "abc";
    int len = strlen(str);
  
    cout << countWords(str, len) << endl;
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP Program to find Smallest and
// Largest Word in a String
#include<iostream>
#include<cstring>
using namespace std;
 
void minMaxLengthWords(string input, string &minWord, string &maxWord)
{
    // minWord and maxWord are received by reference
    // and not by value
    // will be used to store and return output
    int len = input.length();
    int si = 0, ei = 0;
   
   
    int min_length = len, min_start_index = 0, max_length = 0, max_start_index = 0;
 
    // Loop while input string is not empty
    while (ei <= len)
    {
        if (ei < len && input[ei] != ' ')
            ei++;
         
        else
        {
            // end of a word
            // find curr word length
            int curr_length = ei - si;
         
            if (curr_length < min_length)
            {
                min_length = curr_length;
                min_start_index = si;
            }
             
            if (curr_length > max_length)
            {
                max_length = curr_length;
                max_start_index = si;
            }
            ei++;
            si = ei;
        }
    }
     
    // store minimum and maximum length words
    minWord = input.substr(min_start_index, min_length);
    maxWord = input.substr(max_start_index, max_length);
}
 
// Driver code
int main()
{
    string a = "GeeksforGeeks A Computer Science portal for Geeks";
    string minWord, maxWord;
    minMaxLengthWords(a, minWord, maxWord);
     
    // to take input in string use getline(cin, a);
    cout << "Minimum length word: "
        << minWord << endl
        << "Maximum length word: "
        << maxWord << endl;
}

Time complexity: linear
Space complexity: linear
// Most efficient C++ program to count all 
// substrings with same first and last characters.
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;  // assuming lower case only
 
int countSubstringWithEqualEnds(string s)
{
    int result = 0;
    int n = s.length();
 
    // Calculating frequency of each character
    // in the string.
    int count[MAX_CHAR] = {0};
    for (int i=0; i<n; i++)
        count[s[i]-'a']++;
 
    // Computing result using counts
    for (int i=0; i<MAX_CHAR; i++)
        result += (count[i]*(count[i]+1)/2);
 
    return result;
}
 
// Driver function
int main()
{
    string s("abcab");
    cout << countSubstringWithEqualEnds(s);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to count number of possible strings
// with n characters.
#include<bits/stdc++.h>
using namespace std;
 
// Function to calculate number of strings
int possibleStrings( int n, int r, int b, int g)
{
    // Store factorial of numbers up to n
    // for further computation
    int fact[n+1];
    fact[0] = 1;
    for (int i = 1; i <= n; i++)
        fact[i] = fact[i-1] * i;
 
    // Find the remaining values to be added
    int left = n - (r+g+b);
    int sum = 0;
 
    // Make all possible combinations
    // of R, B and G for the remaining value
    for (int i = 0; i <= left; i++)
    {
        for (int j = 0; j<= left-i; j++)
        {
            int k = left - (i+j);
 
            // Compute permutation of each combination
            // one by one and add them.
            sum = sum + fact[n] /
                       (fact[i+r]*fact[j+b]*fact[k+g]);
        }
    }
 
    // Return total no. of strings/permutation
    return sum;
}
 
// Drivers code
int main()
{
    int n = 4, r = 2;
    int b = 0, g = 1;
    cout << possibleStrings(n, r, b, g);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ program to count number of strings
// of n characters with
#include<bits/stdc++.h>
using namespace std;
 
// n is total number of characters.
// bCount and cCount are counts of 'b'
// and 'c' respectively.
int countStrUtil(int dp[][2][3], int n, int bCount=1,
                 int cCount=2)
{
    // Base cases
    if (bCount < 0 || cCount < 0) return 0;
    if (n == 0) return 1;
    if (bCount == 0 && cCount == 0) return 1;
 
    // if we had saw this combination previously
    if (dp[n][bCount][cCount] != -1)
        return dp[n][bCount][cCount];
 
    // Three cases, we choose, a or b or c
    // In all three cases n decreases by 1.
    int res = countStrUtil(dp, n-1, bCount, cCount);
    res += countStrUtil(dp, n-1, bCount-1, cCount);
    res += countStrUtil(dp, n-1, bCount, cCount-1);
 
    return (dp[n][bCount][cCount] = res);
}
 
// A wrapper over countStrUtil()
int countStr(int n)
{
    int dp[n+1][2][3];
    memset(dp, -1, sizeof(dp));
    return countStrUtil(dp, n);
}
 
// Driver code
int main()
{
    int n = 3; // Total number of characters
    cout << countStr(n);
    return 0;
}

Time complexity: linear
Space complexity: linear
// A O(1) CPP program to find number of strings
// that can be made under given constraints.
#include<bits/stdc++.h>
using namespace std;
int countStr(int n){
     
    int count = 0;
     
    if(n>=1){
        //aaa...
        count += 1;
        //b...aaa...
          count += n;
        //c...aaa...
        count += n;
         
        if(n>=2){
          //bc...aaa...
          count += n*(n-1);
          //cc...aaa...
          count += n*(n-1)/2;
           
          if(n>=3){
            //bcc...aaa...
            count += (n-2)*(n-1)*n/2;
          }
        }
     
    }
     
    return count;
     
}
 
// Driver code
int main()
{
  int n = 3;
  cout << countStr(n);
  return 0;
}

Time complexity: constant
Space complexity: constant
// C++ program to find count of substring containing
// exactly K ones
#include <bits/stdc++.h>
using namespace std;
 
// method returns total number of substring having K ones
int countOfSubstringWithKOnes(string s, int K)
{
    int N = s.length();
    int res = 0;
    int countOfOne = 0;
    int freq[N + 1] = {0};
 
    // initialize index having zero sum as 1
    freq[0] = 1;
 
    // loop over binary characters of string
    for (int i = 0; i < N; i++) {
 
        // update countOfOne variable with value
        // of ith character
        countOfOne += (s[i] - '0');
 
        // if value reaches more than K, then
        // update result
        if (countOfOne >= K) {
 
            // add frequency of indices, having
            // sum (current sum - K), to the result
            res += freq[countOfOne - K];
        }
 
        // update frequency of one's count
        freq[countOfOne]++;
    }
    return res;
}
 
// Driver code to test above methods
int main()
{
    string s = "10010";
    int K = 1;
    cout << countOfSubstringWithKOnes(s, K) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to print common characters
// of two Strings in alphabetical order
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    string s1 = "geeksforgeeks";
    string s2 = "practiceforgeeks";
     
    // to store the count of
    // letters in the first string
    int a1[26] = {0};
     
    // to store the count of
    // letters in the second string
    int a2[26] = {0};
    int i , j;
    char ch;
    char ch1 = 'a';
    int k = (int)ch1, m;
     
    // for each letter present, increment the count
    for(i = 0 ; i < s1.length() ; i++)
    {
        a1[(int)s1[i] - k]++;
    }
    for(i = 0 ; i < s2.length() ; i++)
    {
        a2[(int)s2[i] - k]++;
    }
 
    for(i = 0 ; i < 26 ; i++)
    {
        // the if condition guarantees that
        // the element is common, that is,
        // a1[i] and a2[i] are both non zero
        // means that the letter has occurred
        // at least once in both the strings
        if (a1[i] != 0 and a2[i] != 0)
        {
            // print the letter for a number
            // of times that is the minimum
            // of its count in s1 and s2
            for(j = 0 ; j < min(a1[i] , a2[i]) ; j++)
            {
                m = k + i;
                ch = (char)(k + i);
                cout << ch;
            }
        }
    }
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP Program to find all the common characters
// in n strings
#include <bits/stdc++.h>
using namespace std;
 
const int MAX_CHAR = 26;
 
void commonCharacters(string str[], int n)
{
    // primary array for common characters
    // we assume all characters are seen before.
    bool prim[MAX_CHAR];
    memset(prim, true, sizeof(prim));
 
    // for each string
    for (int i = 0; i < n; i++) {
 
        // secondary array for common characters
        // Initially marked false
        bool sec[MAX_CHAR] = { false };
 
        // for every character of ith string
        for (int j = 0; str[i][j]; j++) {
 
            // if character is present in all
            // strings before, mark it.
            if (prim[str[i][j] - 'a'])
                sec[str[i][j] - 'a'] = true;
        }
 
        // copy whole secondary array into primary
        memcpy(prim, sec, MAX_CHAR);
    }
 
    // displaying common characters
    for (int i = 0; i < 26; i++)
        if (prim[i])
            printf("%c ", i + 'a');
}
 
// Driver's Code
int main()
{
    string str[] = { "geeksforgeeks",
                    "gemkstones",
                    "acknowledges",
                    "aguelikes" };
    int n = sizeof(str)/sizeof(str[0]);
    commonCharacters(str, n);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ implementation to find the uncommon
// characters of the two strings
#include <bits/stdc++.h>
using namespace std;
 
// function to find the uncommon characters
// of the two strings
void findAndPrintUncommonChars(string str1, string str2)
{
    // to store the answer
    string ans = "";
     
      // to handle the case of duplicates
    vector<int> used(26, false);
 
    // check first for str1
    for (int i = 0; i < str1.size(); i++) {
        // keeping a flag variable
        bool found = false;
 
        for (int j = 0; j < str2.size(); j++) {
            // if found change the flag
            // and break from loop
            if (str1[i] == str2[j]) {
                found = true;
                break;
            }
        }
 
        // if duplicate character not found
        // then add it to ans
        if (!found and !used[str1[i] - 'a']) {
            used[str1[i] - 'a'] = true;
            ans += str1[i];
        }
    }
 
    // now check for str2
    for (int i = 0; i < str2.size(); i++) {
        // keeping a flag variable
        bool found = false;
 
        for (int j = 0; j < str1.size(); j++) {
            // if found change the flag
            // and break from loop
            if (str2[i] == str1[j]) {
                found = true;
                break;
            }
        }
 
        // if duplicate character not found
        // then add it to ans
        if (!found and !used[str2[i] - 'a']) {
            used[str2[i] - 'a'] = true;
            ans += str2[i];
        }
    }
 
    // to match with output
    sort(ans.begin(), ans.end());
 
      // if not found any character
    if (ans.size() == 0)
        cout << "-1";
     
      // else print the answer
      else
        cout << ans << " ";
}
 
// Driver program to test above
int main()
{
    string str1 = "characters";
    string str2 = "alphabets";
    findAndPrintUncommonChars(str1, str2);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ implementation to find the uncommon
// characters of the two strings
#include <bits/stdc++.h>
using namespace std;
 
// size of the hash table
const int MAX_CHAR = 26;
 
// function to find the uncommon characters
// of the two strings
void findAndPrintUncommonChars(string str1, string str2)
{
    // mark presence of each character as 0
    // in the hash table 'present[]'
    int present[MAX_CHAR];
    for (int i=0; i<MAX_CHAR; i++)
        present[i] = 0;
 
    int l1 = str1.size();
    int l2 = str2.size();
 
    // for each character of str1, mark its
    // presence as 1 in 'present[]'
    for (int i=0; i<l1; i++)
        present[str1[i] - 'a'] = 1;
 
    // for each character of str2
    for (int i=0; i<l2; i++)
    {
        // if a character of str2 is also present
        // in str1, then mark its presence as -1
        if (present[str2[i] - 'a'] == 1
            || present[str2[i] - 'a'] == -1)
            present[str2[i] - 'a'] = -1;
 
        // else mark its presence as 2
        else
            present[str2[i] - 'a'] = 2;
    }
 
    // print all the uncommon characters
    for (int i=0; i<MAX_CHAR; i++)
        if (present[i] == 1 || present[i] == 2 )
            cout << (char(i + 'a')) << " ";
}
 
// Driver program to test above
int main()
{
    string str1 = "characters";
    string str2 = "alphabets";
    findAndPrintUncommonChars(str1, str2);
    return 0;
}

Time complexity: linear
Space complexity: constant
#include<bits/stdc++.h>
using namespace std;
 
string UncommonChars(string a, string b)
{
    int mp1[26] = {0}, mp2[26] = {0};
    int n = a.size(), m = b.size();
 
    for(auto &x: a){
      mp1[x-'a'] = 1;
    }
 
    for(auto &x: b){
      mp2[x-'a'] = 1;
    }
 
    string chars = "";
 
    for(int i = 0; i < 26; ++i){
      if(mp1[i]^mp2[i])
        chars+=char(i+'a');
    }
    if(chars == "")
      return "-1";
    else
      return chars;
}
 
int main(){
    string a = "geeksforgeeks";
    string b = "geeksquiz";
    string result = UncommonChars(a,b);
    cout << result << endl;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program Find concatenated string with
// uncommon characters of given strings
#include <bits/stdc++.h>
using namespace std;
 
string concatenatedString(string s1, string s2)
{
    string res = ""; // result
 
    // store all characters of s2 in map
    unordered_map<char, int> m;
    for (int i = 0; i < s2.size(); i++)
        m[s2[i]] = 1;
 
    // Find characters of s1 that are not
    // present in s2 and append to result
    for (int i = 0; i < s1.size(); i++) {
        if (m.find(s1[i]) == m.end())
            res += s1[i];
        else
            m[s1[i]] = 2;
    }
 
    // Find characters of s2 that are not
    // present in s1.
    for (int i = 0; i < s2.size(); i++)
        if (m[s2[i]] == 1)
            res += s2[i];
    return res;
}
 
/* Driver program to test above function */
int main()
{
    string s1 = "abcs";
    string s2 = "cxzca";
    cout << concatenatedString(s1, s2);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to remove vowels from a String
#include <bits/stdc++.h>
using namespace std;
 
string remVowel(string str)
{
    vector<char> vowels = {'a', 'e', 'i', 'o', 'u',
                           'A', 'E', 'I', 'O', 'U'};
     
    for (int i = 0; i < str.length(); i++)
    {
        if (find(vowels.begin(), vowels.end(),
                      str[i]) != vowels.end())
        {
            str = str.replace(i, 1, "");
            i -= 1;
        }
    }
    return str;
}
 
// Driver Code
int main()
{
    string str = "GeeeksforGeeks - A Computer"
                 " Science Portal for Geeks";
    cout << remVowel(str) << endl;
 
    return 0;
}
 
// This code is contributed by
// sanjeev2552
// and corrected by alreadytaken

Time complexity: linear
Space complexity: constant
// C++ program to remove vowels from a String
#include <bits/stdc++.h>
using namespace std;
 
string remVowel(string str)
{
    regex r("[aeiouAEIOU]");
     
    return regex_replace(str, r, "");
}
 
// Driver Code
int main()
{
    string str = "GeeeksforGeeks - A Computer Science Portal for Geeks";    
    cout << (remVowel(str));
    return 0;
}
 
// This code is contributed by Arnab Kundu

Time complexity: linear
Space complexity: constant