Banana-Leopard/TASTY huggingface dataset:
# Python program to check if a string can be made
# valid by removing at most 1 character using hashmap.
 
# To check a string S can be converted to a variation
# string
def checkForVariation(strr):
    if(len(strr) == 0):
        return True
     
    mapp = {}
     
    # Run loop form 0 to length of string   
    for i in range(len(strr)):
        if strr[i] in mapp:
            mapp[strr[i]] += 1
        else:
            mapp[strr[i]] = 1
     
    # declaration of variables
    first = True
    second = True
    val1 = 0
    val2 = 0
    countOfVal1 = 0
    countOfVal2 = 0
     
    for itr in mapp:
        i = itr
         
        # if first is true than countOfVal1 increase
        if(first):
            val1 = i
            first = False
            countOfVal1 += 1
            continue
         
        if(i == val1):
            countOfVal1 += 1
            continue
         
        # if second is true than countOfVal2 increase
        if(second):
            val2 = i
            countOfVal2 += 1
            second = False
            continue
         
        if(i == val2):
            countOfVal2 += 1
            continue
    if(countOfVal1 > 1 and countOfVal2 > 1):
        return False
     
    else:
        return True
 
# Driver code
print(checkForVariation("abcbc"))
 
# This code is contributed by rag2127

Time complexity: nlogn
Space complexity: linear
# Python program
from collections import Counter
 
# To check a string S can be
# converted to a variation
# string
def checkForVariation(strr):
   
    freq = Counter(strr)
     
    # Converting these values to list
    valuelist = list(freq.values())
     
    # Counting frequencies again
    ValueCounter = Counter(valuelist)
    if(len(ValueCounter) == 1):
        return True
    elif(len(ValueCounter) == 2 and
         min(ValueCounter.values()) == 1):
        return True
       
    # If no conditions satisfied return false
    return False
 
 
# Driver code
string = "abcbc"
 
# passing string to checkForVariation Function
print(checkForVariation(string))
 
# This code is contributed by vikkycirus

Time complexity: linear
Space complexity: linear
# Python3 program to get number of ways to increase
# LCS by 1
 
M = 26
 
# Method returns total ways to increase LCS length by 1
def waysToIncreaseLCSBy1(str1, str2):
    m = len(str1)
    n = len(str2)
 
    # Fill positions of each character in vector
    # vector<int> position[M];
    position = [[] for i in range(M)]
    for i in range(1, n+1, 1):
        position[ord(str2[i-1])-97].append(i)
 
    # Initializing 2D array by 0 values
    lcsl = [[0 for i in range(n+2)] for j in range(m+2)]
    lcsr = [[0 for i in range(n+2)] for j in range(m+2)]
 
    # Filling LCS array for prefix substrings
    for i in range(1, m+1, 1):
        for j in range(1, n+1,1):
            if (str1[i-1] == str2[j-1]):
                lcsl[i][j] = 1 + lcsl[i-1][j-1]
            else:
                lcsl[i][j] = max(lcsl[i-1][j],
                                lcsl[i][j-1])
 
    # Filling LCS array for suffix substrings
    for i in range(m, 0, -1):
        for j in range(n, 0, -1):
            if (str1[i-1] == str2[j-1]):
                lcsr[i][j] = 1 + lcsr[i+1][j+1]
            else:
                lcsr[i][j] = max(lcsr[i+1][j],
                                lcsr[i][j+1])
 
        # Looping for all possible insertion positions
        # in first string
    ways = 0
    for i in range(0, m+1,1):
        # Trying all possible lower case characters
        for C in range(0, 26,1):
            # Now for each character, loop over same
            # character positions in second string
            for j in range(0, len(position[C]),1):
                p = position[C][j]
 
                # If both, left and right substrings make
                # total LCS then increase result by 1
                if (lcsl[i][p-1] + lcsr[i+1][p+1] == lcsl[m][n]):
                    ways += 1
    return ways
 
 
# Driver code to test above methods
str1 = "abcabc"
str2 = "abcd"
print(waysToIncreaseLCSBy1(str1, str2))
 
# This code is contributed by ankush_953

Time complexity: quadratic
Space complexity: quadratic
# Python3 implementation to find the character in
# first that is present at minimum index
# in second String
 
# function to find the minimum index character
def printMinIndexChar(Str, patt):
 
    # to store the index of character having
    # minimum index
    minIndex = 10**9
 
    # lengths of the two Strings
    m =len(Str)
    n =len(patt)
 
    # traverse 'patt'
    for i in range(n):
 
        # for each character of 'patt' traverse 'Str'
        for j in range(m):
 
            # if patt[i] is found in 'Str', check if
            # it has the minimum index or not. If yes,
            # then update 'minIndex' and break
            if (patt[i] == Str[j] and j < minIndex):
                minIndex = j
                break
 
 
 
 
    # print the minimum index character
    if (minIndex != 10**9):
        print("Minimum Index Character = ",Str[minIndex])
 
    # if no character of 'patt' is present in 'Str'
    else:
        print("No character present")
 
 
# Driver code
 
Str = "geeksforgeeks"
patt = "set"
printMinIndexChar(Str, patt)
 
# This code is contributed by mohit kumar 29

Time complexity: quadratic
Space complexity: constant
# Python3 implementation to
# find the character in first
# string that is present at
# minimum index in second string
import sys
 
# Function to find the
# minimum index character
def printMinIndexChar(st, patt):
 
    # unordered_map 'um'
    # implemented as hash table
    um = {}
 
    # to store the index of
    # character having minimum index
    minIndex = sys.maxsize
 
    # Lengths of the two strings
    m = len(st)
    n = len(patt)
 
    # Store the first index of
    # each character of 'str'
    for i in range (m):
        if (st[i] not in um):
            um[st[i]] = i
 
    # traverse the string 'patt'
    for i in range(n):
 
        # If patt[i] is found in 'um',
        # check if  it has the minimum
        # index or not accordingly
        # update 'minIndex'
        if (patt[i] in um and
            um[patt[i]] < minIndex):
            minIndex = um[patt[i]]
 
    # Print the minimum index character
    if (minIndex != sys.maxsize):
        print ("Minimum Index Character = ",
                st[minIndex])
 
    # If no character of 'patt'
    # is present in 'str'
    else:
        print ("No character present")
 
# Driver program to test above
if __name__ == "__main__":
 
  st = "geeksforgeeks"
  patt = "set"
  printMinIndexChar(st, patt)
 
# This code is contributed by Chitranayal

Time complexity: linear
Space complexity: linear
# Python3 implementation of program to find the maximum length
# that can be removed
 
# Function to find the length of longest sub-string that
# can me make removed
# arr --> pair type of array whose first field store
#         character in and second field stores
#         corresponding index of that character
def longestNull(S):
 
    arr=[]
 
    # store {'@',-1} in arr , here this value will
    # work as base index
    arr.append(['@', -1])
 
    maxlen = 0 # Initialize result
 
    # one by one iterate characters of String
    for i in range(len(S)):
        # make pair of char and index , then store
        # them into arr
        arr.append([S[i], i])
 
        # now if last three elements of arr[] are making
        # sub-string"100" or not
        while (len(arr)>=3 and
            arr[len(arr)-3][0]=='1' and
            arr[len(arr)-2][0]=='0' and
            arr[len(arr)-1][0]=='0'):
 
            # if above condition is true then delete
            # sub-string"100" from arr[]
            arr.pop()
            arr.pop()
            arr.pop()
 
        # index of current last element in arr[]
        tmp = arr[-1]
        # This is important, here 'i' is the index of
        # current character inserted into arr[]
        # and 'tmp' is the index of last element in arr[]
        # after continuous deletion of sub-String
        # "100" from arr[] till we make it null, difference
        # of these to 'i-tmp' gives the length of current
        # sub-string that can be make null by continuous
        # deletion of sub-string"100"
        maxlen = max(maxlen, i - tmp[1])
 
 
    return maxlen
 
 
# Driver code
 
print(longestNull("1011100000100"))
 
# This code is contributed by mohit kumar 29

Time complexity: linear
Space complexity: linear
# Simple Python3 program to find pairs with
# distance equal to English alphabet distance
 
# Function to count pairs
def countPairs(str1):
    result = 0;
    n = len(str1)
    for i in range(0, n):
        for j in range(i + 1, n):
 
            # Increment count if characters
            # are at same distance
            if (abs(ord(str1[i]) -
                    ord(str1[j])) == abs(i - j)):
                result += 1;
 
    return result;
 
# Driver code
if __name__ == "__main__":
    str1 = "geeksforgeeks";
    print(countPairs(str1));
 
# This code is contributed
# by Sairahul099

Time complexity: quadratic
Space complexity: linear
# An optimized C++ program to find pairs with
# distance equal to English alphabet distance
 
MAX_CHAR = 26
 
# Function to count pairs with distance
# equal to English alphabet distance
def countPairs(str1):
    result = 0;
    n = len(str1)
 
    for i in range(0, n):
 
        # This loop runs at most 26 times
        for j in range(1, MAX_CHAR + 1):
            if((i + j) < n):
                if ((abs(ord(str1[i + j]) -
                         ord(str1[i])) == j)):
                    result += 1;
 
    return result
 
# Driver code
if __name__ == "__main__":
    str1 = "geeksforgeeks";
    print(countPairs(str1))
 
# This code is contributed
# by Sairahul099

Time complexity: quadratic
Space complexity: linear
# Python3 program to count the
# number of pairs
MAX = 256
 
# Function to count the number
# of equal pairs
def countPairs(s):
     
    # Hash table
    cnt = [0 for i in range(0, MAX)]
 
    # Traverse the string and count
    # occurrence
    for i in range(len(s)):
        cnt[ord(s[i]) - 97] += 1
 
    # Stores the answer
    ans = 0
 
    # Traverse and check the occurrence
    # of every character
    for i in range(0, MAX):
        ans += cnt[i] * cnt[i]
 
    return ans
 
# Driver code
if __name__=="__main__":
    s = "geeksforgeeks"
    print(countPairs(s))
 
# This code is contributed
# by Sairahul099        

Time complexity: linear
Space complexity: linear
# Python3 Program to count strings with
# adjacent characters.
def countStrs(n):
 
    # Initializing arr[n+1][27] to 0
    dp = [[0 for j in range(27)]
             for i in range(n + 1)]
 
    # Initialing 1st row all 1 from 0 to 25
    for i in range(0, 26):
        dp[1][i] = 1
 
    # Begin evaluating from i=2 since
    # 1st row is set    
    for i in range(2, n + 1):
        for j in range(0, 26):
 
            # j=0 is 'A' which can make strings
            # of length i using strings of length
            # i-1 and starting with 'B'
            if(j == 0):
                dp[i][j] = dp[i - 1][j + 1];
            else:
                dp[i][j] = (dp[i - 1][j - 1] +
                            dp[i - 1][j + 1])
 
    # Our result is sum of last row.        
    sum = 0
    for i in range(0, 26):
        sum = sum + dp[n][i]
 
    return sum
     
# Driver's Code
if __name__ == "__main__":
    n = 3
    print("Total strings are : ", countStrs(n))
     
# This code is contributed by Sairahul Jella

Time complexity: linear
Space complexity: linear
# Python3 program to print Number of Words,
# Vowels and Frequency of Each Character
 
# A method to count the number of
# uppercase character, vowels and number of words
def words(str):
    wcount = vcount = ucount = i = 0
    while i < len(str):
        ch = str[i]
         
        # condition checking for word count
        if (ch == " " or ch == "."):
            wcount += 1
             
        # condition checking for vowels
        # in lower case    
        if(ch == "a" or ch == "e" or
           ch == "i" or ch == 'o' or ch == "u"):
            vcount += 1
             
        # condition checking for vowels in uppercase
        if (ch == "A" or ch == "E" or
            ch == "I" or ch == 'O' or ch == "U"):
            vcount += 1
             
        # condition checking for upper case characters
        if (ord(ch) >= 65 and ord(ch) <= 90):
            ucount += 1
        i += 1
         
    print("number of words = ", wcount)
    print("number of vowels = ", vcount)
    print("number of upper case characters = ",
                                        ucount)
     
# a method to print the frequency
# of each character.
def frequency(str):
    i = 1
     
    # checking each and every
    # ascii code character
    while i < 127:
        ch1 = chr(i)
        c = 0
        j = 0
        while j < len(str):
            ch2 = str[j]
            if(ch1 == ch2):
                c += 1
            j += 1
             
        # condition to print the frequency
        if c > 0:
            print("Character:", ch1 +
                  " Frequency:", c)
        i += 1
         
# Driver Code
 
# sample string to check the code    
s = "Geeks for Geeks."
 
# function calling
words(s)
frequency(s)
 
# This code is contributed by Animesh_Gupta

Time complexity: linear
Space complexity: constant
# Python3 program to Find longest subsequence where
#  every character appears at-least k times
  
MAX_CHARS = 26
  
def longestSubseqWithK(s, k):   
 
    n = len(s)                 
  
    # Count frequencies of all characters
    freq = [0]*MAX_CHARS                
    for i in range(n): 
        freq[ord(s[i]) - ord('a')]+=1             
      
    # Traverse given string again and print
    # all those characters whose frequency
    # is more than or equal to k.
    for i in range(n ):
        if (freq[ord(s[i]) - ord('a')] >= k):              
            print(s[i],end="")
  
# Driver code
if __name__ == "__main__":
     
    s = "geeksforgeeks"
    k = 2
    longestSubseqWithK(s, k)

Time complexity: linear
Space complexity: linear
# Python3 program to Find longest subsequence where every
# character appears at-least k times
def longestSubseqWithK(Str, k):
  n = len(Str)
  hm = {}
 
  # Count frequencies of all characters
  for i in range(n):
    c = Str[i]
    if(c in hm):
        hm += 1
     
    else:
        hm = 1
 
  # Traverse given string again and print
  # all those characters whose frequency
  # is more than or equal to k.
  for i in range(n):
    c = Str[i]
    if (hm >= k):
      print(c,end="")
     
# Driver code
Str = "geeksforgeeks"
k = 2
longestSubseqWithK(Str, k)
 
# This code is contributed by shinjanpatra

Time complexity: nlogn
Space complexity: linear
# Recursive Python program to check
# if a string is subsequence
# of another string
 
# Returns true if str1[] is a
# subsequence of str2[].
 
 
def isSubSequence(string1, string2, m, n):
    # Base Cases
    if m == 0:
        return True
    if n == 0:
        return False
 
    # If last characters of two
    # strings are matching
    if string1[m-1] == string2[n-1]:
        return isSubSequence(string1, string2, m-1, n-1)
 
    # If last characters are not matching
    return isSubSequence(string1, string2, m, n-1)
 
 
# Driver program to test the above function
string1 = "gksrek"
string2 = "geeksforgeeks"
 
if isSubSequence(string1, string2, len(string1), len(string2)):
    print ("Yes")
else:
    print ("No")
 
# This code is contributed by BHAVYA JAIN

Time complexity: linear
Space complexity: linear
# Iterative JavaScript program to check
# If a string is subsequence of another string
 
# Returns true if s1 is subsequence of s2
def issubsequence(s1, s2):
 
    n,m = len(s1),len(s2)
    i,j = 0,0
    while (i < n and j < m):
        if (s1[i] == s2[j]):
            i += 1
        j += 1
     
    # If i reaches end of s1,that mean we found all
    # characters of s1 in s2,
    # so s1 is subsequence of s2, else not
    return i == n
 
 
# driver code
s1 = "gksrek"
s2 = "geeksforgeeks"
if (issubsequence(s1, s2)):
    print("gksrek is subsequence of geekforgeeks")
else:
    print("gksrek is not a subsequence of geekforgeeks")
 
# This code is contributed by shinjanpatra

Time complexity: linear
Space complexity: constant
# Python 3 program to count
# subsequences of the form
# a ^ i b ^ j c ^ k
 
# Returns count of subsequences
# of the form a ^ i b ^ j c ^ k
def countSubsequences(s):
 
    # Initialize counts of different
    # subsequences caused by different
    # combination of 'a'
    aCount = 0
 
    # Initialize counts of different
    # subsequences caused by different
    # combination of 'a' and different
    # combination of 'b'
    bCount = 0
 
    # Initialize counts of different
    # subsequences caused by different
    # combination of 'a', 'b' and 'c'.
    cCount = 0
 
    # Traverse all characters
    # of given string
    for i in range(len(s)):
     
        # If current character is 'a',
        # then there are following
        # possibilities :
        # a) Current character begins
        # a new subsequence.
        # b) Current character is part
        # of aCount subsequences.
        # c) Current character is not
        # part of aCount subsequences.
        if (s[i] == 'a'):
            aCount = (1 + 2 * aCount)
 
        # If current character is 'b', then
        # there are following possibilities :
        # a) Current character begins a
        # new subsequence of b's with
        # aCount subsequences.
        # b) Current character is part
        # of bCount subsequences.
        # c) Current character is not
        # part of bCount subsequences.
        else if (s[i] == 'b'):
            bCount = (aCount + 2 * bCount)
 
        # If current character is 'c', then
        # there are following possibilities :
        # a) Current character begins a
        # new subsequence of c's with
        # bCount subsequences.
        # b) Current character is part
        # of cCount subsequences.
        # c) Current character is not
        # part of cCount subsequences.
        else if (s[i] == 'c'):
            cCount = (bCount + 2 * cCount)
 
    return cCount
 
# Driver code
if __name__ == "__main__":
    s = "abbc"
    print(countSubsequences(s))
 
# This code is contributed
# by ChitraNayal

Time complexity: linear
Space complexity: constant
# Python 3 program to count subsequences
# of a string divisible by n.
 
# Returns count of subsequences of
# str divisible by n.
def countDivisibleSubseq(str, n):
    l = len(str)
 
    # division by n can leave only n remainder
    # [0..n-1]. dp[i][j] indicates number of
    # subsequences in string [0..i] which leaves
    # remainder j after division by n.
    dp = [[0 for x in range(l)]
             for y in range(n)]
 
    # Filling value for first digit in str
    dp[int(str[0]) % n][0] += 1
      
    for i in range(1, l):
       
       # start a new subsequence with index i
        dp[int(str[i]) % n][i] += 1
 
        for j in range(n):
 
          # exclude i'th character from all the
          # current subsequences of string [0...i-1]
          dp[j][i] += dp[j][i-1]
 
          # include i'th character in all the current
          # subsequences of string [0...i-1]
          dp[(j * 10 + int(str[i])) % n][i] += dp[j][i-1]
 
    return dp[0][l-1]
 
# Driver code
if __name__ == "__main__":
     
    str = "1234"
    n = 4
    print(countDivisibleSubseq(str, n))
 
# This code is contributed by ita_c

Time complexity: quadratic
Space complexity: quadratic
# Python 3 Program to find the "GFG"
# subsequence in the given string
MAX = 100
 
# Print the count of "GFG" subsequence
# in the string
def countSubsequence(s, n):
    cntG = 0
    cntF = 0
    result = 0
    C=0
 
 
    # Traversing the given string
    for i in range(n):
        if (s[i] == 'G'):
             
            # If the character is 'G', increment
            # the count of 'G', increase the result
            # and update the array.
            cntG += 1
            result += C
            continue
 
        # If the character is 'F', increment
        # the count of 'F' and update the array.
        if (s[i] == 'F'):
            cntF += 1
            C += cntG
            continue
 
        # Ignore other character.
        else:
            continue
     
    print(result)
 
# Driver Code
if __name__ == '__main__':
    s = "GFGFG"
    n = len(s)
    countSubsequence(s, n)
     
# This code is contributed by
# Sanjit_Prasad

Time complexity: linear
Space complexity: constant
# Python3 program to print
# distinct subsequences of
# a given string
import math
 
# Create an empty set
# to store the subsequences
sn = []
global m
m = 0
 
# Function for generating
# the subsequences
 
 
def subsequences(s, op, i, j):
 
    # Base Case
    if(i == m):
        op[j] = None
        temp = "".join([i for i in op if i])
 
        # Insert each generated
        # subsequence into the set
        sn.append(temp)
        return
 
    # Recursive Case
    else:
 
        # When a particular
        # character is taken
        op[j] = s[i]
 
        subsequences(s, op,
                     i + 1, j + 1)
 
        # When a particular
        # character isn't taken
        subsequences(s, op,
                     i + 1, j)
        return
 
 
# Driver Code
str = "ggg"
m = len(str)
n = int(math.pow(2, m) + 1)
 
# Output array for storing
# the generating subsequences
# in each call
op = [None for i in range(n)]
 
# Function Call
subsequences(str, op, 0, 0)
 
# Output will be the number
# of elements in the set
print(len(set(sn)))
 
# This code is contributed by avanitrachhadiya2155

Time complexity: np
Space complexity: linear
# Python3 program to count number of
# distinct subsequences of a given string
 
MAX_CHAR = 256
 
def countSub(ss):
 
    # create an array to store index of last
    last = [-1 for i in range(MAX_CHAR + 1)]
     
    # length of input string
    n = len(ss)
     
    # dp[i] is going to store count of
    # discount subsequence of length of i
    dp = [-2 for i in range(n + 1)]
      
    # empty substring has only
    # one subsequence
    dp[0] = 1
     
    # Traverse through all lengths
    # from 1 to n
    for i in range(1, n + 1):
         
        # number of subsequence with
        # substring str[0...i-1]
        dp[i] = 2 * dp[i - 1]
 
        # if current character has appeared
        # before, then remove all subsequences
        # ending with previous occurrence.
        if last[ord(ss[i - 1])] != -1:
            dp[i] = dp[i] - dp[last[ord(ss[i - 1])]]
        last[ord(ss[i - 1])] = i - 1
     
    return dp[n]
     
# Driver code
print(countSub("gfg"))
 
# This code is contributed
# by mohit kumar 29

Time complexity: linear
Space complexity: linear
# Python3 program for above approach
 
# Returns count of distinct 
# subsequences of str.
def countSub(s):
     
    Map = {}
 
    # Iterate from 0 to length of s
    for i in range(len(s)):
        Map[s[i]] = -1
 
    allCount = 0
    levelCount = 0
 
    # Iterate from 0 to length of s
    for i in range(len(s)):
        c = s[i]
 
        # Check if i equal to 0
        if (i == 0):
            allCount = 1
            Map = 1
            levelCount = 1
            continue
 
        # Replace levelCount with
          # allCount + 1
        levelCount = allCount + 1
 
        # If map is less than 0
        if (Map < 0):
            allCount = allCount + levelCount
        else:
            allCount = allCount + levelCount - Map
 
        Map = levelCount
 
    # Return answer
    return allCount
 
# Driver Code
List = [ "abab", "gfg" ]
 
for s in List:
    cnt = countSub(s)
    withEmptyString = cnt + 1
 
    print("With empty string count for",
          s, "is", withEmptyString)
    print("Without empty string count for",
          s, "is", cnt)
 
# This code is contributed by rag2127

Time complexity: linear
Space complexity: constant