Banana-Leopard/TASTY huggingface dataset:
# Python 3 program to find count of
# substring containing exactly K ones
 
# method returns total number of
# substring having K ones
def countOfSubstringWithKOnes(s, K):
    N = len(s)
    res = 0
    countOfOne = 0
    freq = [0 for i in range(N + 1)]
 
    # initialize index having
    # zero sum as 1
    freq[0] = 1
 
    # loop over binary characters of string
    for i in range(0, N, 1):
         
        # update countOfOne variable with
        # value of ith character
        countOfOne += ord(s[i]) - ord('0')
 
        # if value reaches more than K,
        # then update result
        if (countOfOne >= K):
             
            # add frequency of indices, having
            # sum (current sum - K), to the result
            res += freq[countOfOne - K]
         
        # update frequency of one's count
        freq[countOfOne] += 1
     
    return res
 
# Driver code
if __name__ == '__main__':
    s = "10010"
    K = 1
    print(countOfSubstringWithKOnes(s, K))
 
# This code is contributed by
# Surendra_Gangwar

Time complexity: linear
Space complexity: linear
# Python3 program to print distinct
# characters of a string.
NO_OF_CHARS = 256
 
# Print duplicates present in the
# passed string
def printDistinct(str):
 
    # Create an array of size 256 and
    # count of every character in it
    count = [0] * NO_OF_CHARS
 
    # Count array with frequency of
    # characters
    for i in range (len(str)):
        if(str[i] != ' '):
            count[ord(str[i])] += 1
    n = i
 
    # Print characters having count
    # more than 0
    for i in range(n):
        if (count[ord(str[i])] == 1):
            print (str[i], end = "")
 
# Driver Code
if __name__ == "__main__":
 
    str = "GeeksforGeeks"
    printDistinct(str)
     
# This code is contributed by ita_c

Time complexity: quadratic
Space complexity: constant
# Python3 program to find all distinct characters
# in a String
 
MAX_CHAR = 256
 
# Function to print distinct characters in
# given Str[]
def printDistinct(Str):
 
    n = len(Str)
 
    # count[x] is going to store count of
    # character 'x' in Str. If x is not present,
    # then it is going to store 0.
    count = [0 for i in range(MAX_CHAR)]
 
    # index[x] is going to store index of character
    # 'x' in Str. If x is not present or x is
    # more than once, then it is going to store a value
    # (for example, length of String) that cannot be
    # a valid index in Str[]
    index = [n for i in range(MAX_CHAR)]
 
 
    # Traverse the input String
    for i in range(n):
         
        # Find current character and increment its
        # count
        x = ord(Str[i])
        count[x] += 1
 
        # If this is first occurrence, then set value
        # in index as index of it.
        if (count[x] == 1 and x !=' '):
            index[x] = i
 
        # If character repeats, then remove it from
        # index[]
        if (count[x] == 2):
            index[x] = n
 
 
    # Since size of index is constant, below operations
    # take constant time.
    index=sorted(index)
 
    for i in range(MAX_CHAR):
        if index[i] == n:
            break
        print(Str[index[i]],end="")
 
# Driver code
 
Str = "GeeksforGeeks"
printDistinct(Str)
 
# This code is contributed by mohit kumar 29

Time complexity: linear
Space complexity: linear
# Python3 program to print common characters
# of two Strings in alphabetical order
 
 
# Initializing size of array
MAX_CHAR=26
 
# Function to find similar characters
def printCommon( s1, s2):
    # two arrays of length 26 to store occurrence
    # of a letters alphabetically for each string
    a1 = [0 for i in range(MAX_CHAR)]
    a2 = [0 for i in range(MAX_CHAR)]
 
    length1 = len(s1)
    length2 = len(s2)
 
    for i in range(0,length1):
        a1[ord(s1[i]) - ord('a')] += 1
 
    for i in range(0,length2):
        a2[ord(s2[i]) - ord('a')] += 1
 
    # If a common index is non-zero, it means
    # that the letter corresponding to that
    # index is common to both strings
    for i in range(0,MAX_CHAR):
        if (a1[i] != 0 and a2[i] != 0):
             
            # Find the minimum of the occurrence
            # of the character in both strings and print
            # the letter that many number of times
            for j in range(0,min(a1[i],a2[i])):
                ch = chr(ord('a')+i)
                print (ch, end='')
             
 
# Driver code
if __name__=="__main__":
    s1 = "geeksforgeeks"
    s2 = "practiceforgeeks"
    printCommon(s1, s2);
 
# This Code is contributed by Abhishek Sharma

Time complexity: linear
Space complexity: constant
# Python3 Program to find all the
# common characters in n strings
MAX_CHAR = 26
 
def commonCharacters(strings, n) :
     
    # primary array for common characters
    # we assume all characters are seen before.
    prim = [True] * MAX_CHAR
     
    # for each strings
    for i in range(n):
         
        # secondary array for common characters
        # Initially marked false
        sec = [False] * MAX_CHAR
 
        # for every character of ith strings
        for j in range(len(strings[i])):
 
            # if character is present in all
            # strings before, mark it.
            if (prim[ord(strings[i][j]) - ord('a')]) :
                sec[ord(strings[i][j]) -
                    ord('a')] = True
 
        # copy whole secondary array
        # into primary
        for i in range(MAX_CHAR):
            prim[i] = sec[i]
 
    # displaying common characters
    for i in range(26):
        if (prim[i]) :
            print("%c " % (i + ord('a')),
                               end = "")
 
# Driver's Code
strings = [ "geeksforgeeks", "gemkstones",
            "acknowledges", "aguelikes" ]
n = len(strings)
commonCharacters(strings, n)
 
# This code is contributed by Niwesh Gupta

Time complexity: linear
Space complexity: constant
# Python3 program Find concatenated string
# with uncommon characters of given strings
 
 
def concatenatedString(s1, s2):
    res = ""  # result
    m = {}
 
    # store all characters of s2 in map
    for i in range(0, len(s2)):
        m[s2[i]] = 1
 
    # Find characters of s1 that are not
    # present in s2 and append to result
    for i in range(0, len(s1)):
        if(not s1[i] in m):
            res = res + s1[i]
        else:
            m[s1[i]] = 2
 
    # Find characters of s2 that are not
    # present in s1.
    for i in range(0, len(s2)):
        if(m[s2[i]] == 1):
            res = res + s2[i]
 
    return res
 
 
# Driver Code
if __name__ == "__main__":
    s1 = "abcs"
    s2 = "cxzca"
    print(concatenatedString(s1, s2))
 
# This code is contributed
# by Sairahul099

Time complexity: linear
Space complexity: linear
# Python program to remove vowels from a string
# Function to remove vowels
def rem_vowel(string):
    vowels = ['a','e','i','o','u']
    result = [letter for letter in string if letter.lower() not in vowels]
    result = ''.join(result)
    print(result)
 
# Driver program
string = "GeeksforGeeks - A Computer Science Portal for Geeks"
rem_vowel(string)
string = "Loving Python LOL"
rem_vowel(string)

Time complexity: linear
Space complexity: constant
# Python program to remove vowels from a string
# Function to remove vowels
 
# import the module for regular expression (re)
import re
def rem_vowel(string):
    return (re.sub("[aeiouAEIOU]","",string))           
 
# Driver program
string = "GeeksforGeeks - A Computer Science Portal for Geeks"
print(rem_vowel(string))

Time complexity: linear
Space complexity: constant
# Python3 implementation for printing
# sentence without repetitive vowels
 
# function which returns True or False
# for occurrence of a vowel
def is_vow(c):
 
    # this compares vowel with
    # character 'c'
    return ((c == 'a') or (c == 'e') or
            (c == 'i') or (c == 'o') or
            (c == 'u'));
 
# function to print resultant string
def removeVowels(str):
 
    # print 1st character
    print(str[0], end = "");
 
    # loop to check for each character
    for i in range(1,len(str)):
 
        # comparison of consecutive
        # characters
        if ((is_vow(str[i - 1]) != True) or
            (is_vow(str[i]) != True)):
             
            print(str[i], end = "");
 
# Driver code
str= " geeks for geeks";
removeVowels(str);
 
# This code is contributed by mits

Time complexity: linear
Space complexity: linear
# Python3 program to count vowels
# in a string
 
# Function to check the Vowel
def isVowel(ch):
    return ch.upper() in ['A', 'E', 'I', 'O', 'U']
 
# Returns count of vowels in str
def countVowels(str):
    count = 0
    for i in range(len(str)):
 
        # Check for vowel
        if isVowel(str[i]):
            count += 1
    return count
 
# Driver Code
 
# string object
str = 'abc de'
 
# Total number of Vowels
print(countVowels(str))
 
# This code is contributed
# by SamyuktaSHegde

Time complexity: linear
Space complexity: constant
# Recursive Python 3 program to count the
# total number of vowels using recursion
 
# Function to check the Vowel
def isVowel(ch):
    return ch.upper() in ['A', 'E', 'I', 'O', 'U']
 
# to count total number of
# vowel from 0 to n
def countVovels(str, n):
    if (n == 1):
        return isVowel(str[n - 1]);
 
    return (countVovels(str, n - 1) +
                isVowel(str[n - 1]));
 
# Driver Code
 
# string object
str = "abc de";
 
# Total numbers of Vowel
print(countVovels(str, len(str)))
 
# This code is contributed
# by Akanksha Rai

Time complexity: linear
Space complexity: linear
# Iterative Python3 program to count
# total number of consonants
 
# Function to check for consonant
def isConsonant(ch):
     
    # To handle lower case
    ch = ch.upper()
 
    return not (ch == 'A' or ch == 'E' or
                ch == 'I' or ch == 'O' or
                ch == 'U') and ord(ch) >= 65 and ord(ch) <= 90
 
def totalConsonants(string):
     
    count = 0
     
    for i in range(len(string)):
 
        # To check is character is Consonant
        if (isConsonant(string[i])):
            count += 1
             
    return count
 
# Driver code
string = "abc de"
print(totalConsonants(string))
 
 
# This code id contributed by Ansu Kumari.

Time complexity: linear
Space complexity: constant
# Recursive Python3 program to count
# total number of consonants
 
# Function to check for consonant
def isConsonant(ch):
     
    # To handle lower case
    ch = ch.upper()
 
    return not (ch == 'A' or ch == 'E' or
                ch == 'I' or ch == 'O' or
                ch == 'U') and ord(ch) >= 65 and ord(ch) <= 90
 
# To count total number of 
# consonants from 0 to n-1
def totalConsonants(string, n):
     
    if n == 1:
        return isConsonant(string[0])
 
    return totalConsonants(string, n - 1) + isConsonant(string[n-1])
 
 
# Driver code
string = "abc de"
print(totalConsonants(string, len(string)))
 
 
# This code is contributed by Ansu Kuamri.

Time complexity: linear
Space complexity: linear
# Python implementation of alternate vowel
# and consonant string
 
# 'ch' is vowel or not
def isVowel(ch):
    if(ch == 'a' or ch == 'e' or
       ch == 'i' or ch == 'o' or
       ch == 'u'):
        return True
    return False
 
# create alternate vowel and consonant string
# str1[0...l1-1] and str2[start...l2-1]
def createAltStr(str1, str2, start, l):
    finalStr = ""
    i = 0
     
    # first adding character of vowel/consonant
    # then adding character of consonant/vowel
    for j in range(start, l):
        finalStr = (finalStr + str1[i]) + str2[j]
        i + 1
 
    return finalStr
 
# function to find the required
# alternate vowel and consonant string
def findAltStr(str1):
    nv = 0
    nc = 0
    vstr = ""
    cstr = ""
    l = len(str1)
    for i in range(0, l):
         
        # count vowels and update vowel string
        if(isVowel(str1[i])):
            nv += 1
            vstr = vstr + str1[i]
             
        # count consonants and update
        # consonant string
        else:
            nc += 1
            cstr = cstr + str1[i]
             
    # no such string can be formed
    if(abs(nv - nc) >= 2):
        return "no such string"
     
    # remove first character of vowel string
    # then create alternate string with
    # cstr[0...nc-1] and vstr[1...nv-1]
    if(nv > nc):
        return (vstr[0] + createAltStr(cstr, vstr, 1, nv))
     
    # remove first character of consonant string
    # then create alternate string with
    # vstr[0...nv-1] and cstr[1...nc-1]
    if(nc > nv):
        return (cstr[0] + createAltStr(vstr, cstr, 1, nc))
     
    # if both vowel and consonant
    # strings are of equal length
    # start creating string with consonant
    if(cstr[0] < vstr[0]):
        return createAltStr(cstr, vstr, 0, nv)
 
    return createAltStr(vstr, cstr, 0, nc)
         
# Driver Code
if __name__ == "__main__":
    str1 = "geeks"
    print(findAltStr(str1))
 
# This code is contributed by Sairahul099

Time complexity: linear
Space complexity: linear
# A simple Python 3 program to count number of
# substrings starting and ending with 1
 
 
def countSubStr(st, n):
 
    # Initialize result
    res = 0
 
   # Pick a starting point
    for i in range(0, n):
        if (st[i] == '1'):
 
            # Search for all possible ending point
            for j in range(i+1, n):
                if (st[j] == '1'):
                    res = res + 1
 
    return res
 
 
# Driver program to test above function
st = "00100101"
list(st)
n = len(st)
print(countSubStr(st, n), end="")
 
 
# This code is contributed
# by Nikita Tiwari.

Time complexity: quadratic
Space complexity: constant
# A Python3 program to count number of
# substrings starting and ending with 1
 
 
def countSubStr(st, n):
 
    # Count of 1's in input string
    m = 0
 
    # Traverse input string and
    # count of 1's in it
    for i in range(0, n):
        if (st[i] == '1'):
            m = m + 1
 
    # Return count of possible
    # pairs among m 1's
    return m * (m - 1) // 2
 
 
# Driver program to test above function
st = "00100101"
list(st)
n = len(st)
print(countSubStr(st, n), end="")
 
 
# This code is contributed
# by Nikita Tiwari.

Time complexity: linear
Space complexity: constant
class GFG :
    @staticmethod
    def  helper( n,  str,  i) :
       
        # if 'i' is on the last index
        if (i == n - 1) :
            return 1 if (str[i] == '1') else 0
           
        # if current char is 1
        # add 1 to the answer
        if (str[i] == '1') :
            return 1 + GFG.helper(n, str, i + 1)
        else :
            return GFG.helper(n, str, i + 1)
    @staticmethod
    def  countSubStr( str) :
        n = len(str)
         
        # counting the number of 1's in the string
        count = GFG.helper(n, str, 0)
         
        # return the number of combinations
        return int((count * (count - 1)) / 2)
    @staticmethod
    def main( args) :
        str = list("00100101")
        print(GFG.countSubStr(str))
     
 
if __name__=="__main__":
    GFG.main([])
     
    # This code is contributed by aadityaburujwale.

Time complexity: linear
Space complexity: linear
# Python program to find number of distinct
# permutations of a string.
 
MAX_CHAR = 26
 
# Utility function to find factorial of n.
def factorial(n) :
     
    fact = 1;
    for i in range(2, n + 1) :
        fact = fact * i;
    return fact
       
# Returns count of distinct permutations
# of str.
def countDistinctPermutations(st) :
     
    length = len(st)
    freq = [0] * MAX_CHAR
     
    # finding frequency of all the lower
    # case alphabet and storing them in
    # array of integer
    for i in range(0, length) :
        if (st[i] >= 'a') :
            freq[(ord)(st[i]) - 97] = freq[(ord)(st[i]) - 97] + 1;
   
    # finding factorial of number of
    # appearances and multiplying them
    # since they are repeating alphabets
    fact = 1
    for i in range(0, MAX_CHAR) :
        fact = fact * factorial(freq[i])
   
    # finding factorial of size of string
    # and dividing it by factorial found
    # after multiplying
    return factorial(length) // fact
 
# Driver code
st = "fvvfhvgv"
print (countDistinctPermutations(st))
 
# This code is contributed by Nikita Tiwari.

Time complexity: linear
Space complexity: linear
# Python3 program to check if it is
# possible to split string or not
MAX_CHAR = 26
 
# Function to check if we
# can split string or not
def checkCorrectOrNot(s):
     
    global MAX_CHAR
     
    # Counter array initialized with 0
    count1 = [0] * MAX_CHAR
    count2 = [0] * MAX_CHAR
 
    # Length of the string
    n = len(s)
     
    if n == 1:
        return true
 
    # Traverse till the middle
    # element is reached
    i = 0; j = n - 1
     
    while (i < j):
         
        # First half
        count1[ord(s[i]) - ord('a')] += 1
 
        # Second half
        count2[ord(s[j]) - ord('a')] += 1
 
        i += 1; j -= 1
 
    # Checking if values are
    # different set flag to 1
    for i in range(MAX_CHAR):
         
        if count1[i] != count2[i]:
            return False
             
    return True
 
 
# Driver Code
 
# String to be checked
s = "ababc"
 
print("Yes" if checkCorrectOrNot(s) else "No")
 
 
# This code is contributed by Ansu Kumari.

Time complexity: linear
Space complexity: constant
# Python3 program to check if it is
# possible to split string or not
MAX_CHAR = 26
 
# Function to check if we 
# can split string or not
def checkCorrectOrNot(s):
     
    global MAX_CHAR
     
    # Counter array initialized with 0
    count = [0] * MAX_CHAR
 
    # Length of the string
    n = len(s)
     
    if n == 1:
        return true
 
    # Traverse till the middle
    # element is reached
    i = 0; j = n-1
     
    while i < j:
         
        # First half
        count[ord(s[i]) - ord('a')] += 1
 
        # Second half
        count[ord(s[j])-ord('a')] -= 1
 
        i += 1; j -= 1
 
    # Checking if values are
    # different, set flag to 1
    for i in range(MAX_CHAR):
         
        if count[i] != 0:
            return False
 
    return True
 
 
# Driver Code
 
# String to be checked
s = "abab"
 
print("Yes" if checkCorrectOrNot(s) else "No")
 
 
# This code is contributed by Ansu Kumari.

Time complexity: linear
Space complexity: constant
# Python program to count all
# words with count
# exactly 2.
  
# Returns count of words with frequency
# exactly 2.
def countWords(stri, n):
    m = dict()
    for i in range(n):
        m[stri[i]] = m.get(stri[i],0) + 1
  
    res = 0
    for i in m.values():
        if i == 2:
            res += 1
  
    return res
  
# Driver code
s = [ "hate", "love", "peace", "love",
      "peace", "hate", "love", "peace",
                "love", "peace" ]
n = len(s)
print(countWords(s, n))
 
# This code is contributed
# by Shubham Rana

Time complexity: linear
Space complexity: linear
# importing Counter from collections
from collections import Counter
 
# Python program to count all words with count exactly 2.
# Returns count of words with frequency exactly 2.
def countWords(stri, n):
   
    # Calculating frequency using Counter
    m = Counter(stri)
 
    count = 0
    # Traversing in freq dictionary
    for i in m:
        if m[i] == 2:
            count += 1
 
    return count
 
# Driver code
s = ["hate", "love", "peace", "love",
     "peace", "hate", "love", "peace",
     "love", "peace"]
n = len(s)
print(countWords(s, n))
 
# This code is contributed by vikkycirus

Time complexity: linear
Space complexity: linear
# Python3 program to get same frequency character
# string by removal of at most one char
M = 26
 
# Utility method to get index of character ch
# in lower alphabet characters
def getIdx(ch):
    return (ord(ch) - ord('a'))
 
# Returns true if all non-zero elements
# values are same
def allSame(freq, N):
     
    # get first non-zero element
    for i in range(0, N):
        if(freq[i] > 0):
            same = freq[i]
            break
         
    # check equality of each element
    # with variable same    
    for j in range(i + 1, N):
        if(freq[j] > 0 and freq[j] != same):
            return False
 
    return True
 
# Returns true if we can make all
# character frequencies same
def possibleSameCharFreqByOneRemoval(str1):
    l = len(str1)
 
    # fill frequency array
    freq = [0] * M
    for i in range(0, l):
        freq[getIdx(str1[i])] += 1
         
    # if all frequencies are same,
    # then return true
    if(allSame(freq, M)):
        return True
     
    # Try decreasing frequency of all character
    # by one and then check all equality of all
    # non-zero frequencies
    for i in range(0, 26):
         
        # Check character only if it
        # occurs in str
        if(freq[i] > 0):
            freq[i] -= 1
 
            if(allSame(freq, M)):
                return True
            freq[i] += 1
 
    return False
 
# Driver code
if __name__ == "__main__":
    str1 = "xyyzz"
    if(possibleSameCharFreqByOneRemoval(str1)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by Sairahul099

Time complexity: linear
Space complexity: constant
# Python 3 program to check if
# a string can be made
# valid by removing at most 1 character.
 
# Assuming only lower case characters
CHARS = 26
 
# To check a string S can be converted to a “valid”
# string by removing less than or equal to one
# character.
     
def isValidString(str):
 
    freq = [0]*CHARS
 
    # freq[] : stores the frequency of each
    # character of a string
    for i in range(len(str)):
        freq[ord(str[i])-ord('a')] += 1
 
    # Find first character with non-zero frequency
    freq1 = 0
    count_freq1 = 0
    for i in range(CHARS):
     
        if (freq[i] != 0):
         
            freq1 = freq[i]
            count_freq1 = 1
            break
 
    # Find a character with frequency different
    # from freq1.
    freq2 = 0
    count_freq2 = 0
    for j in range(i+1,CHARS):
     
        if (freq[j] != 0):
     
            if (freq[j] == freq1):
                count_freq1 += 1
            else:
             
                count_freq2 = 1
                freq2 = freq[j]
                break
 
    # If we find a third non-zero frequency
    # or count of both frequencies become more
    # than 1, then return false
    for k in range(j+1,CHARS):
     
        if (freq[k] != 0):
         
            if (freq[k] == freq1):
                count_freq1 += 1
            if (freq[k] == freq2):
                count_freq2 += 1
 
            # If we find a third non-zero freq
            else:
                return False
 
        # If counts of both frequencies is more than 1
        if (count_freq1 > 1 and count_freq2 > 1):
            return False
 
    # Return true if we reach here
    return True
 
# Driver code
if __name__ == "__main__":
    str= "abcbc"
 
    if (isValidString(str)):
        print("YES")
    else:
        print("NO")
         
# this code is contributed by
# ChitraNayal

Time complexity: linear
Space complexity: constant