Banana-Leopard/TASTY huggingface dataset:
# Python 3 program to find LCS
# with permutations allowed
 
# Function to calculate longest string
# str1     --> first string
# str2     --> second string
# count1[] --> hash array to calculate frequency
#             of characters in str1
# count[2] --> hash array to calculate frequency
#             of characters in str2
# result --> resultant longest string whose
# permutations are sub-sequence
# of given two strings
def longestString(str1, str2):
 
    count1 = [0] * 26
    count2 = [0] * 26
 
    # calculate frequency of characters
    for i in range( len(str1)):
        count1[ord(str1[i]) - ord('a')] += 1
    for i in range(len(str2)):
        count2[ord(str2[i]) - ord('a')] += 1
 
    # Now traverse hash array
    result = ""
    for i in range(26):
 
        # append character ('a'+i) in
        # resultant string 'result' by
        # min(count1[i],count2i]) times
        for j in range(1, min(count1[i],
                            count2[i]) + 1):
            result = result + chr(ord('a') + i)
 
    print(result)
 
# Driver Code
if __name__ == "__main__":
     
    str1 = "geeks"
    str2 = "cake"
    longestString(str1, str2)
 
# This code is contributed by ita_c

Time complexity: linear
Space complexity: constant
# A simple recursive Python
# program to find shortest
# uncommon subsequence.
MAX = 1005
 
# A recursive function to
# find the length of shortest
# uncommon subsequence
def shortestSeq(S, T, m, n):
 
    # S String is empty
    if m == 0:
        return MAX
 
    # T String is empty
    if(n <= 0):
        return 1
 
    # Loop to search for
    # current character
    for k in range(n):
        if(T[k] == S[0]):
            break
 
    # char not found in T
    if(k == n):
        return 1
 
    # Return minimum of following
    # two Not including current
    # char in answer Including
    # current char
    return min(shortestSeq(S[1 : ], T, m - 1, n),
               1 + shortestSeq((S[1 : ]), T[k + 1 : ],
                                m - 1, n - k - 1))
 
# Driver code
S = "babab"
T = "babba"
 
m = len(S)
n = len(T)
ans = shortestSeq(S, T, m, n)
if(ans >= MAX):
    ans =- 1
print("Length of shortest subsequence is:", ans)
 
# This code is contributed by avanitrachhadiya2155

Time complexity: cubic
Space complexity: quadratic
# A dynamic programming based Python program
# to find shortest uncommon subsequence.
MAX = 1005
 
# Returns length of shortest common subsequence
def shortestSeq(S: list, T: list):
    m = len(S)
    n = len(T)
 
    # declaring 2D array of m + 1 rows and
    # n + 1 columns dynamically
    dp = [[0 for i in range(n + 1)]
             for j in range(m + 1)]
 
    # T string is empty
    for i in range(m + 1):
        dp[i][0] = 1
 
    # S string is empty
    for i in range(n + 1):
        dp[0][i] = MAX
 
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            ch = S[i - 1]
            k = j - 1
            while k >= 0:
                if T[k] == ch:
                    break
                k -= 1
 
            # char not present in T
            if k == -1:
                dp[i][j] = 1
            else:
                dp[i][j] = min(dp[i - 1][j],
                               dp[i - 1][k] + 1)
 
    ans = dp[m][n]
    if ans >= MAX:
        ans = -1
 
    return ans
 
# Driver Code
if __name__ == "__main__":
    S = "babab"
    T = "babba"
 
    print("Length of shortest subsequence is:",
                             shortestSeq(S, T))
 
# This code is contributed by
# sanjeev2552

Time complexity: cubic
Space complexity: quadratic
# A dynamic programming based Python3 program print
# shortest supersequence of two strings
 
# returns shortest supersequence of X and Y
def printShortestSuperSeq(m, n, x, y):
 
    # dp[i][j] contains length of shortest
    # supersequence for X[0..i-1] and Y[0..j-1]
    dp = [[0 for i in range(n + 1)]
             for j in range(m + 1)]
 
    # Fill table in bottom up manner
    for i in range(m + 1):
        for j in range(n + 1):
 
            # Below steps follow recurrence relation
            if i == 0:
                dp[i][j] = j
            elif j == 0:
                dp[i][j] = i
            elif x[i - 1] == y[j - 1]:
                dp[i][j] = 1 + dp[i - 1][j - 1]
            else:
                dp[i][j] = 1 + min(dp[i - 1][j],
                                   dp[i][j - 1])
 
    # Following code is used to print
    # shortest supersequence
 
    # dp[m][n] stores the length of the
    # shortest supersequence of X and Y
 
    # string to store the shortest supersequence
    string = ""
 
    # Start from the bottom right corner and
    # add the characters to the output string
    i = m
    j = n
    while i * j > 0:
 
        # If current character in X and Y are same,
        # then current character is part of
        # shortest supersequence
        if x[i - 1] == y[j - 1]:
 
            # Put current character in result
            string = x[i - 1] + string
 
            # reduce values of i, j and index
            i -= 1
            j -= 1
 
        # If current character in X and Y are different
        elif dp[i - 1][j] > dp[i][j - 1]:
 
            # Put current character of Y in result
            string = y[j - 1] + string
 
            # reduce values of j and index
            j -= 1
        else:
 
            # Put current character of X in result
            string = x[i - 1] + string
 
            # reduce values of i and index
            i -= 1
 
    # If Y reaches its end, put remaining characters
    # of X in the result string
    while i > 0:
        string = x[i - 1] + string
        i -= 1
 
    # If X reaches its end, put remaining characters
    # of Y in the result string
    while j > 0:
        string = y[j - 1] + string
        j -= 1
 
    return string
 
# Driver Code
if __name__ == "__main__":
    x = "GXTXAYB"
    y = "AGGTAB"
    m = len(x)
    n = len(y)
     
    # Take the smaller string as x and larger one as y
    if m > n:
      x, y = y, x
      m, n = n, m
     
    print(*printShortestSuperSeq(m, n, x, y))
 
# This code is contributed by
# sanjeev2552

Time complexity: quadratic
Space complexity: quadratic
# Space optimized Python
# implementation of LCS problem
  
# Returns length of LCS for 
# X[0..m-1], Y[0..n-1]
def lcs(X, Y):
      
    # Find lengths of two strings
    m = len(X)
    n = len(Y)
  
    L = [[0 for i in range(n+1)] for j in range(2)]
  
    # Binary index, used to index current row and
    # previous row.
    bi = bool
      
    for i in range(m):
        # Compute current binary index
        bi = i&1
  
        for j in range(n+1):
            if (i == 0 or j == 0):
                L[bi][j] = 0
  
            elif (X[i] == Y[j - 1]):
                L[bi][j] = L[1 - bi][j - 1] + 1
  
            else:
                L[bi][j] = max(L[1 - bi][j], 
                               L[bi][j - 1])
  
    # Last filled entry contains length of LCS
    # for X[0..n-1] and Y[0..m-1]
    return L[bi][n]
  
# Driver Code
X = "AGGTAB"
Y = "GXTXAYB"
  
print("Length of LCS is", lcs(X, Y))
  
# This code is contributed by Soumen Ghosh.

Time complexity: quadratic
Space complexity: linear
# Python3 program to find largest word in Dictionary
# by deleting some characters of given string
 
# Returns true if str1[] is a subsequence of str2[].
# m is length of str1 and n is length of str2
def isSubSequence(str1, str2):
 
    m = len(str1);
    n = len(str2);
 
    j = 0; # For index of str1 (or subsequence
 
    # Traverse str2 and str1, and compare current
    # character of str2 with first unmatched char
    # of str1, if matched then move ahead in str1
    i = 0;
    while (i < n and j < m):
        if (str1[j] == str2[i]):
            j += 1;
        i += 1;
 
    # If all characters of str1 were found in str2
    return (j == m);
 
# Returns the longest string in dictionary which is a
# subsequence of str.
def findLongestString(dict1, str1):
    result = "";
    length = 0;
 
    # Traverse through all words of dictionary
    for word in dict1:
         
        # If current word is subsequence of str and is largest
        # such word so far.
        if (length < len(word) and isSubSequence(word, str1)):
            result = word;
            length = len(word);
 
    # Return longest string
    return result;
 
# Driver program to test above function
 
dict1 = ["ale", "apple", "monkey", "plea"];
str1 = "abpcplea" ;
print(findLongestString(dict1, str1));
     
# This code is conribued by mits

Time complexity: quadratic
Space complexity: constant
# Python3 program to find largest word in Dictionary
# by deleting some characters of given string
 
res=""
 
def check(d,s):
    global res
    i = 0
    j = 0
    while(i < len(d) and j < len(s)):
 
        if(d[i] == s[j]):
            i += 1
            j += 1
        else:
            j += 1
 
        if(i == len(d) and len(res) < len(d)):
            res = d
 
def LongestWord(d, S):
   
    # sort the dictionary word
    # for smallest lexicographical order  
    d.sort()
 
    for c in d :
        check(c,S)
     
    return res
 
# Driver program
dict = [ "ale", "apple", "monkey", "plea" ]
str = "abpcplea"
print(LongestWord(dict, str))
 
# This code is contributed by shinjanpatra

Time complexity: quadratic
Space complexity: constant
# Python3 program to check if
# a string is perfect reversible or not
 
# This function basically checks
# if string is palindrome or not
def isReversible(str):
    i = 0; j = len(str) - 1;
 
    # iterate from left and right
    while (i < j):
        if (str[i] != str[j]):
            return False;
        i += 1;
        j -= 1;
    return True;
 
# Driver Code
str = "aba";
if (isReversible(str)):
    print("YES");
else:
    print("NO");
     
# This code is contributed by Princi Singh

Time complexity: linear
Space complexity: constant
# Python3 Program to reverse an equation
# Function to reverse order of words
def reverseEquation(s):
     
    # Reverse String
    result=""
    for i in range(len(s)):
         
        # A space marks the end of the word
        if(s[i]=='+' or s[i]=='-' or s[i]=='/' or s[i]=='*'):
             
            # insert the word at the beginning
            # of the result String
            result = s[i] + result
         
        # insert the symbol
        else:
            result = s[i] + result
    return result
 
# Driver Code
s = "a+b*c-d/e"
print(reverseEquation(s))
 
# This code is contributed by simranjenny84

Time complexity: linear
Space complexity: linear
# Python3 program for Left
# Rotation and Right
# Rotation of a String
 
# In-place rotates s towards left by d
def leftrotate(s, d):
    tmp = s[d : ] + s[0 : d]
    return tmp
   
# In-place rotates s
# towards right by d
def rightrotate(s, d):
   
   return leftrotate(s, len(s) - d)
 
# Driver code
if __name__=="__main__":
     
    str1 = "GeeksforGeeks"
    print(leftrotate(str1, 2))
  
    str2 = "GeeksforGeeks"
    print(rightrotate(str2, 2))
 
# This code is contributed by Rutvik_56

Time complexity: linear
Space complexity: constant
# Python3 program for Left
# Rotation and Right
# Rotation of a String
 
def leftrotate(str1, n):
    # extended string
    temp = str1 + str1
    l = len(str1)
    # Return string
    return temp[n :l+n]
def rightrotate(str1, n):
    return leftrotate(str1, len(str1)-n)
     
    return temp[l-n : l1-n  ]
# Driver code
if __name__=="__main__":
     
    str1 = "GeeksforGeeks"
    print(leftrotate(str1, 2))
 
    str2 = "GeeksforGeeks"
    print(rightrotate(str2, 2))
 
# This code is contributed by sam snehil

Time complexity: linear
Space complexity: linear
# A simple Python3 program to generate
# all rotations of a given string
 
# Print all the rotated strings.
def printRotatedString(str):
 
    lenn = len(str)
 
    # Generate all rotations
    # one by one and print
    temp = [0] * (lenn)
    for i in range(lenn):    
        j = i # Current index in str
        k = 0 # Current index in temp
 
        # Copying the second part from
        # the point of rotation.
        while (j < len(str)):
         
            temp[k] = str[j]
            k += 1
            j += 1
         
 
        # Copying the first part from
        # the point of rotation.
        j = 0
        while (j < i) :
         
            temp[k] = str[j]
            j += 1
            k += 1
         
 
        print(*temp, sep = "")
     
# Driver Code
if __name__ == '__main__':
    str = "geeks"
    printRotatedString(str)
     
# This code is contributed
# by SHUBHAMSINGH10

Time complexity: quadratic
Space complexity: linear
# An efficient Python3 program to print 
# all rotations of a string.
 
# Print all the rotated string.
def printRotatedString(string) :
     
    n = len(string)
 
    # Concatenate str with itself
    temp = string + string
 
    # Print all substrings of size n.
    # Note that size of temp is 2n
    for i in range(n) :
         
        for j in range(n) :
            print(temp[i + j], end = "")
             
        print()
 
# Driver Code
if __name__ == "__main__" :
 
    string = "geeks"
    printRotatedString(string)
 
# This code is contributed by Ryuga

Time complexity: quadratic
Space complexity: linear
# Python 3 program to determine minimum
# number of rotations required to yield
# same string.
 
# Returns count of rotations to get the
# same string back.
def findRotations(str):
     
    # tmp is the concatenated string.
    tmp = str + str
    n = len(str)
 
    for i in range(1, n + 1):
         
        # substring from i index of
        # original string size.
        substring = tmp[i: i+n]
 
        # if substring matches with
        # original string then we will
        # come out of the loop.
        if (str == substring):
            return i
    return n
 
# Driver code
if __name__ == '__main__':
 
    str = "abc"
    print(findRotations(str))
 
# This code is contributed
# by 29AjayKumar.

Time complexity: quadratic
Space complexity: linear
# Python program to determine minimum number
# of rotations required to yield same
# string.
 
# Returns count of rotations to get the
# same string back.
def findRotations(Str):
 
    ans = 0 # to store the answer
    n = len(Str) # length of the String
       
    # All the length where we can partition
    for i in range(1 , len(Str) - 1):
 
        # right part + left part = rotated String
        # we are checking whether the rotated String is equal to
        # original String
        if(Str[i: n] + Str[0: i]  == Str):
            ans = i
            break
 
    if(ans == 0):
        return n
    return ans
 
# Driver code
Str = "abc"
print(findRotations(Str))
 
# This code is contributed by shinjanpatra

Time complexity: quadratic
Space complexity: constant
# Python 3 program to determine minimum
# number of rotations required to yield
# same string.
  
# input
string = 'aaaa'
check = ''
  
for r in range(1, len(string)+1):
  # checking the input after each rotation
  check = string[r:] + string[:r]
    
  # following if statement checks if input is
  # equals to check , if yes it will print r and
  # break out of the loop
  if check == string:
    print(r)
    break
  
# This code is contributed
# by nagasowmyanarayanan.

Time complexity: quadratic
Space complexity: linear
# Python program to check if
# two strings are rotations
# of each other
def isRotation(a: str, b: str) -> bool:
    n = len(a)
    m = len(b)
    if (n != m):
        return False
 
    # create lps[] that
    # will hold the longest
    # prefix suffix values
    # for pattern
    lps = [0 for _ in range(n)]
 
    # length of the previous
    # longest prefix suffix
    length = 0
    i = 1
 
    # lps[0] is always 0
    lps[0] = 0
 
    # the loop calculates
    # lps[i] for i = 1 to n-1
    while (i < n):
        if (a[i] == b[length]):
            length += 1
            lps[i] = length
            i += 1
        else:
            if (length == 0):
                lps[i] = 0
                i += 1
            else:
                length = lps[length - 1]
    i = 0
 
    # Match from that rotating
    # point
    for k in range(lps[n - 1], m):
        if (b[k] != a[i]):
            return False
        i += 1
    return True
 
# Driver code
if __name__ == "__main__":
 
    s1 = "ABACD"
    s2 = "CDABA"
    print("1" if isRotation(s1, s2) else "0")
 
# This code is contributed by sanjeev2552

Time complexity: linear
Space complexity: linear
# Python3 program to count
# all rotation divisible
# by 4.
 
# Returns count of all
# rotations divisible
# by 4
def countRotations(n) :
 
    l = len(n)
 
    # For single digit number
    if (l == 1) :
        oneDigit = (int)(n[0])
         
        if (oneDigit % 4 == 0) :
            return 1
        return 0
     
     
    # At-least 2 digit number
    # (considering all pairs)
    count = 0
    for i in range(0, l - 1) :
        twoDigit = (int)(n[i]) * 10 + (int)(n[i + 1])
         
        if (twoDigit % 4 == 0) :
            count = count + 1
             
    # Considering the number
    # formed by the pair of
    # last digit and 1st digit
    twoDigit = (int)(n[l - 1]) * 10 + (int)(n[0])
    if (twoDigit % 4 == 0) :
        count = count + 1
 
    return count
 
# Driver program
n = "4834"
print("Rotations: " ,
    countRotations(n))
 
# This code is contributed by Nikita tiwari.

Time complexity: linear
Space complexity: constant
# Python program to reverse a string using recursion
 
# Function to print reverse of the passed string
def reverse(string):
    if len(string) == 0:
        return
     
    temp = string[0]
    reverse(string[1:])
    print(temp, end='')
 
# Driver program to test above function
string = "Geeks for Geeks"
reverse(string)
 
# A single line statement to reverse string in python
# string[::-1]
 
# This code is contributed by Bhavya Jain

Time complexity: quadratic
Space complexity: linear
# Python3 program to print reverse
# of words in a string.
 
 
def wordReverse(str):
    i = len(str)-1
    start = end = i+1
    result = ''
 
    while i >= 0:
        if str[i] == ' ':
            start = i+1
            while start != end:
                result += str[start]
                start += 1
            result += ' '
            end = i
        i -= 1
    start = 0
    while start != end:
        result += str[start]
        start += 1
    return result
 
 
# Driver Code
str = 'I AM A GEEK'
print(wordReverse(str))
 
# This code is contributed
# by SamyuktaSHegde

Time complexity: linear
Space complexity: linear
# Python code for the same approach
def reverse_words(s):
 
    left, i, n = 0, 0, len(s)
 
    while (s[i] == ' '):
        i += 1
 
    left = i
 
    while (i < n):
 
        if (i + 1 == n or s[i] == ' '):
         
            j = i - 1
            if (i + 1 == n):
                j += 1
         
            while (left < j):
                s = s[0:left] + s[j] + s[left+1:j] + s[left] + s[j+1:]
             
                left += 1
                j -= 1
         
            left = i + 1
         
        if (i > left and s[left] == ' '):
            left = i
 
        i += 1
 
    s = s[::-1]
 
    return s
 
# driver code
Str = "I AM A GEEK"
Str = reverse_words(Str)
print(Str)
 
# This code is contributed by shinjanpatra

Time complexity: linear
Space complexity: constant
# Python3 program to reverse string without
# affecting it's special character
def rev(s, l, r) :
    while l<r :
        temp = s[l]
        s[l] = s[r]
        s[r] = temp
        l += 1
        r -= 1
     
# creating character array of size
# equal to length of string
def reverse(s):
     
    temp = ['']*len(s)
    x = 0
     
    for i in range(len(s)) :
        if s[i] >= 'a' and s[i] <= 'z' or s[i] >= 'A' and s[i] <= 'Z' :
            # storing elements in array
            temp[x] = s[i]
            x += 1
     
    # reversing the character array       
    rev(temp, 0, x)
     
    lst = list(s)
    x = 0
     
    for i in range(len(s)) :
        if s[i] >= 'a' and s[i] <= 'z' or s[i] >= 'A' and s[i] <= 'Z' :
            # updating the origional string
            lst[i] = temp[x]
            x += 1
     
    revStr = ""
    for i in range(len(s)) :
        revStr += lst[i]
         
    print("reverse string is :",revStr);
     
# Driver Code
if __name__ == "__main__" :
     
    s="Ab,c,de!$"
     
    reverse(s)
     
# This code is contributed by aditya942003patil

Time complexity: linear
Space complexity: linear
def reverseString(text):
    index = -1
 
    # Loop from last index until half of the index    
    for i in range(len(text)-1, int(len(text)/2), -1):
 
        # match character is alphabet or not        
        if text[i].isalpha():
            temp = text[i]
            while True:
                index += 1
                if text[index].isalpha():
                    text[i] = text[index]
                    text[index] = temp
                    break
    return text
     
# Driver code
string = "a!!!b.c.d,e'f,ghi"
print ("Input string: ", string)
string = reverseSting(list(string))
print ("Output string: ", "".join(string))
  
# This code is contributed by shiva9610

Time complexity: linear
Space complexity: constant
# Python3 program to reverse a string preserving
# spaces.
 
# Function to reverse the string
# and preserve the space position
def reverses(st):
 
    # Mark spaces in result
    n = len(st)
    result = [0] * n
     
    for i in range(n):
        if (st[i] == ' '):
            result[i] = ' '
 
    # Traverse input string from beginning
    # and put characters in result from end
    j = n - 1
    for i in range(len(st)):
         
        # Ignore spaces in input string
        if (st[i] != ' '):
             
            # Ignore spaces in result.
            if (result[j] == ' '):
                j -= 1
 
            result[j] = st[i]
            j -= 1
             
    return ''.join(result)
 
# Driver code
if __name__ == "__main__":
 
    st = "internship at geeks for geeks"
    print(reverses(st))
 
# This code is contributed by ukasp

Time complexity: linear
Space complexity: linear
# Python3 program to implement
# the above approach
def preserveSpace(Str):
   
    n = len(Str)
    Str = list(Str)
 
    # Initialize two pointers
    # as two corners
    start = 0
    end = n - 1
 
    # Move both pointers
    # toward each other
    while(start < end):
 
        # If character at start
        # or end is space,
        # ignore it
        if(Str[start] == ' '):
            start += 1
            continue
        elif(Str[end] == ' '):
            end -= 1
            continue
             
        # If both are not
        # spaces, do swap
        else:
            Str[start], Str[end] = (Str[end],
                                    Str[start])
            start += 1
            end -= 1
    print(''.join(Str))
 
# Driver code
Str = "internship at geeks for geeks"
preserveSpace(Str);
 
# This code is contributed by avanitrachhadiya2155

Time complexity: linear
Space complexity: constant
# Python3 Program for removing characters
# from reversed string where vowels are
# present in original string
 
# Function for replacing the string
def replaceOriginal(s, n):
 
    # initialize a string of length n
    r = [' '] * n
 
    # Traverse through all characters of string
    for i in range(n):
 
        # assign the value to string r
        # from last index of string s
        r[i] = s[n - 1 - i]
 
        # if s[i] is a consonant then
        # print r[i]
        if (s[i] != 'a' and s[i] != 'e' and
            s[i] != 'i' and s[i] != 'o' and
            s[i] != 'u'):
            print(r[i], end = "")
    print()
 
# Driver Code
if __name__ == "__main__":
    s = "geeksforgeeks"
    n = len(s)
    replaceOriginal(s, n)
 
# This code is contributed by
# sanjeev2552

Time complexity: linear
Space complexity: linear