Banana-Leopard/TASTY huggingface dataset:
# A O(n) solution for finding rank of string
MAX_CHAR = 256
 
# All elements of count[] are initialized with 0
count = [0]*(MAX_CHAR + 1)
 
# A utility function to find factorial of n
def fact(n):
    return 1 if(n <= 1) else (n * fact(n - 1))
 
# Construct a count array where value at every index
# contains count of smaller characters in whole string
def populateAndIncreaseCount(str):
    for i in range(len(str)):
        count[ord(str[i])] += 1
 
    for i in range(1, MAX_CHAR):
        count[i] += count[i - 1]
 
# Removes a character ch from count[] array
# constructed by populateAndIncreaseCount()
def updatecount(ch):
 
    for i in range(ord(ch), MAX_CHAR):
        count[i] -= 1
 
# A function to find rank of a string in all permutations
# of characters
def findRank(str):
    len1 = len(str)
    mul = fact(len1)
    rank = 1
 
    # Populate the count array such that count[i]
    # contains count of characters which are present
    # in str and are smaller than i
    populateAndIncreaseCount(str)
 
    for i in range(len1):
        mul = mul//(len1 - i)
 
        # count number of chars smaller than str[i]
        # from str[i+1] to str[len-1]
        rank += count[ord(str[i]) - 1] * mul
 
        # Reduce count of characters greater than str[i]
        updatecount(str[i])
 
    return rank
 
 
# Driver code
if __name__ == '__main__':
    str = "string"
    print(findRank(str))
 
# This is code is contributed by chandan_jnu

Time complexity: linear
Space complexity: constant
# Python3 Program for Bad Character Heuristic
# of Boyer Moore String Matching Algorithm
 
NO_OF_CHARS = 256
 
def badCharHeuristic(string, size):
    '''
    The preprocessing function for
    Boyer Moore's bad character heuristic
    '''
 
    # Initialize all occurrence as -1
    badChar = [-1]*NO_OF_CHARS
 
    # Fill the actual value of last occurrence
    for i in range(size):
        badChar[ord(string[i])] = i;
 
    # return initialized list
    return badChar
 
def search(txt, pat):
    '''
    A pattern searching function that uses Bad Character
    Heuristic of Boyer Moore Algorithm
    '''
    m = len(pat)
    n = len(txt)
 
    # create the bad character list by calling
    # the preprocessing function badCharHeuristic()
    # for given pattern
    badChar = badCharHeuristic(pat, m)
 
    # s is shift of the pattern with respect to text
    s = 0
    while(s <= n-m):
        j = m-1
 
        # Keep reducing index j of pattern while
        # characters of pattern and text are matching
        # at this shift s
        while j>=0 and pat[j] == txt[s+j]:
            j -= 1
 
        # If the pattern is present at current shift,
        # then index j will become -1 after the above loop
        if j<0:
            print("Pattern occur at shift = {}".format(s))
 
            '''   
                Shift the pattern so that the next character in text
                      aligns with the last occurrence of it in pattern.
                The condition s+m < n is necessary for the case when
                   pattern occurs at the end of text
               '''
            s += (m-badChar[ord(txt[s+m])] if s+m<n else 1)
        else:
            '''
               Shift the pattern so that the bad character in text
               aligns with the last occurrence of it in pattern. The
               max function is used to make sure that we get a positive
               shift. We may get a negative shift if the last occurrence
               of bad character in pattern is on the right side of the
               current character.
            '''
            s += max(1, j-badChar[ord(txt[s+j])])
 
 
# Driver program to test above function
def main():
    txt = "ABAAABCD"
    pat = "ABC"
    search(txt, pat)
 
if __name__ == '__main__':
    main()
 
# This code is contributed by Atul Kumar
# (www.facebook.com/atul.kr.007)

Time complexity: quadratic
Space complexity: constant
# Python program to implement Manacher's Algorithm
  
def findLongestPalindromicString(text):
    N = len(text)
    if N == 0:
        return
    N = 2*N+1    # Position count
    L = [0] * N
    L[0] = 0
    L[1] = 1
    C = 1     # centerPosition
    R = 2     # centerRightPosition
    i = 0    # currentRightPosition
    iMirror = 0     # currentLeftPosition
    maxLPSLength = 0
    maxLPSCenterPosition = 0
    start = -1
    end = -1
    diff = -1
  
    # Uncomment it to print LPS Length array
    # printf("%d %d ", L[0], L[1]);
    for i in range(2,N):
      
        # get currentLeftPosition iMirror for currentRightPosition i
        iMirror = 2*C-i
        L[i] = 0
        diff = R - i
        # If currentRightPosition i is within centerRightPosition R
        if diff > 0:
            L[i] = min(L[iMirror], diff)
  
        # Attempt to expand palindrome centered at currentRightPosition i
        # Here for odd positions, we compare characters and
        # if match then increment LPS Length by ONE
        # If even position, we just increment LPS by ONE without
        # any character comparison
        try:
            while ((i + L[i]) < N and (i - L[i]) > 0) and \
                    (((i + L[i] + 1) % 2 == 0) or \
                    (text[(i + L[i] + 1) // 2] == text[(i - L[i] - 1) // 2])):
                L[i]+=1
        except Exception as e:
            pass
  
        if L[i] > maxLPSLength:        # Track maxLPSLength
            maxLPSLength = L[i]
            maxLPSCenterPosition = i
  
        # If palindrome centered at currentRightPosition i
        # expand beyond centerRightPosition R,
        # adjust centerPosition C based on expanded palindrome.
        if i + L[i] > R:
            C = i
            R = i + L[i]
  
    # Uncomment it to print LPS Length array
    # printf("%d ", L[i]);
    start = (maxLPSCenterPosition - maxLPSLength) // 2
    end = start + maxLPSLength - 1
    print ("LPS of string is " + text + " : ",text[start:end+1])
  
# Driver program
text1 = "babcbabcbaccba"
findLongestPalindromicString(text1)
  
text2 = "abaaba"
findLongestPalindromicString(text2)
  
text3 = "abababa"
findLongestPalindromicString(text3)
  
text4 = "abcbabcbabcba"
findLongestPalindromicString(text4)
  
text5 = "forgeeksskeegfor"
findLongestPalindromicString(text5)
  
text6 = "caba"
findLongestPalindromicString(text6)
  
text7 = "abacdfgdcaba"
findLongestPalindromicString(text7)
  
text8 = "abacdfgdcabba"
findLongestPalindromicString(text8)
  
text9 = "abacdedcaba"
findLongestPalindromicString(text9)
  
# This code is contributed by BHAVYA JAIN

Time complexity: linear
Space complexity: linear
# Python 3 program to print the
# string in 'plus' pattern
max = 100
 
# Function to make a cross
# in the matrix
def carveCross(str):
     
    n = len(str)
    if (n % 2 == 0) :
     
        ''' As, it is not possible to make
        the cross exactly in the middle of
        the matrix with an even length string.'''
        print("Not possible. Please enter "
            , "odd length string.\n")
     
    else :
 
        # declaring a 2D array i.e a matrix
        arr = [[ False for x in range(max)]
                       for y in range(max)]
        m = n // 2
 
        ''' Now, we will fill all the
        elements of the array with 'X'''
        for i in range( n) :
            for j in range(n) :
                arr[i][j] = 'X'
 
        '''Now, we will place the characters
        of the string in the matrix, such
        that a cross is formed in it.'''
        for i in range(n):
         
            ''' here the characters of the
            string will be added in the
            middle column of our array.'''
            arr[i][m] = str[i]
         
        for i in range(n):
         
            # here the characters of the
            # string will be added in the
            # middle row of our array.
            arr[m][i] = str[i]
 
        # Now finally, we will print
        # the array
        for i in range(n):
            for j in range(n):
                print( arr[i][j] , end=" ")
             
            print()
 
# Driver Code
if __name__ == "__main__":
    str = "PICTURE"
    carveCross(str)
 
# This code is contributed
# by ChitraNayal

Time complexity: quadratic
Space complexity: constant
# Python3 program to implement
# wildcard pattern matching
# algorithm
 
# Function that matches input
# txt with given wildcard pattern
def stringmatch(txt, pat, n, m):
     
    # empty pattern can only
    # match with empty sting
    # Base case
    if (m == 0):
        return (n == 0)
         
    # step 1
    # initialize markers :
    i = 0
    j = 0
    index_txt = -1
    index_pat = -1
    while(i < n - 2):
         
        # For step - (2, 5)
        if (j < m and txt[i] == pat[j]):
            i += 1
            j += 1
             
        # For step - (3)
        elif(j < m and pat[j] == '?'):
            i += 1
            j += 1
             
        # For step - (4)
        elif(j < m and pat[j] == '*'):
            index_txt = i
            index_pat = j
            j += 1
             
        # For step - (5)
        elif(index_pat != -1):
            j = index_pat + 1
            i = index_txt + 1
            index_txt += 1
             
        # For step - (6)
        else:
            return False
    # For step - (7)
    while (j < m and pat[j] == '*'):
        j += 1
 
    # Final Check
    if(j == m):
        return True
 
    return False
 
# Driver code
strr = "baaabab"
pattern = "*****ba*****ab"
 
# char pattern[] = "ba*****ab"
# char pattern[] = "ba * ab"
# char pattern[] = "a * ab"
if (stringmatch(strr, pattern, len(strr),
                               len(pattern))):
    print("Yes")
else:
    print( "No")
 
pattern2 = "a*****ab";
if (stringmatch(strr, pattern2, len(strr),
                                len(pattern2))):
    print("Yes")
else:
    print( "No")
 
# This code is contributed
# by sahilhelangia

Time complexity: linear
Space complexity: constant
# Python3 program to replace c1 with c2
# and c2 with c1
def replace(s, c1, c2):
    l = len(s)
     
    # loop to traverse in the string
    for i in range(l):
         
        # check for c1 and replace
        if (s[i] == c1):
            s = s[0:i] + c2 + s[i + 1:]
         
        # check for c2 and replace
        elif (s[i] == c2):
            s = s[0:i] + c1 + s[i + 1:]
    return s
 
# Driver Code
if __name__ == '__main__':
    s = "grrksfoegrrks"
    c1 = 'e'
    c2 = 'r'
    print(replace(s, c1, c2))
 
# This code is contributed
# by PrinciRaj1992

Time complexity: linear
Space complexity: constant
# Python program to calculate number of times
# the pattern occurred in given string
 
# Returns count of occurrences of "1(0+)1"
def countPattern(s):
    length = len(s)
    oneSeen = False
     
    count = 0   # Initialize result
    for i in range(length):
 
        # Check if encountered '1' forms a valid
        # pattern as specified
        if (s[i] == '1' and oneSeen):
            if (s[i - 1] == '0'):
                count += 1
 
        # if 1 encountered for first time
        # set oneSeen to 1
        if (s[i] == '1' and oneSeen == 0):
            oneSeen = True
  
        # Check if there is any other character
        # other than '0' or '1'. If so then set
        # oneSeen to 0 to search again for new
        # pattern
        if (s[i] != '0' and s[i] != '1'):
            oneSeen = False
  
 
  
    return count
 
# Driver code
s = "100001abc101"
print(countPattern(s))
 
# This code is contributed by Sachin Bisht

Time complexity: linear
Space complexity: constant
# Python3 program to find if a string follows
# order defined by a given pattern
CHAR_SIZE = 256
 
# Returns true if characters of str follow
# order defined by a given ptr.
def checkPattern(Str, pat):
 
    # Initialize all orders as -1
    label = [-1] * CHAR_SIZE
 
    # Assign an order to pattern characters
    # according to their appearance in pattern
    order = 1
     
    for i in range(len(pat)):
 
        # Give the pattern characters order
        label[ord(pat[i])] = order
 
        # Increment the order
        order += 1
 
    # Now one by one check if string
    # characters follow above order
    last_order = -1
     
    for i in range(len(Str)):
        if (label[ord(Str[i])] != -1):
 
            # If order of this character is less
            # than order of previous, return false
            if (label[ord(Str[i])] < last_order):
                return False
 
            # Update last_order for next iteration
            last_order = label[ord(Str[i])]
 
    # return that str followed pat
    return True
 
# Driver Code
if __name__ == '__main__':
     
    Str = "engineers rock"
    pattern = "gsr"
 
    print(checkPattern(Str, pattern))
 
# This code is contributed by himanshu77

Time complexity: linear
Space complexity: constant
# Python code for finding count
# of string in a given 2D
# character array.
 
# utility function to search
# complete string from any
# given index of 2d array
def internalSearch(ii, needle, row, col, hay,
                    row_max, col_max):
     
    found = 0
    if (row >= 0 and row <= row_max and
        col >= 0 and col <= col_max and
        needle[ii] == hay[row][col]):
        match = needle[ii]
        ii += 1
        hay[row][col] = 0
        if (ii == len(needle)):
            found = 1
        else:
             
            # through Backtrack searching
            # in every directions
            found += internalSearch(ii, needle, row,
                               col + 1, hay, row_max, col_max)
            found += internalSearch(ii, needle, row,
                               col - 1, hay, row_max, col_max)
            found += internalSearch(ii, needle, row + 1,
                               col, hay, row_max, col_max)
            found += internalSearch(ii, needle, row - 1,
                               col, hay, row_max, col_max)
        hay[row][col] = match
    return found
 
# Function to search the string in 2d array
def searchString(needle, row, col,strr,
                row_count, col_count):
    found = 0
    for r in range(row_count):
        for c in range(col_count):
            found += internalSearch(0, needle, r, c,
                        strr, row_count - 1, col_count - 1)
             
    return found
 
# Driver code
 
needle = "MAGIC"
inputt = ["BBABBM","CBMBBA","IBABBG",
            "GOZBBI","ABBBBC","MCIGAM"]
 
strr = [0] * len(inputt)
 
for i in range(len(inputt)):
    strr[i] = list(inputt[i])
     
print("count: ", searchString(needle, 0, 0, strr,
                        len(strr), len(strr[0])))
 
# This code is contributed by SHUBHAMSINGH10

Time complexity: quadratic
Space complexity: quadratic
# Python3 program to check if we can
# break a into four distinct strings.
 
# Return if the given string can be
# split or not.
def check(s):
 
    # We can always break a of size 10 or
    # more into four distinct strings.
    if (len(s) >= 10):
        return True
 
    # Brute Force
    for i in range(1, len(s)):
     
        for j in range(i + 1, len(s)):
         
            for k in range(j + 1, len(s)):
             
                # Making 4 from the given
                s1 = s[0:i]
                s2 = s[i:j - i]
                s3 = s[j: k - j]
                s4 = s[k: len(s) - k]
                 
                # Checking if they are distinct or not.
                if (s1 != s2 and s1 != s3 and s1 != s4 and
                    s2 != s3 and s2 != s4 and s3 != s4):
                    return True
             
    return False
 
# Driver Code
if __name__ == '__main__':
    str = "aaabb"
 
    print("Yes") if(check(str)) else print("NO")
 
# This code is contributed
# by SHUBHAMSINGH10

Time complexity: np
Space complexity: linear
# Python 3 program to split an alphanumeric
# string using STL
def splitString(str):
 
    alpha = ""
    num = ""
    special = ""
    for i in range(len(str)):
        if (str[i].isdigit()):
            num = num+ str[i]
        elif((str[i] >= 'A' and str[i] <= 'Z') or
             (str[i] >= 'a' and str[i] <= 'z')):
            alpha += str[i]
        else:
            special += str[i]
 
    print(alpha)
    print(num )
    print(special)
 
# Driver code
if __name__ == "__main__":
     
    str = "geeks01$$for02geeks03!@!!"
    splitString(str)
 
# This code is contributed by ita_c

Time complexity: linear
Space complexity: linear
# Python3 program to split a numeric
# string in an Increasing
# sequence if possible
 
# Function accepts a string and
# checks if string can be split.
def split(Str) :
    Len = len(Str)
  
    # if there is only 1 number
    # in the string then
    # it is not possible to split it
    if (Len == 1) :
        print("Not Possible")
        return
  
    s1, s2 = "", ""
    for i in range((Len // 2) + 1) :
        flag = 0
  
        # storing the substring from
        # 0 to i+1 to form initial
        # number of the increasing sequence
        s1 = Str[0 : i + 1]
        num1 = int(s1)
        num2 = num1 + 1
  
        # convert string to integer
        # and add 1 and again convert
        # back to string s2
        s2 = str(num2)
        k = i + 1
        while (flag == 0) :
            l = len(s2)
  
            # if s2 is not a substring
            # of number than not possible
            if (k + l > Len) :
                flag = 1
                break
  
            # if s2 is the next substring
            # of the numeric string
            if ((Str[k : k + l] == s2)) :
                flag = 0
  
                # Increase num2 by 1 i.e the
                # next number to be looked for
                num2 += 1
                k = k + l
  
                # check if string is fully
                # traversed then break
                if (k == Len) :
                    break
                s2 = str(num2)
                l = len(s2)
                if (k + 1 > len) :
  
                    # If next string doesnot occurs
                    # in a given numeric string
                    # then it is not possible
                    flag = 1
                    break
            else :
                flag = 1
  
        # if the string was fully traversed
        # and conditions were satisfied
        if (flag == 0) :
            print("Possible",  s1)
            break
  
        # if conditions failed to hold
        elif (flag == 1 and i > (Len // 2) - 1) :
            print("Not Possible")
            break
         
# Driver code       
Str = "99100"
    
# Call the split function
# for splitting the string
split(Str)
 
# This code is contributed by divyesh072019.

Time complexity: quadratic
Space complexity: linear
# Python3 Program to find number of way
# to split string such that each partition
# starts with distinct character with
# maximum number of partitions.
 
# Returns the number of we can split
# the string
def countWays(s):
    count = [0] * 26;
 
    # Finding the frequency of each
    # character.
    for x in s:
        count[ord(x) -
              ord('a')] = (count[ord(x) -
                                 ord('a')]) + 1;
 
    # making frequency of first character
    # of string equal to 1.
    count[ord(s[0]) - ord('a')] = 1;
 
    # Finding the product of frequency
    # of occurrence of each character.
    ans = 1;
    for i in range(26):
        if (count[i] != 0):
            ans *= count[i];
 
    return ans;
 
# Driver Code
if __name__ == '__main__':
    s = "acbbcc";
    print(countWays(s));
 
# This code is contributed by Rajput-Ji

Time complexity: linear
Space complexity: constant
# Python3 program to check if a can be splitted
# into two strings such that one is divisible by 'a'
# and other is divisible by 'b'.
 
# Finds if it is possible to partition str
# into two parts such that first part is
# divisible by a and second part is divisible
# by b.
def findDivision(str, a, b):
    lenn = len(str)
     
    # Create an array of size lenn+1 and
    # initialize it with 0.
    # Store remainders from left to right
    # when divided by 'a'
    lr = [0] * (lenn + 1)
    lr[0] = (int(str[0]))%a
    for i in range(1, lenn):
        lr[i] = ((lr[i - 1] * 10) % a + \
                     int(str[i])) % a
                      
    # Compute remainders from right to left
    # when divided by 'b'
    rl = [0] * (lenn + 1)
    rl[lenn - 1] = int(str[lenn - 1]) % b
    power10 = 10
    for i in range(lenn - 2, -1, -1):
        rl[i] = (rl[i + 1] + int(str[i]) * power10) % b
        power10 = (power10 * 10) % b
         
    # Find a point that can partition a number
    for i in range(0, lenn - 1):
         
        # If split is not possible at this point
        if (lr[i] != 0):
            continue
             
        # We can split at i if one of the following
        # two is true.
        # a) All characters after str[i] are 0
        # b) after str[i] is divisible by b, i.e.,
        # str[i+1..n-1] is divisible by b.
        if (rl[i + 1] == 0):
            print("YES")
            for k in range(0, i + 1):
                print(str[k], end = "")
             
            print(",", end = " ")
             
            for i in range(i + 1, lenn):
                print(str[k], end = "")
                return
     
    print("NO")
 
# Driver code
str = "123"
a, b = 12, 3
findDivision(str, a, b)
 
# This code is contributed by SHUBHAMSINGH10

Time complexity: linear
Space complexity: linear
# Python program to check if a string can be splitted
# into two strings such that one is divisible by 'a'
# and other is divisible by 'b'.
 
# Finds if it is possible to partition str
# into two parts such that first part is
# divisible by a and second part is divisible
# by b.
def findDivision(S, a, b):
 
    for i in range(len(S)-1):
 
        firstPart = S[0: i + 1]
        secondPart = S[i + 1:]
 
        if (int(firstPart) % a == 0
            and int(secondPart) % b == 0):
            return firstPart + " " + secondPart
 
    return "-1"
 
# Driver code
Str = "125"
a,b = 12,3
result = findDivision(Str, a, b)
 
if (result == "-1"):
    print("NO")
 
else:
    print("YES")
    print(result)
 
# This code is contributed by shinjanpatra

Time complexity: linear
Space complexity: constant
# Python3 code to implement the approach
 
# This code kind of uses sliding window technique. First
# checking if string[0] and string[0..n-1] is divisible if
# yes then return else run a loop from 1 to n-1 and check if
# taking this (0-i)index number and (i+1 to n-1)index number
# on our two declared variables if they are divisible by given two numbers respectively
# in any iteration return them simply
def stringPartition(s, a, b):
    # code here
    n = len(s)
 
    # if length is 1 not possible
    if (n == 1):
        return "-1"
 
    else:
        # Checking if number formed bt S[0] and S[1->n-1] is divisible
        a1 = int(s[0])
        a2 = int(s[1])
        multiplyer = 10
        for i in range(2, n):
            a2 = a2 * multiplyer + int(s[i])
 
        i = 1
        if (a1 % a == 0 and a2 % b == 0):
            k1 = '1' * (s[0])
            k2 = ""
            for j in range(1, n):
                k2 += s[j]
            return k1 + " " + k2   # return the numbers formed as string
 
        # from here by using sliding window technique we
        # will iterate and check for every i that if the
        # two current numbers formed are divisible if yes
        # return else form the two new numbers for next
        # iteration using sliding window technique
        q1 = 10
        q2 = 1
        for i in range(1, n - 1):
            q2 *= 10
        while (i < n - 1):
            x = s[i]
            ad = int(x)
            a1 = a1 * q1 + ad
            a2 = a2 - q2 * ad
            if (a1 % a == 0 and a2 % b == 0):
                k1 = ""
                k2 = ""
                for j in range(i + 1):
                    k1 += s[j]
                for j in range(i + 1, n):
                    k2 += s[j]
                return k1 + " " + k2
 
            q2 //= 10
            i += 1
 
    return "-1"
 
# Driver code
str = "123"
a = 12
b = 3
result = stringPartition(str, a, b)
 
if (result == "-1"):
    print("NO")
else:
    print("YES")
    print(result)
 
# This code is contributed by phasing17

Time complexity: linear
Space complexity: constant