Banana-Leopard/TASTY huggingface dataset:
// C++ program to query number of palindromic
// substrings of a string in a range
#include <bits/stdc++.h>
using namespace std;
#define M 50
 
// Utility method to construct the dp array
void constructDP(int dp[M][M], string str)
{
    int l = str.length();
 
    // declare 2D array isPalin, isPalin[i][j] will
    // be 1 if str(i..j) is palindrome
    int isPalin[l + 1][l + 1];
 
    // initialize dp and isPalin array by zeros
    for (int i = 0; i <= l; i++) {
        for (int j = 0; j <= l; j++) {
            isPalin[i][j] = dp[i][j] = 0;
        }
    }
 
    // loop for starting index of range
    for (int i = l - 1; i >= 0; i--) {
 
        // initialize value for one character strings as 1
        isPalin[i][i] = 1;
        dp[i][i] = 1;
 
        // loop for ending index of range
        for (int j = i + 1; j < l; j++) {
 
            /* isPalin[i][j] will be 1 if ith and
               jth characters are equal and mid
               substring str(i+1..j-1) is also a
               palindrome             */
            isPalin[i][j] = (str[i] == str[j] && (i + 1 > j - 1 || isPalin[i + 1][j - 1]));
 
            /* dp[i][j] will be addition of number
               of palindromes from i to j-1 and i+1
               to j subtracting palindromes from i+1
               to j-1 (as counted twice) plus 1 if
               str(i..j) is also a palindrome */
            dp[i][j] = dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1] + isPalin[i][j];
        }
    }
}
 
// method returns count of palindromic substring in range (l, r)
int countOfPalindromeInRange(int dp[M][M], int l, int r)
{
    return dp[l][r];
}
 
// Driver code to test above methods
int main()
{
    string str = "xyaabax";
 
    int dp[M][M];
    constructDP(dp, str);
 
    int l = 3;
    int r = 5;
 
    cout << countOfPalindromeInRange(dp, l, r);
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// C++ program to find n=th even length string.
#include <bits/stdc++.h>
using namespace std;
 
// Function to find nth even length Palindrome
string evenlength(string n)
{
    // string r to store resultant
    // palindrome. Initialize same as s
    string res = n;
 
    // In this loop string r stores
    // reverse of string s after the
    // string s in consecutive manner .
    for (int j = n.length() - 1; j >= 0; --j)
        res += n[j];
 
    return res;
}
 
// Driver code
int main()
{
    string n = "10";
   
    // Function call
    cout << evenlength(n);
    return 0;
}

Time complexity: linear
Space complexity: linear
//code to make 'ab' free string
 
#include<bits/stdc++.h>
using namespace std;
 
// code to make 'ab' free string
int abFree(string s)
{
    int n = s.length();
    char char_array[n + 1];
     
    // convert string into char array
    strcpy(char_array, s.c_str());
     
    // Traverse from end. Keep track of count
    // b's. For every 'a' encountered, add b_count
    // to result and double b_count.
    int b_count = 0;
    int res = 0;
    for (int i = 0; i < n; i++)
    {
        if (char_array[n - i - 1] == 'a')
        {
            res = (res + b_count);
            b_count = (b_count * 2);
        } else {
            b_count += 1;
        }
    }
    return res;
}
 
// Driver code
int main()
{
    string s = "abbaa";
    cout<<abFree(s)<<endl;
     
    s = "aab";
    cout<<abFree(s)<<endl;
     
    s = "ababab";
    cout<<abFree(s)<<endl;
     
    return 0;
}
 
// This code is contributed by Rajput-Ji

Time complexity: linear
Space complexity: linear
// C++ program to find minimum number of flip to make binary
// string alternate
#include <bits/stdc++.h>
using namespace std;
 
//  Utility method to flip a character
char flip(char ch) { return (ch == '0') ? '1' : '0'; }
 
//  Utility method to get minimum flips when alternate
//  string starts with expected char
int getFlipWithStartingCharcter(string str, char expected)
{
    int flipCount = 0;
    for (int i = 0; i < str.length(); i++) {
        // if current character is not expected, increase
        // flip count
        if (str[i] != expected)
            flipCount++;
 
        //  flip expected character each time
        expected = flip(expected);
    }
    return flipCount;
}
 
// method return minimum flip to make binary string
// alternate
int minFlipToMakeStringAlternate(string str)
{
    //  return minimum of following two
    //  1) flips when alternate string starts with 0
    //  2) flips when alternate string starts with 1
    return min(getFlipWithStartingCharcter(str, '0'),
               getFlipWithStartingCharcter(str, '1'));
}
 
//  Driver code to test above method
int main()
{
    string str = "0001010111";
    cout << minFlipToMakeStringAlternate(str);
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta
// (kriSania804)

Time complexity: linear
Space complexity: constant
// An efficient C++ program to find 2's complement
#include<bits/stdc++.h>
using namespace std;
 
// Function to find two's complement
string findTwoscomplement(string str)
{
    int n = str.length();
 
    // Traverse the string to get first '1' from
    // the last of string
    int i;
    for (i = n-1 ; i >= 0 ; i--)
        if (str[i] == '1')
            break;
 
    // If there exists no '1' concatenate 1 at the
    // starting of string
    if (i == -1)
        return '1' + str;
 
    // Continue traversal after the position of
    // first '1'
    for (int k = i-1 ; k >= 0; k--)
    {
        //Just flip the values
        if (str[k] == '1')
            str[k] = '0';
        else
            str[k] = '1';
    }
 
    // return the modified string
    return str;
}
 
// Driver code
int main()
{
    string str = "00000101";
    cout << findTwoscomplement(str);
    return 0;
}             

Time complexity: linear
Space complexity: constant
// C++ program to count all distinct
// binary strings with two consecutive 1's
#include <iostream>
using namespace std;
 
// Returns count of n length binary
// strings with consecutive 1's
int countStrings(int n)
{
    // Count binary strings without consecutive 1's.
    // See the approach discussed on be
    // ( http://goo.gl/p8A3sW )
    int a[n], b[n];
    a[0] = b[0] = 1;
    for (int i = 1; i < n; i++)
    {
        a[i] = a[i - 1] + b[i - 1];
        b[i] = a[i - 1];
    }
 
    // Subtract a[n-1]+b[n-1] from 2^n
    return (1 << n) - a[n - 1] - b[n - 1];
}
 
// Driver code
int main()
{
    cout << countStrings(5) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// Recursive C++ program to generate all binary strings
// formed by replacing each wildcard character by 0 or 1
#include <iostream>
using namespace std;
 
// Recursive function to generate all binary strings
// formed by replacing each wildcard character by 0 or 1
void print(string str, int index)
{
    if (index == str.size())
    {
        cout << str << endl;
        return;
    }
 
    if (str[index] == '?')
    {
        // replace '?' by '0' and recurse
        str[index] = '0';
        print(str, index + 1);
 
        // replace '?' by '1' and recurse
        str[index] = '1';
        print(str, index + 1);
 
        // No need to backtrack as string is passed
        // by value to the function
    }
    else
        print(str, index + 1);
}
 
// Driver code to test above function
int main()
{
    string str = "1??0?101";
 
    print(str, 0);
 
    return 0;
}

Time complexity: np
Space complexity: quadratic
// Iterative C++ program to generate all binary
// strings formed by replacing each wildcard
// character by 0 or 1
#include <iostream>
#include <queue>
using namespace std;
 
// Iterative function to generate all binary strings
// formed by replacing each wildcard character by 0
// or 1
void print(string str)
{
    queue<string> q;
    q.push(str);
 
    while (!q.empty())
    {
        string str = q.front();
 
        // find position of first occurrence of wildcard
        size_t index = str.find('?');
 
        // If no matches were found,
        // find returns string::npos
        if(index != string::npos)
        {
            // replace '?' by '0' and push string into queue
            str[index] = '0';
            q.push(str);
 
            // replace '?' by '1' and push string into queue
            str[index] = '1';
            q.push(str);
        }
 
        else
            // If no wildcard characters are left,
            // print the string.
            cout << str << endl;
 
        q.pop();
    }
}
 
// Driver code to test above function
int main()
{
    string str = "1??0?101";
 
    print(str);
 
    return 0;
}

Time complexity: quadratic
Space complexity: np
// C++ program to implement the approach
#include <bits/stdc++.h>
 
using namespace std;
 
/* we store processed strings in all (array)
we see if string as "?", if so, replace it with 0 and 1
and send it back to recursive func until base case is
reached which is no wildcard left */
 
vector<string> res;
 
void genBin(string s)
{
    auto pos = s.find('?');
    if (pos != string::npos) {
        // copying s to s1
        string s1 = s;
        // replacing first occurrence of ?
        // with 0
        s1.replace(pos, 1, "0");
        // copying s to s2
        string s2 = s;
        // replacing first occurrence of ?
        // with 1
        s2.replace(pos, 1, "1");
        genBin(s1);
        genBin(s2);
    }
    else {
        res.push_back(s);
    }
}
 
// Driver code
int main()
{
    genBin("1??0?101");
    for (string x : res) {
        cout << x << " ";
    }
}
 
// This code is contributed by phasing17

Time complexity: quadratic
Space complexity: np
#include <bits/stdc++.h>
using namespace std;
 
// The function that adds two-bit sequences and returns the addition
string addBitStrings(string str1, string str2)
{
    string ans = "";
    int i = str1.size() - 1;
    int j = str2.size() - 1;
    int carry = 0;
    while (i >= 0 || j >= 0 || carry) {
        carry += ((i >= 0) ? (str1[i--] - '0') : (0));
        carry += ((j >= 0) ? (str2[j--] - '0') : (0));
        ans = char('0' + (carry % 2)) + ans;
        carry = carry / 2;
    }
    return ans;
}
 
// Driver program to test above functions
int main()
{
    string str1 = "1100011";
    string str2 = "10";
 
    cout << "Sum is " << addBitStrings(str1, str2);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to count all distinct binary strings
// without two consecutive 1's
#include <iostream>
using namespace std;
 
int countStrings(int n)
{
    int a[n], b[n];
    a[0] = b[0] = 1;
    for (int i = 1; i < n; i++)
    {
        a[i] = a[i-1] + b[i-1];
        b[i] = a[i-1];
    }
    return a[n-1] + b[n-1];
}
 
 
// Driver program to test above functions
int main()
{
    cout << countStrings(3) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to count all distinct binary strings
// without two consecutive 1's
#include <bits/stdc++.h>
using namespace std;
 
int countStrings(int n)
{
    int a = 1, b = 1;
    for (int i = 1; i < n; i++) {
        // Here we have used the temp variable because we
        // want to assign the older value of a to b
        int temp = a + b;
        b = a;
        a = temp;
    }
    return a + b;
}
 
// Driver program to test above functions
int main()
{
    cout << countStrings(3) << endl;
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Time complexity: linear
Space complexity: constant
// C++ program to check if a string is of
// the form a^nb^n.
#include <iostream>
using namespace std;
  
// Returns true str is of the form a^nb^n.
bool isAnBn(string str)
{
    int n = str.length();
  
    // After this loop 'i' has count of a's
    int i;
    for (i = 0; i < n; i++)
        if (str[i] != 'a')
            break;
  
    // Since counts of a's and b's should
    // be equal, a should appear exactly
    // n/2 times
    if (i * 2 != n)
        return false;
  
    // Rest of the characters must be all 'b'
    int j;
    for (j = i; j < n; j++)
        if (str[j] != 'b')
            return false;
  
    return true;
}
  
// Driver code
int main()
{
    string str = "abab";
    
    // Function call
    isAnBn(str) ? cout << "Yes" : cout << "No";
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ code to check a^nb^n
// pattern
#include <iostream>
using namespace std;
  
// Returns "Yes" str is of the form a^nb^n.
string isAnBn(string str)
{
    int n = str.length();
    if (n & 1)
        return "No";
      
    // check first half is 'a' and other half is full of 'b'
    int i;
    for (i = 0; i < n / 2; i++)
        if (str[i] != 'a' || str[n - i - 1] != 'b')
            return "No";
    return "Yes";
}
  
// Driver code
int main()
{
    string str = "ab";
    
    // Function call
    cout << isAnBn(str);
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ implementation to find the binary
// representation of next greater integer
#include <bits/stdc++.h>
using namespace std;
 
// function to find the required
// binary representation
string nextGreater(string num)
{
    int l = num.size();
 
    // examine bits from the right
    for (int i=l-1; i>=0; i--)
    {
        // if '0' is encountered, convert
        // it to '1' and then break
        if (num.at(i) == '0')
        {
            num.at(i) = '1';
            break;
        }
 
        // else convert '1' to '0'
        else
            num.at(i) = '0';
 
 
    // if the binary representation
    // contains only the set bits
    if (i < 0)
        num = "1" + num;
    }
    // final binary representation
    // of the required integer
    return num;
}
 
// Driver program to test above
int main()
{
    string num = "10011";
    cout << "Binary representation of next number = "
         << nextGreater(num);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to find next permutation in a
// binary string.
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the next greater number
// with same number of 1's and 0's
string nextGreaterWithSameDigits(string bnum)
{
    int l = bnum.size();
    int i;
    for (int i=l-2; i>=1; i--)
    {
        // locate first 'i' from end such that
        // bnum[i]=='0' and bnum[i+1]=='1'
        // swap these value and break;
        if (bnum.at(i) == '0' &&
           bnum.at(i+1) == '1')
        {
            char ch = bnum.at(i);
            bnum.at(i) = bnum.at(i+1);
            bnum.at(i+1) = ch;
            break;
        }
    }
 
    // if no swapping performed
    if (i == 0)
        "no greater number";
 
    // Since we want the smallest next value,
    // shift all 1's at the end in the binary
    // substring starting from index 'i+2'
    int j = i+2, k = l-1;
    while (j < k)
    {
        if (bnum.at(j) == '1' && bnum.at(k) == '0')
        {
            char ch = bnum.at(j);
            bnum.at(j) = bnum.at(k);
            bnum.at(k) = ch;
            j++;
            k--;
        }
 
        // special case while swapping if '0'
        // occurs then break
        else if (bnum.at(i) == '0')
            break;
 
        else
            j++;
 
    }
 
    // required next greater number
    return bnum;
}
 
// Driver program to test above
int main()
{
    string bnum = "10010";
    cout << "Binary representation of next greater number = "
         << nextGreaterWithSameDigits(bnum);
    return 0;
}

Time complexity: linear
Space complexity: constant
// CPP Program to find the length of
// substring with maximum difference of
// zeros and ones in binary string.
#include <iostream>
using namespace std;
 
// Returns the length of substring with
// maximum difference of zeroes and ones
// in binary string
int findLength(string str, int n)
{
    int current_sum = 0;
    int max_sum = 0;
 
    // traverse a binary string from left
    // to right
    for (int i = 0; i < n; i++) {
 
        // add current value to the current_sum
        // according to the Character
        // if it's '0' add 1 else -1
        current_sum += (str[i] == '0' ? 1 : -1);
 
        if (current_sum < 0)
            current_sum = 0;
 
        // update maximum sum
        max_sum = max(current_sum, max_sum);
    }
 
    // return -1 if string does not contain
    // any zero that means all ones
    // otherwise max_sum
    return max_sum == 0 ? -1 : max_sum;
}
 
// Driven Program
int main()
{
    string s = "11000010001";
    int n = 11;
    cout << findLength(s, n) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// CPP program to find min flips in binary
// string to make all characters equal
#include <bits/stdc++.h>
using namespace std;
  
// To find min number of flips in binary string
int findFlips(char str[], int n)
{
    char last = ' '; int res = 0;
  
    for (int i = 0; i < n; i++) {
  
        // If last character is not equal
        // to str[i] increase res
        if (last != str[i])
            res++;
        last = str[i];
    }
  
    // To return min flips
    return res / 2;
}
  
// Driver program to check findFlips()
int main()
{
    char str[] = "00011110001110";
    int n = strlen(str);
  
    cout << findFlips(str, n);
  
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to add two binary strings
#include <bits/stdc++.h>
using namespace std;
  
// This function adds two binary strings and return
// result as a third string
string addBinary(string A, string B)
{
    // If the length of string A is greater than the length
    // of B then just swap the string by calling the
    // same function and make sure to return the function
    // otherwise recursion will occur which leads to
    // calling the same function twice
    if (A.length() > B.length())
        return addBinary(B, A);
  
    // Calculating the difference between the length of the
    // two strings.
    int diff = B.length() - A.length();
  
    // Initialise the padding string which is used to store
    // zeroes that should be added as prefix to the string
    // which has length smaller than the other string.
    string padding;
    for (int i = 0; i < diff; i++)
        padding.push_back('0');
  
    A = padding + A;
    string res;
    char carry = '0';
  
    for (int i = A.length() - 1; i >= 0; i--) {
        // This if condition solves 110 111 possible cases
        if (A[i] == '1' && B[i] == '1') {
            if (carry == '1')
                res.push_back('1'), carry = '1';
            else
                res.push_back('0'), carry = '1';
        }
        // This if condition solves 000 001 possible cases
        else if (A[i] == '0' && B[i] == '0') {
            if (carry == '1')
                res.push_back('1'), carry = '0';
            else
                res.push_back('0'), carry = '0';
        }
        // This if condition solves 100 101 010 011 possible
        // cases
        else if (A[i] != B[i]) {
            if (carry == '1')
                res.push_back('0'), carry = '1';
            else
                res.push_back('1'), carry = '0';
        }
    }
  
    // If at the end there is carry then just add it to the
    // result
    if (carry == '1')
        res.push_back(carry);
    // reverse the result
    reverse(res.begin(), res.end());
  
    // To remove leading zeroes
    int index = 0;
    while (index + 1 < res.length() && res[index] == '0')
        index++;
    return (res.substr(index));
}
  
// Driver program
int main()
{
    string a = "1101", b = "100";
    cout << addBinary(a, b) << endl;
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to convert
// string into binary string
#include <bits/stdc++.h>
using namespace std;
 
// utility function
void strToBinary(string s)
{
    int n = s.length();
 
 
    for (int i = 0; i <= n; i++)
    {
        // convert each char to
        // ASCII value
        int val = int(s[i]);
 
        // Convert ASCII value to binary
        string bin = "";
        while (val > 0)
        {
            (val % 2)? bin.push_back('1') :
                       bin.push_back('0');
            val /= 2;
        }
        reverse(bin.begin(), bin.end());
 
        cout << bin << " ";
    }
}
 
// driver code
int main()
{
 
    string s = "geeks";
    strToBinary(s);
    return 0;
}

Time complexity: linear
Space complexity: linear
// C++ program to Generate
// all binary string without
// consecutive 1's of size K
#include<bits/stdc++.h>
using namespace std ;
 
// A utility function generate all string without
// consecutive 1'sof size K
void generateAllStringsUtil(int K, char str[], int n)
{
     
    // Print binary string without consecutive 1's
    if (n  == K)
    {
         
        // Terminate binary string
        str[n] = '\0' ;
        cout << str << " ";
        return ;
    }
 
    // If previous character is '1' then we put
    // only 0 at end of string
    //example str = "01" then new string be "010"
    if (str[n-1] == '1')
    {
        str[n] = '0';
        generateAllStringsUtil (K , str , n+1);
    }
 
    // If previous character is '0' than we put
    // both '1' and '0' at end of string
    // example str = "00" then
    // new string "001" and "000"
    if (str[n-1] == '0')
    {
        str[n] = '0';
        generateAllStringsUtil(K, str, n+1);
        str[n] = '1';
        generateAllStringsUtil(K, str, n+1) ;
    }
}
 
// Function generate all binary string without
// consecutive 1's
void generateAllStrings(int K )
{
    // Base case
    if (K <= 0)
        return ;
 
    // One by one stores every
    // binary string of length K
    char str[K];
 
    // Generate all Binary string
    // starts with '0'
    str[0] = '0' ;
    generateAllStringsUtil ( K , str , 1 ) ;
 
    // Generate all Binary string
    // starts with '1'
    str[0] = '1' ;
    generateAllStringsUtil ( K , str , 1 );
}
 
// Driver program to test above function
int main()
{
    int K = 3;
    generateAllStrings (K) ;
    return 0;
}

Time complexity: np
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
void All_Binary_Strings(bool arr[],int num,int r)
{
    if(r==num)
    {
        for(int i=0;i<num;i++)
        cout<<arr[i];
        cout<<" ";
        return;
    }
    else if(arr[r-1])
    {
        arr[r]=0;
        All_Binary_Strings(arr,num,r+1);
    }
    else
    {
         arr[r]=0;
        All_Binary_Strings(arr,num,r+1);
         arr[r]=1;
        All_Binary_Strings(arr,num,r+1);
    }
}
 
void print(bool a[],int& num)
{
    a[0]=0;
    All_Binary_Strings(a,num,1);
    a[0]=1;
    All_Binary_Strings(a,num,1);
}
 
//driver's code
int main()
{
    int n=2;
    bool a[n];
    print(a,n);
    return 0;
}

Time complexity: np
Space complexity: linear
#include <bits/stdc++.h>
using namespace std;
 
void All_Binary_Strings(string str,int num)
{
    int len=str.length();
    if(len==num)
    {
        cout<<str<<" ";
        return;
    }
    else if(str[len-1]=='1')
        All_Binary_Strings(str+'0',num);
    else
    {
        All_Binary_Strings(str+'0',num);
        All_Binary_Strings(str+'1',num);
    }
}
 
void print(int& num)
{
    string word;
    word.push_back('0');
    All_Binary_Strings(word,num);
    word[0]='1';
    All_Binary_Strings(word,num);
}
 
//driver's code
int main()
{
    int n=4;
    print(n);
    return 0;
}

Time complexity: np
Space complexity: linear
// C++ Program to find ith character in
// a binary string.
#include <bits/stdc++.h>
using namespace std;
 
// Function to store binary Representation
void binary_conversion(string &s, int m) {
  while (m) {
    int tmp = m % 2;
    s += tmp + '0';
    m = m / 2;
  }
  reverse(s.begin(), s.end());
}
 
// Function to find ith character
int find_character(int n, int m, int i) {
 
  string s;
 
  // Function to change decimal to binary
  binary_conversion(s, m);
 
  string s1 = "";
  for (int x = 0; x < n; x++) {
    for (int y = 0; y < s.length(); y++) {
      if (s[y] == '1')
        s1 += "10";
      else
        s1 += "01";     
    }
 
    // Assign s1 string in s string
    s = s1;
    s1 = "";
  }
  return s[i] - '0';
}
 
// Driver Function
int main() {
  int m = 5, n = 2, i = 8;
  cout << find_character(n, m, i);
  return 0;
}

Time complexity: quadratic
Space complexity: linear