Banana-Leopard/TASTY huggingface dataset:
# A recursive Python program
# to check whether a given
# number is palindrome or not
 
# A recursive function that
# check a str[s..e] is
# palindrome or not.
def isPalRec(st, s, e) :
     
    # If there is only one character
    if (s == e):
        return True
 
    # If first and last
    # characters do not match
    if (st[s] != st[e]) :
        return False
 
    # If there are more than
    # two characters, check if
    # middle substring is also
    # palindrome or not.
    if (s < e + 1) :
        return isPalRec(st, s + 1, e - 1);
 
    return True
 
def isPalindrome(st) :
    n = len(st)
     
    # An empty string is
    # considered as palindrome
    if (n == 0) :
        return True
     
    return isPalRec(st, 0, n - 1);
 
 
# Driver Code
st = "geeg"
if (isPalindrome(st)) :
    print "Yes"
else :
    print "No"
     
# This code is contributed
# by Nikita Tiwari.

Time complexity: linear
Space complexity: linear
def isPalindrome(s, i):
    if(i > len(s)/2):
       return True
    ans = False
    if((s[i] is s[len(s) - i - 1]) and isPalindrome(s, i + 1)):
      ans = True
    return ans
 
str = "geeg"
if (isPalindrome(str, 0)):
    print("Yes")
else:
    print("No")
     
# This code is contributed by akashish__

Time complexity: linear
Space complexity: linear
# Python 3 implementation to find maximum
# length substring which is not palindrome
 
# utility function to check whether
# a string is palindrome or not
def isPalindrome(str):
     
    # Check for palindrome.
    n = len(str)
    for i in range(n // 2):
        if (str[i] != str[n - i - 1]):
            return False
 
    # palindrome string
    return True
 
# function to find maximum length
# substring which is not palindrome
def maxLengthNonPalinSubstring(str):
    n = len(str)
    ch = str[0]
 
    # to check whether all characters
    # of the string are same or not
    i = 1
    for i in range(1, n):
        if (str[i] != ch):
            break
 
    # All characters are same, we can't
    # make a non-palindromic string.
    if (i == n):
        return 0
 
    # If string is palindrome, we can make
    # it non-palindrome by removing any
    # corner character
    if (isPalindrome(str)):
        return n - 1
 
    # Complete string is not a palindrome.
    return n
 
# Driver Code
if __name__ == "__main__":
     
    str = "abba"
    print("Maximum length =",
           maxLengthNonPalinSubstring(str))
 
# This code is contributed by ita_c

Time complexity: linear
Space complexity: linear
# Python3 program to query the number of
# palindromic substrings of a string in a range
M = 50
 
# Utility method to construct the dp array
def constructDP(dp, string):
 
    l = len(string)
 
    # declare 2D array isPalin, isPalin[i][j]
    # will be 1 if str(i..j) is palindrome
    # and initialize it with zero
    isPalin = [[0 for i in range(l + 1)]
                  for j in range(l + 1)]
 
    # loop for starting index of range
    for i in range(l - 1, -1, -1):
 
        # initialize value for one
        # character strings as 1
        isPalin[i][i], dp[i][i] = 1, 1
 
        # loop for ending index of range
        for j in range(i + 1, l):
 
            # 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] = (string[i] == string[j] and
                            (i + 1 > j - 1 or 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)
def countOfPalindromeInRange(dp, l, r):
    return dp[l][r]
 
# Driver code
if __name__ == "__main__":
 
    string = "xyaabax"
    dp = [[0 for i in range(M)]
             for j in range(M)]
     
    constructDP(dp, string)
 
    l, r = 3, 5
    print(countOfPalindromeInRange(dp, l, r))
 
# This code is contributed by Rituraj Jain

Time complexity: quadratic
Space complexity: quadratic
# Python3 program to find n=th even
# length string.
import math as mt
 
# Function to find nth even length
# Palindrome
 
 
def evenlength(n):
 
    # string r to store resultant
    # palindrome. Initialize same as s
    res = n
 
    # In this loop string r stores
    # reverse of string s after the
    # string s in consecutive manner .
    for j in range(len(n) - 1, -1, -1):
        res += n[j]
 
    return res
 
 
# Driver code
n = "10"
 
# Function call
print(evenlength(n))
 
# This code is contributed by
# Mohit kumar 29

Time complexity: linear
Space complexity: linear
# code to make 'ab' free string
def abFree(s):
    
    # Traverse from end. Keep track of count
    # b's. For every 'a' encountered, add b_count
    # to result and double b_count.
    b_count = 0
    res = 0
    for i in range(len(s)):
        if s[~i] == 'a':
            res = (res + b_count)
            b_count  = (b_count  * 2)
        else:
            b_count  += 1
    return res
 
# driver code
s = 'abbaa'
print(abFree(s))
 
s = 'aab'
print(abFree(s))
 
s ='ababab'
print(abFree(s))

Time complexity: linear
Space complexity: linear
# Python 3 program to find minimum number of
# flip to make binary string alternate
 
# Utility method to flip a character
def flip( ch):
    return '1' if (ch == '0') else '0'
 
# Utility method to get minimum flips when
# alternate string starts with expected char
def getFlipWithStartingCharcter(str, expected):
 
    flipCount = 0
    for i in range(len( str)):
         
        # if current character is not expected,
        # increase flip count
        if (str[i] != expected):
            flipCount += 1
 
        # flip expected character each time
        expected = flip(expected)
    return flipCount
 
# method return minimum flip to make binary
# string alternate
def minFlipToMakeStringAlternate(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
if __name__ == "__main__":
     
    str = "0001010111"
    print(minFlipToMakeStringAlternate(str))

Time complexity: linear
Space complexity: constant
# An efficient Python 3 program to
# find 2's complement
 
# Function to find two's complement
def findTwoscomplement(str):
    n = len(str)
 
    # Traverse the string to get first
    # '1' from the last of string
    i = n - 1
    while(i >= 0):
        if (str[i] == '1'):
            break
 
        i -= 1
 
    # 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'
    k = i - 1
    while(k >= 0):
         
        # Just flip the values
        if (str[k] == '1'):
            str = list(str)
            str[k] = '0'
            str = ''.join(str)
        else:
            str = list(str)
            str[k] = '1'
            str = ''.join(str)
 
        k -= 1
 
    # return the modified string
    return str
 
# Driver code
if __name__ == '__main__':
    str = "00000101"
    print(findTwoscomplement(str))
 
# This code is contributed by
# Sanjit_Prasad

Time complexity: linear
Space complexity: constant
# Python 3 program to count all
# distinct binary strings with
# two consecutive 1's
 
 
# Returns count of n length
# binary strings with
# consecutive 1's
def countStrings(n):
 
    # Count binary strings without
    # consecutive 1's.
    # See the approach discussed on be
    # ( http://goo.gl/p8A3sW )
    a = [0] * n
    b = [0] * n
    a[0] = b[0] = 1
    for i in range(1, n):
        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
print(countStrings(5))
 
 
# This code is contributed
# by Nikita tiwari.

Time complexity: linear
Space complexity: linear
# Recursive Python program to generate all
# binary strings formed by replacing
# each wildcard character by 0 or 1
 
# Recursive function to generate all binary
# strings formed by replacing each wildcard
# character by 0 or 1
def _print(string, index):
    if index == len(string):
        print(''.join(string))
        return
 
    if string[index] == "?":
 
        # replace '?' by '0' and recurse
        string[index] = '0'
        _print(string, index + 1)
 
        # replace '?' by '1' and recurse
        string[index] = '1'
        _print(string, index + 1)
 
        # NOTE: Need to backtrack as string
        # is passed by reference to the
        # function
        string[index] = '?'
    else:
        _print(string, index + 1)
 
# Driver code
if __name__ == "__main__":
 
    string = "1??0?101"
    string = list(string)
    _print(string, 0)
 
    # This code is contributed by
    # sanjeev2552
 
# Note: function name _print is used because
# print is already a predefined function in Python

Time complexity: np
Space complexity: quadratic
# Iterative Python program to generate all binary
# strings formed by replacing each wildcard
# character by 0 or 1
  
# Iterative function to generate all binary strings
# formed by replacing each wildcard character by 0
# or 1
def Print(Str):
    q = []
    q.append(Str)
    while(len(q) > 0):
        Str = q[0]
         
        # find position of first occurrence of wildcard
        try:
            index = Str.index('?')
        except ValueError:
            index = -1
             
        # If no matches were found,
        # find returns -1
        if(index != -1):
            
            # replace '?' by '0' and push string into queue
            s1=Str.replace('?','0',1)
            q.append(s1)
             
            # replace '?' by '1' and push string into queue
            s2=Str.replace('?','1',1)
            q.append(s2)
        else:
            # If no wildcard characters are left,
            # print the string.
            print(Str)
        q.pop(0)
             
# Driver code
Str = "1??0?101"
Print(Str)
 
# This code is contributed by Pushpesh Raj

Time complexity: quadratic
Space complexity: np
#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
 
res = []
def genBin(s):
    if '?' in s:
        s1 = s.replace('?','0',1) #only replace once
        s2 = s.replace('?','1',1) #only replace once
        genBin(s1)
        genBin(s2)
    else: res.append(s)
 
# Driver code
genBin("1??0?101")
print(res)
 
# This code is contributed by
# divay pandey

Time complexity: quadratic
Space complexity: np
# The function that adds two-bit sequences and returns the addition
def addBitStrings(str1, str2):
    ans = ''
    i = len(str1) - 1
    j = len(str2) - 1
    carry = 0
    while i >= 0 or j >= 0 or carry:
        if i >= 0:
            carry += ord(str1[i]) - ord('0')
            i = i - 1
        else:
            carry += 0
 
        if j >= 0:
            carry += ord(str2[j]) - ord('0')
            j = j - 1
        else:
            carry += 0
 
        ans = chr(ord('0') + carry % 2) + ans
        carry = carry // 2
    return ans
 
 
# Driver program to test above functions
str1 = '1100011'
str2 = '10'
 
print('Sum is ', addBitStrings(str1, str2))
 
# This code is contributed by ajaymakavan.

Time complexity: linear
Space complexity: constant
# Python program to count
# all distinct binary strings
# without two consecutive 1's
 
def countStrings(n):
 
    a=[0 for i in range(n)]
    b=[0 for i in range(n)]
    a[0] = b[0] = 1
    for i in range(1,n):
        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
 
print(countStrings(3))
 
# This code is contributed
# by Anant Agarwal.

Time complexity: linear
Space complexity: linear
class Subset_sum :
    @staticmethod
    def  countStrings( n) :
        a = 1
        b = 1
        i = 1
        while (i < n) :
            # Here we have used the temp variable because
            # we want to assign the older value of a to b
            temp = a + b
            b = a
            a = temp
            i += 1
        return a + b
       
    # Driver program to test above function
    @staticmethod
    def main( args) :
        print(Subset_sum.countStrings(3))
     
 
if __name__=="__main__":
    Subset_sum.main([])
     
    # This code is contributed by aadityaburujwale.

Time complexity: linear
Space complexity: constant
# Python 3program to check if a
# string is of the form a^nb^n.
  
# Returns true str is of the
# form a^nb^n.
  
  
def isAnBn(str):
  
    n = len(str)
  
    # After this loop 'i' has
    # count of a's
    for i in range(n):
        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'
    for j in range(i, n):
        if (str[j] != 'b'):
            return False
  
    return True
  
  
# Driver code
if __name__ == "__main__":
    str = "abab"
    print("Yes") if isAnBn(str) else print("No")
  
# This code is contributed
# by ChitraNayal

Time complexity: linear
Space complexity: constant
# Python3 code to check
# a^nb^n pattern
  
def isanbn(str):
  n=len(str)
    
  # if length of str is odd return No
  if n&1:
    return "No"
    
  # check first half is 'a' and other half is full of 'b'
  for i in range(int(n/2)):
    if str[i]!='a' or str[n-i-1]!='b':
      return "No"
  return "Yes"
      
# Driver code
input_str = "ab"
  
# Function call
print(isanbn(input_str))

Time complexity: linear
Space complexity: constant
# Python3 implementation to find the binary
# representation of next greater integer
 
# function to find the required
# binary representation
def nextGreater(num1):
 
    l = len(num1);
    num = list(num1);
 
    # examine bits from the right
    i = l-1;
    while(i >= 0):
        # if '0' is encountered, convert
        # it to '1' and then break
        if (num[i] == '0'):
            num[i] = '1';
            break;
 
        # else convert '1' to '0'
        else:
            num[i] = '0';
        i-=1;
 
    # if the binary representation
    # contains only the set bits
    num1 = ''.join(num);
    if (i < 0):
        num1 = '1' + num1;
 
    # final binary representation
    # of the required integer
    return num1;
 
# Driver Code
num = "10011";
print("Binary representation of next number = ",nextGreater(num));
 
# This code is contributed by mits

Time complexity: linear
Space complexity: linear
# Python3 program to find next permutation in a
# binary string.
 
# Function to find the next greater number
# with same number of 1's and 0's
def nextGreaterWithSameDigits(bnum):
    l = len(bnum)
    bnum = list(bnum)
    for i in range(l - 2, 0, -1):
         
        # locate first 'i' from end such that
        # bnum[i]=='0' and bnum[i+1]=='1'
        # swap these value and break
        if (bnum[i] == '0' and bnum[i + 1] == '1'):
            ch = bnum[i]
            bnum[i] = bnum[i + 1]
            bnum[i + 1] = ch        
            break
         
    # if no swapping performed
    if (i == 0):
        return "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'
    j = i + 2
    k = l - 1
    while (j < k):
        if (bnum[j] == '1' and bnum[k] == '0'):
            ch = bnum[j]
            bnum[j] = bnum[k]
            bnum[k] = ch
            j += 1
            k -= 1
             
        # special case while swapping if '0'
        # occurs then break
        else if (bnum[i] == '0'):
            break
        else:
            j += 1
     
    # required next greater number
    return bnum
 
# Driver code
bnum = "10010"
print("Binary representation of next greater number = ",*nextGreaterWithSameDigits(bnum),sep="")
 
# This code is contributed by shubhamsingh10

Time complexity: linear
Space complexity: constant
# Python Program to find the length of
# substring with maximum difference of
# zeros and ones in binary string.
 
# Returns the length of substring with
# maximum difference of zeroes and ones
# in binary string
def findLength(string, n):
    current_sum = 0
    max_sum = 0
 
    # traverse a binary string from left
    # to right
    for i in range(n):
 
        # add current value to the current_sum
        # according to the Character
        # if it's '0' add 1 else -1
        current_sum += (1 if string[i] == '0' else -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 if max_sum else 0
 
# Driven Program
s = "11000010001"
n = 11
print(findLength(s, n))
 
# This code is contributed by Ansu Kumari.

Time complexity: linear
Space complexity: linear
# Python3 regex program to check for valid string
import re
 
# Method to check for valid string
def checkString(str):
    # regular expression for invalid string
    regex = "10+1"
    x = re.search("10+1", str)
    return x is None
 
#Driver method
str = "00011111111100000"
 
if checkString(str):
    print("VALID")
else:
    print("NOT VALID")
     
#this code is contributed by phasing17

Time complexity: linear
Space complexity: constant
# Python 3 program to find min flips in 
# binary string to make all characters equal
  
# To find min number of flips in
# binary string
def findFlips(str, n):
  
    last = ' '
    res = 0
  
    for i in range( n) :
  
        # If last character is not equal
        # to str[i] increase res
        if (last != str[i]):
            res += 1
        last = str[i]
  
    # To return min flips
    return res // 2
  
# Driver Code
if __name__ == "__main__":
      
    str = "00011110001110"
    n = len(str)
  
    print(findFlips(str, n))
  
# This code is contributed by ita_c

Time complexity: linear
Space complexity: constant
# Python Solution for above problem:
  
# This function adds two binary 
# strings return the resulting string
def add_binary_nums(x, y):
        max_len = max(len(x), len(y))
  
        x = x.zfill(max_len)
        y = y.zfill(max_len)
          
        # initialize the result
        result = ''
          
        # initialize the carry
        carry = 0
  
        # Traverse the string
        for i in range(max_len - 1, -1, -1):
            r = carry
            r += 1 if x[i] == '1' else 0
            r += 1 if y[i] == '1' else 0
            result = ('1' if r % 2 == 1 else '0') + result
            carry = 0 if r < 2 else 1     # Compute the carry.
          
        if carry !=0 : result = '1' + result
  
        return result.zfill(max_len)
  
# Driver code 
print(add_binary_nums('1101', '100'))
  
# This code is contributed 
# by Anand Khatri

Time complexity: linear
Space complexity: linear
# Python 3 program to convert
# string into binary string
 
# utility function
def strToBinary(s):
    bin_conv = []
 
    for c in s:
         
        # convert each char to
        # ASCII value
        ascii_val = ord(c)
         
        # Convert ASCII value to binary
        binary_val = bin(ascii_val)
        bin_conv.append(binary_val[2:])
         
    return (' '.join(bin_conv))
 
# Driver Code
if __name__ == '__main__':
    s = 'geeks'
 
print (strToBinary(s))
 
# This code is contributed
# by Vikas Chitturi

Time complexity: linear
Space complexity: linear
# Python3 program to Generate all binary string
# without consecutive 1's of size K
 
# A utility function generate all string without
# consecutive 1'sof size K
def generateAllStringsUtil(K, str, n):
     
    # print binary string without consecutive 1's
    if (n == K):
         
        # terminate binary string
        print(*str[:n], sep = "", end = " ")
        return
     
    # if previous character is '1' then we put
    # only 0 at end of string
    # example str = "01" then new string be "000"
    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
def generateAllStrings(K):
     
    # Base case
    if (K <= 0):
        return
     
    # One by one stores every
    # binary string of length K
    str = [0] * 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 code
K = 3
generateAllStrings (K)
 
# This code is contributed by SHUBHAMSINGH10

Time complexity: np
Space complexity: linear
def All_Binary_Strings(arr,num,r):
 
    if(r == num):
 
        for i in range(num):
            print(arr[i],end="")
        print(end=" ")
        return
     
    elif(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)
 
 
def Print(a,num):
    a[0] = 0
    All_Binary_Strings(a,num,1)
    a[0] = 1
    All_Binary_Strings(a,num,1)
 
 
# driver's code
 
n = 2
a = [False for i in range(n)]
Print(a,n)
 
# This code is contributed by shinjanpatra

Time complexity: np
Space complexity: linear
def All_Binary_Strings(str,num):
 
    Len = len(str)
    if(Len == num):
        print(str,end = " ")
        return
 
    elif(str[Len - 1]=='1'):
        All_Binary_Strings(str+'0',num)
    else:
        All_Binary_Strings(str+'0',num)
        All_Binary_Strings(str+'1',num)
 
def Print(num):
 
    word = ""
    word += '0'
    All_Binary_Strings(word,num)
    word = '1'
    All_Binary_Strings(word,num)
 
# Driver's code
n = 4
Print(n)
 
# This code is contributed by shinjanpatra.

Time complexity: np
Space complexity: linear
# Python 3 implementation to check
# whether given binary number is
# evenly divisible by 2^k or not
 
# function to check whether
# given binary number is
# evenly divisible by 2^k or not
def isDivisible(str, k):
    n = len(str)
    c = 0
     
    # count of number of 0 from last
    for i in range(0, k):
        if (str[n - i - 1] == '0'):    
            c += 1
     
    # if count = k, number is evenly
    # divisible, so returns true else
    # false
    return (c == k)
 
# Driver program to test above
# first example
str1 = "10101100"
k = 2
if (isDivisible(str1, k)):
    print("Yes")
else:
    print("No")
 
# Second example
str2 = "111010100"
k = 2
if (isDivisible(str2, k)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Smitha

Time complexity: constant
Space complexity: constant
# Python3 Program to find ith character in
# a binary string.
 
# Function to store binary Representation
def binary_conversion(s, m):
    while(m):
        temp = m % 2
        s += str(temp)
        m = m // 2
     
    return s[::-1]
 
# Function to find ith character
def find_character(n, m, i):
    s = ""
 
 
# Function to change decimal to binary
    s = binary_conversion(s, m)
    s1 = ""
 
    for x in range(n):
        for j in range(len(s)):
            if s[j] == "1":
                s1 += "10"
            else:
                s1 += "01"
 
    # Assign s1 string in s string    
        s = s1
        s1 = ""
    e = ord(s[i])
    r = ord('0')
 
    return e-r
 
# Driver code
m, n, i = 5, 2, 8
 
print(find_character(n,m,i))
 
# This code is contributed by mohit kumar 29

Time complexity: quadratic
Space complexity: linear