Banana-Leopard/TASTY huggingface dataset:
// C++ program to calculate number of substring
// divisible by 6.
#include <bits/stdc++.h>
#define MAX 100002
using namespace std;
 
// Return the number of substring divisible by 6
// and starting at index i in s[] and previous sum
// of digits modulo 3 is m.
int f(int i, int m, char s[], int memoize[][3])
{
    // End of the string.
    if (i == strlen(s))
        return 0;
 
    // If already calculated, return the
    // stored value.
    if (memoize[i][m] != -1)
        return memoize[i][m];
 
    // Converting into integer.
    int x = s[i] - '0';
 
    // Increment result by 1, if current digit
    // is divisible by 2 and sum of digits is
    // divisible by 3.
    // And recur for next index with new modulo.
    int ans = ((x+m)%3 == 0 && x%2 == 0) +
              f(i+1, (m+x)%3, s, memoize);
 
    return memoize[i][m] = ans;
}
 
// Returns substrings divisible by 6.
int countDivBy6(char s[])
{
    int n = strlen(s);
 
    // For storing the value of all states.
    int memoize[n+1][3];
    memset(memoize, -1, sizeof memoize);
 
    int ans = 0;
    for (int i = 0; i < strlen(s); i++)
    {
        // If string contain 0, increment count by 1.
        if (s[i] == '0')
            ans++;
 
        // Else calculate using recursive function.
        // Pass previous sum modulo 3 as 0.
        else
            ans += f(i, 0, s, memoize);
    }
 
    return ans;
}
 
// Driven Program
int main()
{
    char s[] = "4806";
 
    cout << countDivBy6(s) << endl;
 
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ implementation to check whether decimal representation
// of given binary number is divisible by 5 or not
#include <bits/stdc++.h>
 
using namespace std;
 
// function to return equivalent base 4 number
// of the given binary number
int equivalentBase4(string bin)
{
    if (bin.compare("00") == 0)
        return 0;
    if (bin.compare("01") == 0)
        return 1;
    if (bin.compare("10") == 0)
        return 2;
    return 3;
}
 
// function to check whether the given binary
// number is divisible by 5 or not
string isDivisibleBy5(string bin)
{
    int l = bin.size();
     
    if (l % 2 != 0)
    // add '0' in the beginning to make
    // length an even number
        bin = '0' + bin;
     
    // to store sum of digits at odd and
    // even places respectively
    int odd_sum, even_sum = 0;
     
    // variable check for odd place and
    // even place digit
    int isOddDigit = 1;
    for (int i = 0; i<bin.size(); i+= 2)
    {
        // if digit of base 4 is at odd place, then
        // add it to odd_sum
        if (isOddDigit)
            odd_sum += equivalentBase4(bin.substr(i, 2));
        // else digit of base 4 is at even place,
        // add it to even_sum
        else
            even_sum += equivalentBase4(bin.substr(i, 2));
         
        isOddDigit ^= 1;
    }
     
    // if this diff is divisible by 11(which is 5 in decimal)
    // then, the binary number is divisible by 5
    if (abs(odd_sum - even_sum) % 5 == 0)
        return "Yes";
     
    // else not divisible by 5
    return "No";
             
}
 
// Driver program to test above
int main()
{
    string bin = "10000101001";
    cout << isDivisibleBy5(bin);
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP Program to count substrings which are
// divisible by 8 but not by 3
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
 
// Returns count of substrings divisible by 8
// but not by 3.
int count(char s[], int len)
{
    int cur = 0, dig = 0;
    int sum[MAX], dp[MAX][3];
 
    memset(sum, 0, sizeof(sum));
    memset(dp, 0, sizeof(dp));
 
    dp[0][0] = 1;
 
    // Iterating the string.
    for (int i = 1; i <= len; i++)
    {
        dig = int(s[i-1])-48;
        cur += dig;
        cur %= 3;
 
        sum[i] = cur;
 
        // Prefix sum of number of substrings whose
        // sum of digits modulo 3 is 0, 1, 2.
        dp[i][0] = dp[i-1][0];
        dp[i][1] = dp[i-1][1];
        dp[i][2] = dp[i-1][2];
 
        dp[i][sum[i]]++;
    }
 
    int ans = 0, dprev = 0, value = 0, dprev2 = 0;
 
    // Iterating the string.
    for (int i = 1; i <= len; i++)
    {
        dig = int(s[i-1])-48;
 
        // Since single digit 8 is divisible
        // by 8 and not by 3.
        if (dig == 8)
            ans++;
 
        // Taking two digit number.
        if (i-2 >= 0)
        {
            dprev = int(s[i-2])-48; // 10th position
            value = dprev*10 + dig; // Complete 2 digit
                                    // number
 
            if ((value%8 == 0) && (value%3 != 0))
                ans++;
        }
 
        // Taking 3 digit number.
        if (i-3 >= 0)
        {
            dprev2 = int(s[i-3])-48; // 100th position
            dprev = int(s[i-2])-48; // 10th position
 
            // Complete 3 digit number.
            value = dprev2*100 + dprev*10 + dig;
 
            if (value%8 != 0)
                continue;
 
            // If number formed is divisible by 8 then
            // last 3 digits are also divisible by 8.
            // Then all the substring ending at this
            // index is divisible.
            ans += (i-2);
 
            // But those substring also contain number
            // which are not divisible by 3 so
            // remove them.
            ans -= (dp[i-3][sum[i]]);
        }
    }
 
    return ans;
}
 
// Driven Program
int main()
{
    char str[] = "6564525600";
    int len = strlen(str);
    cout << count(str, len) <<endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP for divisibility of number by 999
#include<bits/stdc++.h>
using namespace std;
 
// function to check divisibility
bool isDivisible999(string num)
{
    int n = num.length();
    if (n == 0 && num[0] == '0')
       return true;
 
    // Append required 0s at the beginning.
    if (n % 3 == 1)
       num = "00" + num;
    if (n % 3 == 2)
       num = "0" + num;
 
    // add digits in group of three in gSum
    int gSum = 0;
    for (int i = 0; i<n; i++)
    {
        // group saves 3-digit group
        int group = 0;
        group += (num[i++] - '0') * 100;
        group += (num[i++] - '0') * 10;
        group += num[i] - '0';
        gSum += group;
    }
 
    // calculate result till 3 digit sum
    if (gSum > 1000)
    {
        num = to_string(gSum);
        n = num.length();
        gSum = isDivisible999(num);
    }
    return (gSum == 999);
}
 
// driver program
int main()
{
    string num = "1998";
    int n = num.length();
    if (isDivisible999(num))
        cout << "Divisible";
    else
        cout << "Not divisible";
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to implement division with large
// number
#include <bits/stdc++.h>
using namespace std;
 
// A function to perform division of large numbers
string longDivision(string number, int divisor)
{
    // As result can be very large store it in string
    string ans;
 
    // Find prefix of number that is larger
    // than divisor.
    int idx = 0;
    int temp = number[idx] - '0';
    while (temp < divisor)
        temp = temp * 10 + (number[++idx] - '0');
 
    // Repeatedly divide divisor with temp. After
    // every division, update temp to include one
    // more digit.
    while (number.size() > idx) {
        // Store result in answer i.e. temp / divisor
        ans += (temp / divisor) + '0';
 
        // Take next digit of number
        temp = (temp % divisor) * 10 + number[++idx] - '0';
    }
 
    // If divisor is greater than number
    if (ans.length() == 0)
        return "0";
 
    // else return ans
    return ans;
}
 
// Driver program to test longDivision()
int main()
{
    string number = "1248163264128256512";
    int divisor = 125;
    cout << longDivision(number, divisor);
    return 0;
}

Time complexity: np
Space complexity: linear
// C++ program to find remainder of a large
// number when divided by 7.
#include<iostream>
using namespace std;
 
/* Function which returns Remainder after dividing
   the number by 7 */
int remainderWith7(string num)
{
    // This series is used to find remainder with 7
    int series[] = {1, 3, 2, -1, -3, -2};
 
    // Index of next element in series
    int series_index = 0;
 
    int result = 0;  // Initialize result
 
    // Traverse num from end
    for (int i=num.size()-1; i>=0; i--)
    {
        /* Find current digit of nun */
        int digit = num[i] - '0';
 
        // Add next term to result
        result += digit * series[series_index];
 
        // Move to next term in series
        series_index = (series_index + 1) % 6;
 
        // Make sure that result never goes beyond 7.
        result %= 7;
    }
 
    // Make sure that remainder is positive
    if (result < 0)
      result = (result + 7) % 7;
 
    return result;
}
 
/* Driver program */
int main()
{
    string str = "12345";
    cout << "Remainder with 7 is "
         << remainderWith7(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
// A simple C++ program to
// check for even or odd
#include <iostream>
using namespace std;
 
// Returns true if n is
// even, else odd
bool isEven(int n) { return (n % 2 == 0); }
 
// Driver code
int main()
{
    int n = 101;
    isEven(n) ? cout << "Even" : cout << "Odd";
 
    return 0;
}

Time complexity: constant
Space complexity: constant
// A simple C++ program to
// check for even or odd
#include <iostream>
using namespace std;
 
// Returns true if n is
// even, else odd
bool isEven(int n)
{
     
// n & 1 is 1, then
// odd, else even
return (!(n & 1));
}
 
// Driver code
int main()
{
int n = 101;
isEven(n)? cout << "Even" :
           cout << "Odd";
 
return 0;
}

Time complexity: constant
Space complexity: constant
// C++ implementation to find product of
// digits of elements at k-th level
#include <bits/stdc++.h>
using namespace std;
 
// Function to find product of digits
// of elements at k-th level
int productAtKthLevel(string tree, int k)
{
    int level = -1;
    int product = 1; // Initialize result
    int n = tree.length();
 
    for (int i = 0; i < n; i++) {
        // increasing level number
        if (tree[i] == '(')
            level++;
 
        // decreasing level number
        else if (tree[i] == ')')
            level--;
 
        else {
            // check if current level is
            // the desired level or not
            if (level == k)
                product *= (tree[i] - '0');
        }
    }
 
    // required product
    return product;
}
 
// Driver program
int main()
{
    string tree = "(0(5(6()())(4()(9()())))(7(1()())(3()())))";
    int k = 2;
    cout << productAtKthLevel(tree, k);
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP implementation to find remainder
// when a large number is divided by 11
#include <bits/stdc++.h>
using namespace std;
 
// Function to return remainder
int remainder(string str)
{
    // len is variable to store the
    // length of number string.
    int len = str.length();
 
    int num, rem = 0;
 
    // loop that find remainder
    for (int i = 0; i < len; i++) {
        num = rem * 10 + (str[i] - '0');
        rem = num % 11;
    }
 
    return rem;
}
 
// Driver code
int main()
{
    string str = "3435346456547566345436457867978";
    cout << remainder(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to count number of ways to
// remove an element so that XOR of remaining
// string becomes 0.
#include <bits/stdc++.h>
using namespace std;
 
// Return number of ways in which XOR become ZERO
// by remove 1 element
int xorZero(string str)
{
    int one_count = 0, zero_count = 0;
    int n = str.length();
 
    // Counting number of 0 and 1
    for (int i = 0; i < n; i++)
        if (str[i] == '1')
            one_count++;
        else
            zero_count++;
     
    // If count of ones is even
    // then return count of zero
    // else count of one
    if (one_count % 2 == 0)
        return zero_count;
    return one_count;
}
 
// Driver Code
int main()
{
    string str = "11111";
    cout << xorZero(str) << endl;
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find difficulty of a sentence
#include <iostream>
using namespace std;
 
// Utility function to check character is vowel
// or not
bool isVowel(char ch)
{
    return ( ch == 'a' || ch == 'e' ||
             ch == 'i' || ch == 'o' ||
             ch == 'u');
}
 
// Function to calculate difficulty
int calcDiff(string str)
{
 
    int count_vowels = 0, count_conso = 0;
    int hard_words = 0, easy_words = 0;
    int consec_conso = 0;
 
    // Start traversing the string
    for (int i = 0; i < str.length(); i++)
    {
        // Check if current character is vowel
        // or consonant
        if (str[i] != ' ' && isVowel(tolower(str[i])))
        {
            // Increment if vowel
            count_vowels++;
            consec_conso = 0;
        }
 
        // Increment counter for consonant
        // also maintain a separate counter for
        // counting consecutive consonants
        else if (str[i]!= ' ')
        {
            count_conso++;
            consec_conso++;
        }
 
        // If we get 4 consecutive consonants
        // then it is a hard word
        if (consec_conso == 4)
        {
            hard_words++;
 
            // Move to the next word
            while (i < str.length() && str[i]!= ' ')
                i++;
 
            // Reset all counts
            count_conso = 0;
            count_vowels = 0;
            consec_conso = 0;
        }
 
        else if ( i < str.length() &&
                  (str[i] == ' ' || i == str.length()-1))
        {
            // Increment hard_words, if no. of consonants are
            // higher than no. of vowels, otherwise increment
            // count_vowels
            count_conso > count_vowels ? hard_words++
                                       : easy_words++;
 
            // Reset all counts
            count_conso = 0;
            count_vowels = 0;
            consec_conso = 0;
        }
    }
 
    // Return difficulty of sentence
    return 5 * hard_words + 3 * easy_words;
}
 
// Drivers code
int main()
{
    string str = "I am a geek";
    string str2 = "We are geeks";
    cout << calcDiff(str) << endl;
    cout << calcDiff(str2) << endl;
 
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to count the uppercase,
// lowercase, special characters
// and numeric values
#include<iostream>
using namespace std;
 
// Function to count uppercase, lowercase,
// special characters and numbers
void Count(string str)
{
    int upper = 0, lower = 0, number = 0, special = 0;
    for (int i = 0; i < str.length(); i++)
    {
        if (str[i] >= 'A' && str[i] <= 'Z')
            upper++;
        else if (str[i] >= 'a' && str[i] <= 'z')
            lower++;
        else if (str[i]>= '0' && str[i]<= '9')
            number++;
        else
            special++;
    }
    cout << "Upper case letters: " << upper << endl;
    cout << "Lower case letters : " << lower << endl;
    cout << "Number : " << number << endl;
    cout << "Special characters : " << special << endl;
}
 
// Driver function
int main()
{
    string str = "#GeeKs01fOr@gEEks07";
    Count(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to find
// smallest window containing
// all characters of a pattern.
#include <bits/stdc++.h>
using namespace std;
 
const int no_of_chars = 256;
 
// Function to find smallest
// window containing
// all characters of 'pat'
string findSubString(string str, string pat)
{
    int len1 = str.length();
    int len2 = pat.length();
 
    // Check if string's length
    // is less than pattern's
    // length. If yes then no such
    // window can exist
    if (len1 < len2) {
        cout << "No such window exists";
        return "";
    }
 
    int hash_pat[no_of_chars] = { 0 };
    int hash_str[no_of_chars] = { 0 };
 
    // Store occurrence ofs characters
    // of pattern
    for (int i = 0; i < len2; i++)
        hash_pat[pat[i]]++;
 
    int start = 0, start_index = -1, min_len = INT_MAX;
 
    // Start traversing the string
    // Count of characters
    int count = 0;
    for (int j = 0; j < len1; j++) {
       
        // Count occurrence of characters
        // of string
        hash_str[str[j]]++;
 
        // If string's char matches with
        // pattern's char
        // then increment count
        if (hash_str[str[j]] <= hash_pat[str[j]])
            count++;
 
        // if all the characters are matched
        if (count == len2) {
           
            // Try to minimize the window
            while (hash_str[str[start]]
                       > hash_pat[str[start]]
                   || hash_pat[str[start]] == 0) {
 
                if (hash_str[str[start]]
                    > hash_pat[str[start]])
                    hash_str[str[start]]--;
                start++;
            }
 
            // update window size
            int len_window = j - start + 1;
            if (min_len > len_window) {
                min_len = len_window;
                start_index = start;
            }
        }
    }
 
    // If no window found
    if (start_index == -1) {
        cout << "No such window exists";
        return "";
    }
 
    // Return substring starting from start_index
    // and length min_len
    return str.substr(start_index, min_len);
}
 
// Driver code
int main()
{
    string str = "this is a test string";
    string pat = "tist";
 
    cout << findSubString(str, pat);
    return 0;
}

Time complexity: linear
Space complexity: constant
#include <bits/stdc++.h>
using namespace std;
 
// Function
string Minimum_Window(string s, string t)
{
 
    int m[256] = { 0 };
 
    // Answer
    int ans = INT_MAX; // length of ans
    int start = 0; // starting index of ans
    int count = 0;
 
    // creating map
    for (int i = 0; i < t.length(); i++) {
        if (m[t[i]] == 0)
            count++;
        m[t[i]]++;
    }
 
    // References of Window
    int i = 0;
    int j = 0;
 
    // Traversing the window
    while (j < s.length()) {
        // Calculations
        m[s[j]]--;
        if (m[s[j]] == 0)
            count--;
 
        // Condition matching
        if (count == 0) {
            while (count == 0) {
                // Sorting ans
                if (ans > j - i + 1) {
                    ans = min(ans, j - i + 1);
                    start = i;
                }
                // Sliding I
                // Calculation for removing I
 
                m[s[i]]++;
                if (m[s[i]] > 0)
                    count++;
 
                i++;
            }
        }
        j++;
    }
 
    if (ans != INT_MAX)
        return s.substr(start, ans);
    else
        return "-1";
}
 
int main()
{
    string s = "this is a test string";
    string t = "tist";
 
    cout << Minimum_Window(s, t);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to count number of substrings with
// exactly k distinct characters in a given string
#include<bits/stdc++.h>
using namespace std;
 
// Function to count number of substrings
// with exactly k unique characters
int countkDist(string str, int k)
{
    int n = str.length();
 
    // Initialize result
    int res = 0;
 
    // To store count of characters from 'a' to 'z'
    int cnt[26];
 
    // Consider all substrings beginning with
    // str[i]
    for (int i = 0; i < n; i++)
    {
        int dist_count = 0;
 
        // Initializing array with 0
        memset(cnt, 0, sizeof(cnt));
 
        // Consider all substrings between str[i..j]
        for (int j=i; j<n; j++)
        {
            // If this is a new character for this
            // substring, increment dist_count.
            if (cnt[str[j] - 'a'] == 0)
                dist_count++;
 
            // Increment count of current character
            cnt[str[j] - 'a']++;
 
            // If distinct character count becomes k,
            // then increment result.
            if (dist_count == k)
                res++;
            if(dist_count > k) break;
        }
    }
 
    return res;
}
 
// Driver Program
int main()
{
    string str = "abcbaa";
    int k = 3;
    cout << "Total substrings with exactly "
         << k <<" distinct characters :"
         << countkDist(str, k) << endl;
    return 0;
}

Time complexity: quadratic
Space complexity: constant
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
 
// the number of subarrays with at most K distinct elements
int most_k_chars(string& s, int k)
{
    if (s.size() == 0) {
        return 0;
    }
    unordered_map<char, int> map;
    int num = 0, left = 0;
 
    for (int i = 0; i < s.size(); i++) {
        map[s[i]]++;
        while (map.size() > k) {
            map[s[left]]--;
            if (map[s[left]] == 0) {
                map.erase(s[left]);
            }
            left++;
        }
        num += i - left + 1;
    }
    return num;
}
 
int exact_k_chars(string& s, int k)
{
    return most_k_chars(s, k) - most_k_chars(s, k - 1);
}
 
// Driver Program
int main()
{
    string s1 = "pqpqs";
    int k = 2;
    cout << "Total substrings with exactly " << k
         << " distinct characters : "
         << exact_k_chars(s1, k) << endl;
 
    string s2 = "aabab";
    k = 2;
    cout << "Total substrings with exactly " << k
         << " distinct characters : "
         << exact_k_chars(s2, k) << endl;
}

Time complexity: linear
Space complexity: constant
// C++ program to count number of substrings
// with counts of distinct characters as k.
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
 
// Returns true if all values
// in freq[] are either 0 or k.
bool check(int freq[], int k)
{
    for (int i = 0; i < MAX_CHAR; i++)
        if (freq[i] && freq[i] != k)
            return false;
    return true;
}
 
// Returns count of substrings where frequency
// of every present character is k
int substrings(string s, int k)
{
    int res = 0;  // Initialize result
 
    // Pick a starting point
    for (int i = 0; s[i]; i++) {
 
        // Initialize all frequencies as 0
        // for this starting point
        int freq[MAX_CHAR] = { 0 };
 
        // One by one pick ending points
        for (int j = i; s[j]; j++) {
  
            // Increment frequency of current char
            int index = s[j] - 'a';
            freq[index]++;
 
            // If frequency becomes more than
            // k, we can't have more substrings
            // starting with i
            if (freq[index] > k)
                break;
 
            // If frequency becomes k, then check
            // other frequencies as well.
            else if (freq[index] == k &&
                  check(freq, k) == true)
                res++;
        }
    }
    return res;
}
 
// Driver code
int main()
{
    string s = "aabbcc";
    int k = 2;
    cout << substrings(s, k) << endl;
 
    s = "aabbc";
    k = 2;
    cout << substrings(s, k) << endl;
}

Time complexity: quadratic
Space complexity: constant