codeparrot/xlcost-text-to-code huggingface dataset:
Smallest number whose product with N has sum of digits equal to that of N | Function to find the minimum integer having sum of digits of a number multiplied by n equal to sum of digits of n ; Initialize answer ; Convert string to character array ; Find sum of digits of N ; Multiply N with x ; Sum of digits of the new number ; If condition satisfies ; Print answer ; Driver Code ; Function call
def find_num ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE digitsOfN = str ( n ) NEW_LINE sumOfDigitsN = 0 NEW_LINE for c in digitsOfN : NEW_LINE INDENT sumOfDigitsN += int ( c ) NEW_LINE DEDENT for x in range ( 11 , 50 ) : NEW_LINE INDENT newNum = x * int ( n ) NEW_LINE tempSumDigits = 0 NEW_LINE temp = str ( newNum ) NEW_LINE for c in temp : NEW_LINE INDENT tempSumDigits += int ( c ) NEW_LINE DEDENT if ( tempSumDigits == sumOfDigitsN ) : NEW_LINE INDENT ans = x NEW_LINE break NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = "3029" NEW_LINE find_num ( N ) NEW_LINE DEDENT
Print all distinct strings from a given array | Function to find the distinct strings from the given array ; Stores distinct strings from the given array ; Traverse the array ; If current string not present into the set ; Insert current string into the set ; Traverse the set DistString ; Print distinct string ; Driver Code ; Stores length of the array
def findDisStr ( arr , N ) : NEW_LINE INDENT DistString = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] not in DistString ) : NEW_LINE INDENT DistString . add ( arr [ i ] ) NEW_LINE DEDENT DEDENT for string in DistString : NEW_LINE INDENT print ( string , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " Geeks " , " For " , " Geeks " , " Code " , " Coder " ] NEW_LINE N = len ( arr ) NEW_LINE findDisStr ( arr , N ) NEW_LINE DEDENT
Reverse substrings of given string according to specified array indices | Function to perform the reversal operation on the given string ; Size of string ; Stores the count of indices ; Count the positions where reversals will begin ; Store the count of reversals beginning at position i ; Check if the count [ i ] is odd the swap the character ; Return the updated string ; Driver Code ; Given str ; Given array of reversing index ; Function Call
def modifyString ( A , str , K ) : NEW_LINE INDENT N = len ( str ) NEW_LINE count = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( K ) : NEW_LINE INDENT count [ A [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 1 , N // 2 + 1 ) : NEW_LINE INDENT count [ i ] = count [ i ] + count [ i - 1 ] NEW_LINE if ( count [ i ] & 1 ) : NEW_LINE INDENT str [ i - 1 ] , str [ N - i ] = str [ N - i ] , str [ i - 1 ] NEW_LINE DEDENT DEDENT return " " . join ( str ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " abcdef " NEW_LINE str = [ i for i in str1 ] NEW_LINE arr = [ 1 , 2 , 3 ] NEW_LINE K = len ( arr ) NEW_LINE print ( modifyString ( arr , str , K ) ) NEW_LINE DEDENT
Convert given Float value to equivalent Fraction | Python3 program for the above approach ; Function to convert the floating values into fraction ; Initialize variables ; Traverse the floating string ; Check if decimal part exist ; Check if recurrence sequence exist ; Retrieve decimal part and recurrence resquence ; Traverse the string ; Convert to integer ; If no recurrence sequence exist ; Initialize numerator & denominator ; No reccuring term ; Print the result ; If reccuring term exist ; Convert reccuring term to integer ; reccu . size ( ) is num of digit in reccur term ; eq 2 - eq 1 ; Print the result ; Driver Code ; Given str ; Function Call
from math import gcd NEW_LINE def findFraction ( s ) : NEW_LINE INDENT be_deci = " " NEW_LINE af_deci = " " NEW_LINE reccu = " " NEW_LINE x = True NEW_LINE y = False NEW_LINE z = False NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == ' . ' ) : NEW_LINE INDENT x = False NEW_LINE y = True NEW_LINE continue NEW_LINE DEDENT if ( s [ i ] == ' ( ' ) : NEW_LINE INDENT z = True NEW_LINE y = False NEW_LINE continue NEW_LINE DEDENT if ( x ) : NEW_LINE INDENT be_deci += s [ i ] NEW_LINE DEDENT if ( y ) : NEW_LINE INDENT af_deci += s [ i ] NEW_LINE DEDENT if ( z ) : NEW_LINE INDENT while i < len ( s ) and s [ i ] != ' ) ' : NEW_LINE INDENT reccu += s [ i ] NEW_LINE i += 1 NEW_LINE DEDENT break NEW_LINE DEDENT DEDENT num_be_deci = int ( be_deci ) NEW_LINE num_af_deci = 0 NEW_LINE if len ( af_deci ) != 0 : NEW_LINE INDENT num_af_deci = int ( af_deci ) NEW_LINE DEDENT numr = ( num_be_deci * pow ( 10 , len ( af_deci ) ) + num_af_deci ) NEW_LINE deno = pow ( 10 , len ( af_deci ) ) NEW_LINE if len ( reccu ) == 0 : NEW_LINE INDENT gd = gcd ( numr , deno ) NEW_LINE print ( numr // gd , " / " , deno // gd ) NEW_LINE DEDENT else : NEW_LINE INDENT reccu_num = int ( reccu ) NEW_LINE numr1 = ( numr * pow ( 10 , len ( reccu ) ) + reccu_num ) NEW_LINE deno1 = deno * pow ( 10 , len ( reccu ) ) NEW_LINE res_numr = numr1 - numr NEW_LINE res_deno = deno1 - deno NEW_LINE gd = gcd ( res_numr , res_deno ) NEW_LINE print ( res_numr // gd , " ▁ / ▁ " , res_deno // gd ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "23.98(231 ) " NEW_LINE findFraction ( str ) NEW_LINE DEDENT
Lexicographically largest string possible for a given cost of appending characters | Function to find the lexicographically largest string possible ; If sum is less than 0 ; If sum is equal to 0 ; If sum is less than 0 ; Add current character ; Check if selecting current character generates lexicographically largest string ; Backtrack if solution not found ; Find the lexicographically largest string excluding the current character ; Function to print the lexicographically largest string generated ; Function call ; Stores the string ; Print the lexicographically largest string formed ; Driver code ; Cost of adding each alphabet ; Cost of generating the string
def lexi_largest_string ( a , i , sum , v ) : NEW_LINE INDENT if ( sum < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( sum == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( i < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT v . append ( i ) NEW_LINE if ( lexi_largest_string ( a , i , sum - a [ i ] , v ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT v . pop ( - 1 ) NEW_LINE return lexi_largest_string ( a , i - 1 , sum , v ) NEW_LINE DEDENT def generateString ( a , sum ) : NEW_LINE INDENT v = [ ] NEW_LINE lexi_largest_string ( a , 25 , sum , v ) NEW_LINE s = " " NEW_LINE for j in range ( len ( v ) ) : NEW_LINE INDENT s += chr ( v [ j ] + ord ( ' a ' ) ) NEW_LINE DEDENT print ( s ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 1 , 2 , 33 , 4 , 6 , 9 , 7 , 36 , 32 , 58 , 32 , 28 , 904 , 22 , 255 , 47 , 69 , 558 , 544 , 21 , 36 , 48 , 85 , 48 , 58 ] NEW_LINE sum = 236 NEW_LINE generateString ( a , sum ) NEW_LINE DEDENT
Count of distinct permutations of every possible length of given string | Function to find the factorial of a number ; Loop to find the factorial of the given number ; Function to find the number of permutations possible for a given string ; Function to find the total number of combinations possible ; Driver Code
def fact ( a ) : NEW_LINE INDENT f = 1 NEW_LINE for i in range ( 2 , a + 1 ) : NEW_LINE INDENT f = f * i NEW_LINE DEDENT return f NEW_LINE DEDENT def permute ( n , r ) : NEW_LINE INDENT ans = 0 NEW_LINE ans = fact ( n ) // fact ( n - r ) NEW_LINE return ans NEW_LINE DEDENT def findPermutations ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for r in range ( 1 , n + 1 ) : NEW_LINE INDENT P = permute ( n , r ) NEW_LINE sum = sum + P NEW_LINE DEDENT return sum NEW_LINE DEDENT str = " xz " NEW_LINE n = len ( str ) NEW_LINE print ( findPermutations ( n ) ) NEW_LINE
Cycles of length n in an undirected and connected graph | Number of vertices ; mark the vertex vert as visited ; if the path of length ( n - 1 ) is found ; mark vert as un - visited to make it usable again . ; Check if vertex vert can end with vertex start ; For searching every possible path of length ( n - 1 ) ; DFS for searching path by decreasing length by 1 ; marking vert as unvisited to make it usable again . ; Counts cycles of length N in an undirected and connected graph . ; all vertex are marked un - visited initially . ; Searching for cycle by using v - n + 1 vertices ; ith vertex is marked as visited and will not be visited again . ; main :
V = 5 NEW_LINE def DFS ( graph , marked , n , vert , start , count ) : NEW_LINE INDENT marked [ vert ] = True NEW_LINE if n == 0 : NEW_LINE INDENT marked [ vert ] = False NEW_LINE if graph [ vert ] [ start ] == 1 : NEW_LINE INDENT count = count + 1 NEW_LINE return count NEW_LINE DEDENT else : NEW_LINE INDENT return count NEW_LINE DEDENT DEDENT for i in range ( V ) : NEW_LINE INDENT if marked [ i ] == False and graph [ vert ] [ i ] == 1 : NEW_LINE INDENT count = DFS ( graph , marked , n - 1 , i , start , count ) NEW_LINE DEDENT DEDENT marked [ vert ] = False NEW_LINE return count NEW_LINE DEDENT def countCycles ( graph , n ) : NEW_LINE INDENT marked = [ False ] * V NEW_LINE count = 0 NEW_LINE for i in range ( V - ( n - 1 ) ) : NEW_LINE INDENT count = DFS ( graph , marked , n - 1 , i , i , count ) NEW_LINE marked [ i ] = True NEW_LINE DEDENT return int ( count / 2 ) NEW_LINE DEDENT graph = [ [ 0 , 1 , 0 , 1 , 0 ] , [ 1 , 0 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 1 , 0 ] , [ 1 , 0 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 1 , 0 ] ] NEW_LINE n = 4 NEW_LINE print ( " Total ▁ cycles ▁ of ▁ length ▁ " , n , " ▁ are ▁ " , countCycles ( graph , n ) ) NEW_LINE
String hashing using Polynomial rolling hash function | Function to calculate the hash of a string ; P and M ; Loop to calculate the hash value by iterating over the elements of string ; Driver Code ; Given string
def polynomialRollingHash ( str ) : NEW_LINE INDENT p = 31 NEW_LINE m = 1e9 + 9 NEW_LINE power_of_p = 1 NEW_LINE hash_val = 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT hash_val = ( ( hash_val + ( ord ( str [ i ] ) - ord ( ' a ' ) + 1 ) * power_of_p ) % m ) NEW_LINE power_of_p = ( power_of_p * p ) % m NEW_LINE DEDENT return int ( hash_val ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " geeksforgeeks " NEW_LINE print ( " Hash ▁ of ▁ ' { } ' ▁ = ▁ { } " . format ( str1 , polynomialRollingHash ( str1 ) ) ) NEW_LINE DEDENT
Find first non | Python3 implementation to find the first non - repeating element of the string using Linked List ; Function to find the first non - repeating element of the given string using Linked List ; Driver Code ; Function Call
import collections NEW_LINE def firstNonRepElement ( str ) : NEW_LINE INDENT list . append ( str [ 0 ] ) NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if str [ i ] in list : NEW_LINE INDENT list . remove ( str [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT list . append ( str [ i ] ) NEW_LINE DEDENT DEDENT print ( list [ 0 ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " geeksforgeeks " ; NEW_LINE firstNonRepElement ( str ) ; NEW_LINE DEDENT
DFA that begins with ' a ' but does not contain substring ' aab ' | Function for state A transition ; If at index 0 ' a ' if found then call stateB function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for state B transition ; Length of string become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateD function with passing n [ 1 : ] to it ; Function for state C transition ; Length of string become 0 then print Accepted ; If at index 0 ' a ' if found then call stateC function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateQ function with passing n to it ; Function for state D transition ; Length of string become 0 then print Accepted ; If at index 0 ' a ' if found then call stateB function with passing n [ 1 : ] to it ; If at index 0 ' b ' if found then call stateD function with passing n [ 1 : ] to it ; Function for state Q transition ; In dead state it shows string not accepted ; Take string input ; Call stateA to check the input
def stateA ( n ) : NEW_LINE INDENT if ( n [ 0 ] == ' a ' ) : NEW_LINE INDENT stateB ( n [ 1 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stateQ ( n ) NEW_LINE DEDENT DEDENT def stateB ( n ) : NEW_LINE INDENT if ( len ( n ) == 0 ) : NEW_LINE INDENT print ( " Accepted " ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( n [ 0 ] == ' a ' ) : NEW_LINE INDENT stateC ( n [ 1 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stateD ( n [ 1 : ] ) NEW_LINE DEDENT DEDENT DEDENT def stateC ( n ) : NEW_LINE INDENT if ( len ( n ) == 0 ) : NEW_LINE INDENT print ( " Accepted " ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( n [ 0 ] == ' a ' ) : NEW_LINE INDENT stateC ( n [ 1 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stateQ ( n ) NEW_LINE DEDENT DEDENT DEDENT def stateD ( n ) : NEW_LINE INDENT if ( len ( n ) == 0 ) : NEW_LINE INDENT print ( " Accepted " ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( n [ 0 ] == ' a ' ) : NEW_LINE INDENT stateB ( n [ 1 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stateD ( n [ 1 : ] ) NEW_LINE DEDENT DEDENT DEDENT def stateQ ( n ) : NEW_LINE INDENT print ( " Not ▁ Accepted " ) NEW_LINE DEDENT n = " aaaba " NEW_LINE stateA ( n ) NEW_LINE
Count of substrings of length K with exactly K distinct characters | Function to return the required count of substrings ; Store the count ; Store the count of distinct characters in every window ; Store the frequency of the first K length substring ; Increase frequency of i - th character ; If K distinct characters exist ; Traverse the rest of the substring ; Increase the frequency of the last character of the current substring ; Decrease the frequency of the first character of the previous substring ; If the character is not present in the current substring ; If the count of distinct characters is 0 ; Return the count ; Driver code ; string str ; Integer K ; Print the count of K length substrings with k distinct characters
def countSubstrings ( str , K ) : NEW_LINE INDENT N = len ( str ) NEW_LINE answer = 0 NEW_LINE map = { } NEW_LINE for i in range ( K ) : NEW_LINE INDENT map [ str [ i ] ] = map . get ( str [ i ] , 0 ) + 1 NEW_LINE DEDENT if ( len ( map ) == K ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT for i in range ( K , N ) : NEW_LINE INDENT map [ str [ i ] ] = map . get ( str [ i ] , 0 ) + 1 NEW_LINE map [ str [ i - K ] ] -= 1 NEW_LINE if ( map [ str [ i - K ] ] == 0 ) : NEW_LINE INDENT del map [ str [ i - K ] ] NEW_LINE DEDENT if ( len ( map ) == K ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " aabcdabbcdc " NEW_LINE K = 3 NEW_LINE print ( countSubstrings ( str , K ) ) NEW_LINE DEDENT
Check if a string is a scrambled form of another string | Declaring unordered map globally ; Python3 program to check if a given string is a scrambled form of another string ; Strings of non - equal length cant ' be scramble strings ; Empty strings are scramble strings ; Equal strings are scramble strings ; Check for the condition of anagram ;  ; Checking if both Substrings are in map or are already calculated or not ; Declaring a flag variable ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ 0. . . i ] and if S2 [ i + 1. . . n ] is a scrambled string of S1 [ i + 1. . . n ] ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ n - i ... n ] and S2 [ i + 1. . . n ] is a scramble string of S1 [ 0. . . n - i - 1 ] ; Storing calculated value to map ; If none of the above conditions are satisfied ; Driver Code
map = { } NEW_LINE def isScramble ( S1 : str , S2 : str ) : NEW_LINE INDENT if len ( S1 ) != len ( S2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT n = len ( S1 ) NEW_LINE if not n : NEW_LINE INDENT return True NEW_LINE DEDENT if S1 == S2 : NEW_LINE INDENT return True NEW_LINE DEDENT if sorted ( S1 ) != sorted ( S2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT / * make key of type string for search in map * / NEW_LINE INDENT if ( S1 + ' ▁ ' + S2 in map ) : NEW_LINE INDENT return map [ S1 + ' ▁ ' + S2 ] NEW_LINE DEDENT flag = False NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( isScramble ( S1 [ : i ] , S2 [ : i ] ) and isScramble ( S1 [ i : ] , S2 [ i : ] ) ) : NEW_LINE INDENT flag = True NEW_LINE return True NEW_LINE DEDENT if ( isScramble ( S1 [ - i : ] , S2 [ : i ] ) and isScramble ( S1 [ : - i ] , S2 [ i : ] ) ) : NEW_LINE INDENT flag = True NEW_LINE return True NEW_LINE DEDENT DEDENT map [ S1 + " ▁ " + S2 ] = flag NEW_LINE return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S1 = " great " NEW_LINE S2 = " rgate " NEW_LINE if ( isScramble ( S1 , S2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Print the longest palindromic prefix of a given string | Function to find the longest prefix which is palindrome ; Find the length of the given string ; For storing the length of longest Prefix Palindrome ; Loop to check the substring of all length from 1 to n which is palindrome ; String of length i ; To store the value of temp ; Reversing the value of temp ; If string temp is palindromic then update the length ; Print the palindromic string of max_len ; Driver code ; Function call
def LongestPalindromicPrefix ( string ) : NEW_LINE INDENT n = len ( string ) NEW_LINE max_len = 0 NEW_LINE for length in range ( 0 , n + 1 ) : NEW_LINE INDENT temp = string [ 0 : length ] NEW_LINE temp2 = temp NEW_LINE temp3 = temp2 [ : : - 1 ] NEW_LINE if temp == temp3 : NEW_LINE INDENT max_len = length NEW_LINE DEDENT DEDENT print ( string [ 0 : max_len ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT string = " abaac " ; NEW_LINE LongestPalindromicPrefix ( string ) NEW_LINE DEDENT
K | Python program to find the K - th lexicographical string of length N ; Initialize the array to store the base 26 representation of K with all zeroes , that is , the initial string consists of N a 's ; Start filling all the N slots for the base 26 representation of K ; Store the remainder ; Reduce K ; If K is greater than the possible number of strings that can be represented by a string of length N ; Store the Kth lexicographical string ; Driver Code ; Reducing k value by 1 because our stored value starts from 0
def find_kth_string_of_n ( n , k ) : NEW_LINE INDENT d = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT d [ i ] = k % 26 NEW_LINE k //= 26 NEW_LINE DEDENT if k > 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT s = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT s += chr ( d [ i ] + ord ( ' a ' ) ) NEW_LINE DEDENT return s NEW_LINE DEDENT n = 3 NEW_LINE k = 10 NEW_LINE k -= 1 NEW_LINE print ( find_kth_string_of_n ( n , k ) ) NEW_LINE
Length of the smallest substring which contains all vowels | Python3 program to find the length of the smallest substring of which contains all vowels ; Map to store the frequency of vowels ; Store the indices which contains the vowels ; If all vowels are not present in the string ; If the frequency of the vowel at i - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the left ; Otherwise set flag1 ; If the frequency of the vowel at j - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the right ; Otherwise set flag2 ; If both flag1 and flag2 are set , break out of the loop as the substring length cannot be minimized ; Return the length of the substring ; Driver Code
from collections import defaultdict NEW_LINE def findMinLength ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE counts = defaultdict ( int ) NEW_LINE indices = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == ' a ' or s [ i ] == ' e ' or s [ i ] == ' o ' or s [ i ] == ' i ' or s [ i ] == ' u ' ) : NEW_LINE INDENT counts [ s [ i ] ] += 1 NEW_LINE indices . append ( i ) NEW_LINE DEDENT DEDENT if len ( counts ) < 5 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT flag1 = 0 NEW_LINE flag2 = 0 NEW_LINE i = 0 NEW_LINE j = len ( indices ) - 1 NEW_LINE while ( j - i ) >= 4 : NEW_LINE INDENT if ( ~ flag1 and counts [ s [ indices [ i ] ] ] > 1 ) : NEW_LINE INDENT counts [ s [ indices [ i ] ] ] -= 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT flag1 = 1 NEW_LINE DEDENT if ( ~ flag2 and counts [ s [ indices [ j ] ] ] > 1 ) : NEW_LINE INDENT counts [ s [ indices [ j ] ] ] -= 1 NEW_LINE j -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT flag2 = 1 NEW_LINE DEDENT if ( flag1 and flag2 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return ( indices [ j ] - indices [ i ] + 1 ) NEW_LINE DEDENT s = " aaeebbeaccaaoiuooooooooiuu " NEW_LINE print ( findMinLength ( s ) ) NEW_LINE
Longest Subsequence of a String containing only Consonants | Returns true if x is consonants . ; '' Function to check whether a character  is consonants or not  ; Function to find the longest subsequence which contain all consonants ; Driver code ; Function call
def isComsomamts ( x ) : NEW_LINE INDENT x = x . lower ( ) NEW_LINE return not ( x == ' a ' or x == ' e ' or x == ' i ' or x == ' o ' or x == ' u ' ) NEW_LINE DEDENT def longestConsonantsSubsequence ( s ) : NEW_LINE INDENT answer = ' ' NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if isComsomamts ( s [ i ] ) : NEW_LINE INDENT answer += s [ i ] NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT s = ' geeksforgeeks ' NEW_LINE print ( longestConsonantsSubsequence ( s ) ) NEW_LINE
Find the length of the longest subsequence with first K alphabets having same frequency | Python3 program to find the longest subsequence with first K alphabets having same frequency ; Function to return the length of the longest subsequence with first K alphabets having same frequency ; Map to store frequency of all characters in the string ; Variable to store the frequency of the least frequent of first K alphabets ; Driver code
from collections import defaultdict NEW_LINE def lengthOfSubsequence ( st , K ) : NEW_LINE INDENT mp = defaultdict ( int ) NEW_LINE for ch in st : NEW_LINE INDENT mp [ ch ] += 1 NEW_LINE DEDENT minimum = mp [ ' A ' ] NEW_LINE for i in range ( 1 , K ) : NEW_LINE INDENT minimum = min ( minimum , mp [ chr ( i + ord ( ' A ' ) ) ] ) NEW_LINE DEDENT return ( minimum * K ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " ACAABCCAB " NEW_LINE K = 3 NEW_LINE print ( lengthOfSubsequence ( st , K ) ) NEW_LINE DEDENT