codeparrot/xlcost-text-to-code huggingface dataset:
Check if string is palindrome after removing all consecutive duplicates | Function to check if a string is palindrome or not ; Length of the string ; Check if its a palindrome ; If the palindromic condition is not met ; Return true as Str is palindromic ; Function to check if string str is palindromic after removing every consecutive characters from the str ; Length of the string str ; Create an empty compressed string ; The first character will always be included in final string ; Check all the characters of the string ; If the current character is not same as its previous one , then insert it in the final string ; Check if the compressed string is a palindrome ; Driver Code ; Given string ; Function call
def isPalindrome ( Str ) : NEW_LINE INDENT Len = len ( Str ) NEW_LINE for i in range ( Len ) : NEW_LINE INDENT if ( Str [ i ] != Str [ Len - i - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isCompressablePalindrome ( Str ) : NEW_LINE INDENT Len = len ( Str ) NEW_LINE compressed = " " NEW_LINE compressed += Str [ 0 ] NEW_LINE for i in range ( 1 , Len ) : NEW_LINE INDENT if ( Str [ i ] != Str [ i - 1 ] ) : NEW_LINE INDENT compressed += Str [ i ] NEW_LINE DEDENT DEDENT return isPalindrome ( compressed ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Str = " abbcbbbaaa " NEW_LINE if ( isCompressablePalindrome ( Str ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Count of substrings consisting only of vowels | function to check vowel or not ; Function to Count all substrings in a string which contains only vowels ; if current character is vowel ; increment ; Count all possible substring of calculated length ; Reset the length ; Add remaining possible substrings consisting of vowels occupying last indices of the string ; Driver Program
def isvowel ( ch ) : NEW_LINE INDENT return ( ch in " aeiou " ) NEW_LINE DEDENT def CountTotal ( s ) : NEW_LINE INDENT ans = 0 NEW_LINE n = len ( s ) NEW_LINE cnt = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( isvowel ( s [ i ] ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += ( cnt * ( cnt + 1 ) // 2 ) NEW_LINE cnt = 0 NEW_LINE DEDENT DEDENT if ( cnt != 0 ) : NEW_LINE INDENT ans += ( cnt * ( cnt + 1 ) // 2 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE print ( CountTotal ( s ) ) NEW_LINE
Number formed by flipping all bits to the left of rightmost set bit | Function to get the total count ; Moving until we get the rightmost set bit ; To get total number of bits in a number ; Function to find the integer formed after flipping all bits to the left of the rightmost set bit ; Find the total count of bits and the rightmost set bit ; XOR given number with the number which has is made up of only totbits set ; To avoid flipping the bits to the right of the set bit , take XOR with the number made up of only set firstbits ; Driver Code
def getTotCount ( num ) : NEW_LINE INDENT totCount = 1 NEW_LINE firstCount = 1 NEW_LINE temp = 1 NEW_LINE while ( not ( num & temp ) ) : NEW_LINE INDENT temp = temp << 1 NEW_LINE totCount += 1 NEW_LINE DEDENT firstCount = totCount NEW_LINE temp = num >> totCount NEW_LINE while ( temp ) : NEW_LINE INDENT totCount += 1 NEW_LINE temp = temp >> 1 NEW_LINE DEDENT return totCount , firstCount NEW_LINE DEDENT def flipBitsFromRightMostSetBit ( num ) : NEW_LINE INDENT totbit , firstbit = getTotCount ( num ) NEW_LINE num1 = num ^ ( ( 1 << totbit ) - 1 ) NEW_LINE num1 = num1 ^ ( ( 1 << firstbit ) - 1 ) NEW_LINE return num1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 120 NEW_LINE print ( flipBitsFromRightMostSetBit ( n ) ) NEW_LINE DEDENT
Longest substring of vowels with no two adjacent alphabets same | Function to check a character is vowel or not ; Function to find length of longest substring consisting only of vowels and no similar adjacent alphabets ; Stores max length of valid subString ; Stores length of current valid subString ; If curr and prev character are not same , include it ; If same as prev one , start new subString from here ; Store max in maxLen ; Driver code
def isVowel ( x ) : NEW_LINE INDENT return ( x == ' a ' or x == ' e ' or x == ' i ' or x == ' o ' or x == ' u ' ) ; NEW_LINE DEDENT def findMaxLen ( s ) : NEW_LINE INDENT maxLen = 0 NEW_LINE cur = 0 NEW_LINE if ( isVowel ( s [ 0 ] ) ) : NEW_LINE INDENT maxLen = 1 ; NEW_LINE DEDENT cur = maxLen NEW_LINE for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT if ( isVowel ( s [ i ] ) ) : NEW_LINE INDENT if ( s [ i ] != s [ i - 1 ] ) : NEW_LINE INDENT cur += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cur = 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT cur = 0 ; NEW_LINE DEDENT maxLen = max ( cur , maxLen ) ; NEW_LINE DEDENT return maxLen NEW_LINE DEDENT Str = " aeoibsddaeiouudb " NEW_LINE print ( findMaxLen ( Str ) ) NEW_LINE
Count of non | Iterative Function to calculate base ^ pow in O ( log y ) ; Function to return the count of non palindromic strings ; Count of strings using n characters with repetitions allowed ; Count of palindromic strings ; Count of non - palindromic strings ; Driver code
def power ( base , pwr ) : NEW_LINE INDENT res = 1 NEW_LINE while ( pwr > 0 ) : NEW_LINE INDENT if ( pwr & 1 ) : NEW_LINE INDENT res = res * base NEW_LINE DEDENT base = base * base NEW_LINE pwr >>= 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def countNonPalindromicString ( n , m ) : NEW_LINE INDENT total = power ( n , m ) NEW_LINE palindrome = power ( n , m // 2 + m % 2 ) NEW_LINE count = total - palindrome NEW_LINE return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE m = 5 NEW_LINE print ( countNonPalindromicString ( n , m ) ) NEW_LINE DEDENT
Count of K | To store the frequency array ; Function to check palindromic of of any substring using frequency array ; Initialise the odd count ; Traversing frequency array to compute the count of characters having odd frequency ; Returns true if odd count is atmost 1 ; Function to count the total number substring whose any permutations are palindromic ; Computing the frequency of first K character of the string ; To store the count of palindromic permutations ; Checking for the current window if it has any palindromic permutation ; Start and end poof window ; Decrementing count of first element of the window ; Incrementing count of next element of the window ; Checking current window character frequency count ; Return the final count ; Given string str ; Window of size K ; Function call
freq = [ 0 ] * 26 NEW_LINE def checkPalindrome ( ) : NEW_LINE INDENT oddCnt = 0 NEW_LINE for x in freq : NEW_LINE INDENT if ( x % 2 == 1 ) : NEW_LINE INDENT oddCnt += 1 NEW_LINE DEDENT DEDENT return oddCnt <= 1 NEW_LINE DEDENT def countPalindromePermutation ( s , k ) : NEW_LINE INDENT for i in range ( k ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - 97 ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE if ( checkPalindrome ( ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT i = 0 NEW_LINE j = k NEW_LINE while ( j < len ( s ) ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - 97 ] -= 1 NEW_LINE i += 1 NEW_LINE freq [ ord ( s [ j ] ) - 97 ] += 1 NEW_LINE j += 1 NEW_LINE if ( checkPalindrome ( ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT str = " abbaca " NEW_LINE K = 3 NEW_LINE print ( countPalindromePermutation ( str , K ) ) NEW_LINE
Minimum flips required to form given binary string where every flip changes all bits to its right as well | Function to return the count of minimum flips required ; If curr occurs in the final string ; Switch curr to '0' if '1' or vice - versa ; Driver Code
def minFlips ( target ) : NEW_LINE INDENT curr = '1' NEW_LINE count = 0 NEW_LINE for i in range ( len ( target ) ) : NEW_LINE INDENT if ( target [ i ] == curr ) : NEW_LINE INDENT count += 1 NEW_LINE curr = chr ( 48 + ( ord ( curr ) + 1 ) % 2 ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "011000" NEW_LINE print ( minFlips ( S ) ) NEW_LINE DEDENT
Check if a number ends with another number or not | Python3 program for the above approach ; Function to check if B is a suffix of A or not ; Find the number of digit in B ; Subtract B from A ; Returns true , if B is a suffix of A ; Given numbers ; Function Call ; If B is a suffix of A , then print " Yes "
import math NEW_LINE def checkSuffix ( A , B ) : NEW_LINE INDENT digit_B = int ( math . log10 ( B ) ) + 1 ; NEW_LINE A -= B ; NEW_LINE return ( A % int ( math . pow ( 10 , digit_B ) ) ) ; NEW_LINE DEDENT A = 12345 ; B = 45 ; NEW_LINE result = checkSuffix ( A , B ) ; NEW_LINE if ( result == 0 ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Minimum length of substring whose rotation generates a palindromic substring | Python3 program to find the minimum length of substring whose rotation generates a palindromic substring ; Function to return the minimum length of substring ; Store the index of previous occurrence of the character ; Variable to store the maximum length of substring ; If the current character hasn 't appeared yet ; If the character has occured within one or two previous index , a palindromic substring already exists ; Update the maximum ; Replace the previous index of the character by the current index ; If character appeared at least twice ; Driver Code
import sys NEW_LINE INT_MAX = sys . maxsize ; NEW_LINE def count_min_length ( s ) : NEW_LINE INDENT hash = [ 0 ] * 26 ; NEW_LINE ans = sys . maxsize ; NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT hash [ i ] = - 1 ; NEW_LINE DEDENT for i in range ( len ( s ) ) : NEW_LINE INDENT if ( hash [ ord ( s [ i ] ) - ord ( ' a ' ) ] == - 1 ) : NEW_LINE INDENT hash [ ord ( s [ i ] ) - ord ( ' a ' ) ] = i ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( hash [ ord ( s [ i ] ) - ord ( ' a ' ) ] == i - 1 or hash [ ord ( s [ i ] ) - ord ( ' a ' ) ] == i - 2 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT ans = min ( ans , i - hash [ ord ( s [ i ] ) - ord ( ' a ' ) ] - 1 ) ; NEW_LINE hash [ ord ( s [ i ] ) - ord ( ' a ' ) ] = i ; NEW_LINE DEDENT DEDENT if ( ans == INT_MAX ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " abcdeba " ; NEW_LINE print ( count_min_length ( string ) ) ; NEW_LINE DEDENT
Program to remove HTML tags from a given String | Python3 program for the above approach ; Function to remove the HTML tags from the given tags ; Print string after removing tags ; Driver code ; Given String ; Function call to print the HTML string after removing tags
import re NEW_LINE def RemoveHTMLTags ( strr ) : NEW_LINE INDENT print ( re . compile ( r ' < [ ^ > ] + > ' ) . sub ( ' ' , strr ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " < div > < b > Geeks ▁ for ▁ Geeks < / b > < / div > " NEW_LINE RemoveHTMLTags ( strr ) ; NEW_LINE DEDENT
Score of Parentheses using Tree | Customized tree class or struct , contains all required methods . ; Function to add a child into the list of children ; Function to change the parent pointer to the node passed ; Function to return the parent of the current node ; Function to compute the score recursively . ; Base case ; Adds scores of all children ; Function to create the tree structure ; Creating a node for every " ( ) " ; If we find " ( " we add a node as a child ; On finding " ) " which confirms that a pair is closed , we go back to the parent ; Driver code ; Generating the tree ; Computing the score
class TreeNode : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . parent = None NEW_LINE self . children = [ ] NEW_LINE DEDENT def addChild ( self , node ) : NEW_LINE INDENT self . children . append ( node ) ; NEW_LINE DEDENT def setParent ( self , node ) : NEW_LINE INDENT self . parent = node ; NEW_LINE DEDENT def getParent ( self ) : NEW_LINE INDENT return self . parent ; NEW_LINE DEDENT def computeScore ( self ) : NEW_LINE INDENT if ( len ( self . children ) == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT res = 0 ; NEW_LINE for curr in self . children : NEW_LINE INDENT res += curr . computeScore ( ) ; NEW_LINE DEDENT if ( self . parent == None ) : NEW_LINE INDENT return res ; NEW_LINE DEDENT else : NEW_LINE INDENT return 2 * res ; NEW_LINE DEDENT DEDENT DEDENT def computeTree ( s ) : NEW_LINE INDENT current = TreeNode ( ) ; NEW_LINE root = current ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == ' ( ' ) : NEW_LINE INDENT child = TreeNode ( ) ; NEW_LINE child . setParent ( current ) ; NEW_LINE current . addChild ( child ) ; NEW_LINE current = child ; NEW_LINE DEDENT else : NEW_LINE INDENT current = current . getParent ( ) ; NEW_LINE DEDENT DEDENT return root ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " ( ( ) ( ( ) ) ) " ; NEW_LINE root = computeTree ( s ) ; NEW_LINE print ( root . computeScore ( ) ) NEW_LINE DEDENT
Move all occurrence of letter ' x ' from the string s to the end using Recursion | Recursive program to bring ' x ' to the end ; When the string is completed from reverse direction end of recursion ; If the character x is found ; Transverse the whole string ; Swap the x so that it moves to the last ; Call to the smaller problem now ; Driver code ; Size of a ; Call to rec
def rec ( a , i ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT a . pop ( ) NEW_LINE print ( " " . join ( a ) ) NEW_LINE return NEW_LINE DEDENT if ( a [ i ] == ' x ' ) : NEW_LINE j = i NEW_LINE while ( a [ j ] != ' \0' and a [ j + 1 ] != ' \0' ) : NEW_LINE INDENT ( a [ j ] , a [ j + 1 ] ) = ( a [ j + 1 ] , a [ j ] ) NEW_LINE j += 1 NEW_LINE DEDENT rec ( a , i - 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ ' g ' , ' e ' , ' e ' , ' k ' , ' x ' , ' s ' , ' x ' , ' x ' , ' k ' , ' s ' , ' \0' ] NEW_LINE n = 10 NEW_LINE rec ( a , n - 1 ) NEW_LINE DEDENT
Find the largest Alphabetic character present in the string | Function to find the Largest Alphabetic Character ; Array for keeping track of both uppercase and lowercase english alphabets ; Iterate from right side of array to get the largest index character ; Check for the character if both its uppercase and lowercase exist or not ; Return - 1 if no such character whose uppercase and lowercase present in string str ; Driver code
def largestCharacter ( str ) : NEW_LINE INDENT uppercase = [ False ] * 26 NEW_LINE lowercase = [ False ] * 26 NEW_LINE arr = list ( str ) NEW_LINE for c in arr : NEW_LINE INDENT if ( c . islower ( ) ) : NEW_LINE INDENT lowercase [ ord ( c ) - ord ( ' a ' ) ] = True NEW_LINE DEDENT if ( c . isupper ( ) ) : NEW_LINE INDENT uppercase [ ord ( c ) - ord ( ' A ' ) ] = True NEW_LINE DEDENT DEDENT for i in range ( 25 , - 1 , - 1 ) : NEW_LINE INDENT if ( uppercase [ i ] and lowercase [ i ] ) : NEW_LINE INDENT return chr ( i + ord ( ' A ' ) ) + " " NEW_LINE DEDENT DEDENT return " - 1" NEW_LINE DEDENT str = " admeDCAB " NEW_LINE print ( largestCharacter ( str ) ) NEW_LINE
Print string after removing all ( “ 10 ” or “ 01 ” ) from the binary string | Function to print the final string after removing all the occurrences of "10" and "01" from the given binary string ; Variables to store the count of 1 ' s ▁ and ▁ 0' s ; Length of the string ; For loop to count the occurrences of 1 ' s ▁ and ▁ 0' s in the string ; To check if the count of 1 ' s ▁ is ▁ ▁ greater ▁ than ▁ the ▁ count ▁ of ▁ 0' s or not . If x is greater , then those many 1 's  are printed. ; Length of the final remaining string after removing all the occurrences ; Printing the final string ; Driver Code
def finalString ( st ) : NEW_LINE INDENT x , y = 0 , 0 NEW_LINE n = len ( st ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( st [ i ] == '1' ) : NEW_LINE INDENT x += 1 NEW_LINE DEDENT else : NEW_LINE INDENT y += 1 NEW_LINE DEDENT DEDENT if ( x > y ) : NEW_LINE INDENT left = 1 NEW_LINE DEDENT else : NEW_LINE INDENT left = 0 NEW_LINE DEDENT length = n - 2 * min ( x , y ) ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT print ( left , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = "010110100100000" NEW_LINE finalString ( st ) NEW_LINE DEDENT
Longest palindrome formed by concatenating and reordering strings of equal length | Function to print the longest palindrome ; Printing every string in left vector ; Printing the palindromic string in the middle ; Printing the reverse of the right vector to make the final output palindromic ; Function to find and print the longest palindrome that can be formed ; Inserting each string in the set ; Vectors to add the strings in the left and right side ; To add the already present palindrome string in the middle of the solution ; Iterating through all the given strings ; If the string is a palindrome it is added in the middle ; Checking if the reverse of the string is already present in the set ; Driver code
def printPalindrome ( left , mid , right ) : NEW_LINE INDENT for x in left : NEW_LINE INDENT print ( x , end = " " ) NEW_LINE DEDENT print ( mid , end = " " ) NEW_LINE right = right [ : : - 1 ] NEW_LINE for x in right : NEW_LINE INDENT print ( x , end = " " ) NEW_LINE DEDENT print ( ' ' , end = " " ) NEW_LINE DEDENT def findPalindrome ( S , N , M ) : NEW_LINE INDENT d = set ( ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT d . add ( S [ i ] ) NEW_LINE DEDENT left = [ ] NEW_LINE right = [ ] NEW_LINE mid = " " NEW_LINE for i in range ( N ) : NEW_LINE INDENT t = S [ i ] NEW_LINE t = t [ : : - 1 ] NEW_LINE if ( t == S [ i ] ) : NEW_LINE INDENT mid = t NEW_LINE DEDENT elif ( t in d ) : NEW_LINE INDENT left . append ( S [ i ] ) NEW_LINE right . append ( t ) NEW_LINE d . remove ( S [ i ] ) NEW_LINE d . remove ( t ) NEW_LINE DEDENT DEDENT printPalindrome ( left , mid , right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = [ " tab " , " one " , " bat " ] NEW_LINE M = 3 NEW_LINE N = len ( S ) NEW_LINE findPalindrome ( S , N , M ) NEW_LINE DEDENT
Lexicographically smaller string by swapping at most one character pair | Function that finds whether is it possible to make string A lexicographically smaller than string B ; Condition if string A is already smaller than B ; Sorting temp string ; Condition for first changed character of string A and temp ; Condition if string A is already sorted ; Finding first changed character from last of string A ; Swap the two characters ; Condition if string A is smaller than B ; Driver Code
def IsLexicographicallySmaller ( A , B ) : NEW_LINE INDENT if ( A < B ) : NEW_LINE INDENT return True NEW_LINE DEDENT temp = A NEW_LINE temp = ' ' . join ( sorted ( temp ) ) NEW_LINE index = - 1 NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT if ( A [ i ] != temp [ i ] ) : NEW_LINE INDENT index = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( index == - 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT j = 0 NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT if ( A [ i ] == temp [ index ] ) : NEW_LINE INDENT j = i NEW_LINE DEDENT DEDENT A = list ( A ) NEW_LINE A [ index ] , A [ j ] = A [ j ] , A [ index ] NEW_LINE A = ' ' . join ( A ) NEW_LINE if ( A < B ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT A = " AGAIN " NEW_LINE B = " ACTION " NEW_LINE if ( IsLexicographicallySmaller ( A , B ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Length of longest palindromic sub | Function to find maximum of the two variables ; Function to find the longest palindromic substring : Recursion ; Base condition when the start index is greater than end index ; Base condition when both the start and end index are equal ; Condition when corner characters are equal in the string ; Recursive call to find the longest Palindromic string by excluding the corner characters ; Recursive call to find the longest Palindromic string by including one corner character at a time ; Function to find the longest palindromic sub - string ; Utility function call ; Driver Code ; Function Call
def maxi ( x , y ) : NEW_LINE INDENT if x > y : NEW_LINE INDENT return x NEW_LINE DEDENT else : NEW_LINE INDENT return y NEW_LINE DEDENT DEDENT def longestPalindromic ( strn , i , j , count ) : NEW_LINE INDENT if i > j : NEW_LINE INDENT return count NEW_LINE DEDENT if i == j : NEW_LINE INDENT return ( count + 1 ) NEW_LINE DEDENT if strn [ i ] == strn [ j ] : NEW_LINE INDENT count = longestPalindromic ( strn , i + 1 , j - 1 , count + 2 ) NEW_LINE return maxi ( count , maxi ( longestPalindromic ( strn , i + 1 , j , 0 ) , longestPalindromic ( strn , i , j - 1 , 0 ) ) ) NEW_LINE DEDENT return maxi ( longestPalindromic ( strn , i + 1 , j , 0 ) , longestPalindromic ( strn , i , j - 1 , 0 ) ) NEW_LINE DEDENT def longest_palindromic_substr ( strn ) : NEW_LINE INDENT k = len ( strn ) - 1 NEW_LINE return longestPalindromic ( strn , 0 , k , 0 ) NEW_LINE DEDENT strn = " aaaabbaa " NEW_LINE print ( longest_palindromic_substr ( strn ) ) NEW_LINE
Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency | Function to find the maximum possible prefix of the string ; Hash map to store the frequency of the characters in the string ; Iterate over the string to find the occurence of each Character ; Minimum frequency of the Characters ; Loop to find the count of minimum frequency in the hash - map ; Loop to find the maximum possible length of the prefix in the string ; Condition to check if the frequency is greater than minimum possible freq ; maxprefix string and its length . ; Driver code ; String is initialize . ; str is passed in MaxPrefix function .
def MaxPrefix ( string ) : NEW_LINE INDENT Dict = { } NEW_LINE maxprefix = 0 NEW_LINE for i in string : NEW_LINE INDENT Dict [ i ] = Dict . get ( i , 0 ) + 1 NEW_LINE DEDENT minfrequency = min ( Dict . values ( ) ) NEW_LINE countminFrequency = 0 NEW_LINE for x in Dict : NEW_LINE INDENT if ( Dict [ x ] == minfrequency ) : NEW_LINE INDENT countminFrequency += 1 NEW_LINE DEDENT DEDENT mapper = { } NEW_LINE indi = 0 NEW_LINE for i in string : NEW_LINE INDENT mapper [ i ] = mapper . get ( i , 0 ) + 1 NEW_LINE if ( mapper [ i ] > countminFrequency ) : NEW_LINE INDENT break NEW_LINE DEDENT indi += 1 NEW_LINE DEDENT print ( string [ : indi ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = ' aabcdaab ' NEW_LINE MaxPrefix ( str ) NEW_LINE DEDENT
Count of Substrings that can be formed without using the given list of Characters | Function to find the Number of sub - strings without using given character ; the freq array ; Count variable to store the count of the characters until a character from given L is encountered ; If a character from L is encountered , then the answer variable is incremented by the value obtained by using the mentioned formula and count is set to 0 ; For last remaining characters ; Driver code
def countSubstring ( S , L , n ) : NEW_LINE INDENT freq = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ( ord ( L [ i ] ) - ord ( ' a ' ) ) ] = 1 NEW_LINE DEDENT count , ans = 0 , 0 NEW_LINE for x in S : NEW_LINE INDENT if ( freq [ ord ( x ) - ord ( ' a ' ) ] ) : NEW_LINE INDENT ans += ( count * count + count ) // 2 NEW_LINE count = 0 NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT ans += ( count * count + count ) // 2 NEW_LINE return ans NEW_LINE DEDENT S = " abcpxyz " NEW_LINE L = [ ' a ' , ' p ' , ' q ' ] NEW_LINE n = len ( L ) NEW_LINE print ( countSubstring ( S , L , n ) ) NEW_LINE