codeparrot/xlcost-text-to-code huggingface dataset:
Lexicographic rank of a Binary String | Function to find the rank of a string ; Store the length of the string ; Stores its equivalent decimal value ; Traverse the string ; Store the number of strings of length less than N occurring before the given string ; Store the decimal equivalent number of string bin ; Store the rank in answer ; Prthe answer ; Driver Code
def findRank ( s ) : NEW_LINE INDENT N = len ( s ) ; NEW_LINE sb = " " ; NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT sb += str ( '0' ) ; NEW_LINE DEDENT else : NEW_LINE INDENT sb += str ( '1' ) ; NEW_LINE DEDENT DEDENT bin = str ( sb ) ; NEW_LINE X = pow ( 2 , N ) ; NEW_LINE Y = int ( bin ) ; NEW_LINE ans = X + Y - 1 ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "001" ; NEW_LINE findRank ( S ) ; NEW_LINE DEDENT
Find the GCD of an array made up of numeric strings | Recursive function to return gcd of A and B ; Base case ; Length of A is greater ; Calculate GCD ; Store the GCD of the length of the strings ; Driver Code ; Function call
def GCD ( lena , lenb ) : NEW_LINE INDENT if ( lena == 0 ) : NEW_LINE INDENT return lenb NEW_LINE DEDENT if ( lenb == 0 ) : NEW_LINE INDENT return lena NEW_LINE DEDENT if ( lena == lenb ) : NEW_LINE INDENT return lena NEW_LINE DEDENT if ( lena > lenb ) : NEW_LINE INDENT return GCD ( lena - lenb , lenb ) NEW_LINE DEDENT return GCD ( lena , lenb - lena ) NEW_LINE DEDENT def StringGCD ( a , b ) : NEW_LINE INDENT gcd = GCD ( len ( a ) , len ( b ) ) NEW_LINE if a [ : gcd ] == b [ : gcd ] : NEW_LINE INDENT if a * ( len ( b ) // gcd ) == b * ( len ( a ) // gcd ) : NEW_LINE return a [ : gcd ] NEW_LINE DEDENT return - 1 NEW_LINE DEDENT a = ' geeksgeeks ' NEW_LINE b = ' geeks ' NEW_LINE print ( StringGCD ( a , b ) ) NEW_LINE
Minimize cost to rearrange substrings to convert a string to a Balanced Bracket Sequence | Function to count minimum number of operations to convert the string to a balanced bracket sequence ; Initialize the integer array ; Traverse the string ; Decrement a [ i ] ; Increment a [ i ] ; Update the sum as current value of arr [ i ] ; If answer exists ; Traverse from i ; Find all substrings with 0 sum ; Print the sum of sizes ; No answer exists ; Driver Code
def countMinMoves ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE a = [ 0 for i in range ( n ) ] NEW_LINE j , ans , sum = 0 , 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == ' ) ' ) : NEW_LINE INDENT a [ i ] += sum - 1 NEW_LINE DEDENT else : NEW_LINE INDENT a [ i ] += sum + 1 NEW_LINE DEDENT sum = a [ i ] NEW_LINE DEDENT if ( sum == 0 ) : NEW_LINE INDENT i = 1 NEW_LINE while ( i < n ) : NEW_LINE INDENT j = i - 1 NEW_LINE while ( i < n and a [ i ] != 0 ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if ( i < n and a [ i - 1 ] < 0 ) : NEW_LINE INDENT ans += i - j NEW_LINE if ( j == 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : str = " ) ( ( ) " NEW_LINE INDENT countMinMoves ( str ) NEW_LINE DEDENT
Check if a given string can be converted to a Balanced Bracket Sequence | Function to check if the can be balanced by replacing the ' $ ' with opening or closing brackets ; If string can never be balanced ; Declare 2 stacks to check if all ) can be balanced with ( or $ and vice - versa ; Store the count the occurence of ( , ) and $ ; Traverse the string ; Increment closed bracket count by 1 ; If there are no opening bracket to match it then return False ; Otherwise , pop the character from the stack ; If current character is an opening bracket or $ , push it to the stack ; Increment symbol count by 1 ; Increment open bracket count by 1 ; Traverse the string from end and repeat the same process ; If there are no closing brackets to match it ; Otherwise , pop character from stack ; Store the extra ( or ) which are not balanced yet ; Check if $ is available to balance the extra brackets ; Count ramaining $ after balancing extra ( and ) ; Check if each pair of $ is convertable in ( ) ; Driver Code ; Function Call
def canBeBalanced ( sequence ) : NEW_LINE INDENT if ( len ( sequence ) % 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT stack_ , stack2_ = [ ] , [ ] NEW_LINE countOpen , countClosed = 0 , 0 NEW_LINE countSymbol = 0 NEW_LINE for i in range ( len ( sequence ) ) : NEW_LINE INDENT if ( sequence [ i ] == ' ) ' ) : NEW_LINE INDENT countClosed += 1 NEW_LINE if ( len ( stack_ ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT del stack_ [ - 1 ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( sequence [ i ] == ' $ ' ) : NEW_LINE INDENT countSymbol += 1 NEW_LINE DEDENT else : NEW_LINE INDENT countOpen += 1 NEW_LINE DEDENT stack_ . append ( sequence [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( sequence ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( sequence [ i ] == ' ( ' ) : NEW_LINE INDENT if ( len ( stack2_ ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT del stack2_ [ - 1 ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT stack2_ . append ( sequence [ i ] ) NEW_LINE DEDENT DEDENT extra = abs ( countClosed - countOpen ) NEW_LINE if ( countSymbol < extra ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT countSymbol -= extra NEW_LINE if ( countSymbol % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " ( ) ( $ " NEW_LINE if ( canBeBalanced ( S ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Queries to calculate difference between the frequencies of the most and least occurring characters in specified substring | Function to find difference between maximum and minimum frequency of a character in given range ; Stores length of string ; Stores count of queries ; Iterate over the characters of the string ; Stores l - value of a query ; Stores r - value of a query ; Store count of every character laying in range [ l , r ] ; Update frequency of current character ; Stores maximum frequency of characters in given range ; Stores minimum frequency of characters in given range ; Iterate over all possible characters of the given string ; Update mx ; If ( j + ' a ' ) is present ; Difference between max and min ; Driver Code ; Given string ; Given queries ; Function Call
def maxDiffFreq ( queries , S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE Q = len ( queries ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT l = queries [ i ] [ 0 ] - 1 NEW_LINE r = queries [ i ] [ 1 ] - 1 NEW_LINE freq = [ 0 ] * 26 NEW_LINE for j in range ( l , r + 1 ) : NEW_LINE INDENT freq [ ord ( S [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT mx = 0 NEW_LINE mn = 99999999 NEW_LINE for j in range ( 26 ) : NEW_LINE INDENT mx = max ( mx , freq [ j ] ) NEW_LINE if ( freq [ j ] ) : NEW_LINE INDENT mn = min ( mn , freq [ j ] ) NEW_LINE DEDENT DEDENT print ( mx - mn ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " abaabac " NEW_LINE queries = [ [ 2 , 6 ] , [ 1 , 7 ] ] NEW_LINE maxDiffFreq ( queries , S ) NEW_LINE DEDENT
Count strings from given array having all characters appearing in a given string | Function to count the number of strings from an array having all characters appearing in the string S ; Initialize a set to store all distinct characters of S ; Traverse over S ; Insert characters into the Set ; Stores the required count ; Traverse the array ; Traverse over arr [ i ] ; Check if character in arr [ i ] [ j ] is present in the S or not ; Increment the count if all the characters of arr [ i ] are present in the S ; Finally , prthe count ; Driver code
def countStrings ( S , list ) : NEW_LINE INDENT valid = { } NEW_LINE for x in S : NEW_LINE INDENT valid [ x ] = 1 NEW_LINE DEDENT cnt = 0 NEW_LINE for i in range ( len ( list ) ) : NEW_LINE INDENT j = 0 NEW_LINE while j < len ( list [ i ] ) : NEW_LINE INDENT if ( list [ i ] [ j ] in valid ) : NEW_LINE INDENT j += 1 NEW_LINE continue NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( j == len ( list [ i ] ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " ab " , " aab " , " abaaaa " , " bbd " ] NEW_LINE S , l = " ab " , [ ] NEW_LINE print ( countStrings ( S , arr ) ) NEW_LINE DEDENT
Minimize a string by removing all occurrences of another string | Function to find the minimum length to which string str can be reduced to by removing all occurrences of string K ; Initialize stack of characters ; Push character into the stack ; If stack size >= K . size ( ) ; Create empty string to store characters of stack ; Traverse the string K in reverse ; If any of the characters differ , it means that K is not present in the stack ; Push the elements back into the stack ; Store the string ; Remove top element ; Size of stack gives the minimized length of str ; Driver code ; Function Call
def minLength ( Str , N , K , M ) : NEW_LINE INDENT stackOfChar = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT stackOfChar . append ( Str [ i ] ) NEW_LINE if ( len ( stackOfChar ) >= M ) : NEW_LINE INDENT l = " " NEW_LINE for j in range ( M - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( K [ j ] != stackOfChar [ - 1 ] ) : NEW_LINE INDENT f = 0 NEW_LINE while ( f != len ( l ) ) : NEW_LINE INDENT stackOfChar . append ( l [ f ] ) NEW_LINE f += 1 NEW_LINE DEDENT break NEW_LINE DEDENT else : NEW_LINE INDENT l = stackOfChar [ - 1 ] + l NEW_LINE stackOfChar . pop ( ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return len ( stackOfChar ) NEW_LINE DEDENT S1 = " fffoxoxoxfxo " NEW_LINE S2 = " fox " NEW_LINE N = len ( S1 ) NEW_LINE M = len ( S2 ) NEW_LINE print ( minLength ( S1 , N , S2 , M ) ) NEW_LINE
Largest palindrome not exceeding N which can be expressed as product of two 3 | Function to find the largest palindrome not exceeding N which can be expressed as the product of two 3 - digit numbers ; Stores all palindromes ; Stores the product ; Check if X is palindrome ; Check n is less than N ; If true , append it in the list ; Print the largest palindrome ; Driver Code ; Function Call
def palindrome_prod ( N ) : NEW_LINE INDENT palindrome_list = [ ] NEW_LINE for i in range ( 101 , 1000 ) : NEW_LINE INDENT for j in range ( 121 , 1000 , ( 1 if i % 11 == 0 else 11 ) ) : NEW_LINE INDENT n = i * j NEW_LINE x = str ( n ) NEW_LINE if x == x [ : : - 1 ] : NEW_LINE INDENT if n < N : NEW_LINE INDENT palindrome_list . append ( i * j ) NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( max ( palindrome_list ) ) NEW_LINE DEDENT N = 101110 NEW_LINE palindrome_prod ( N ) NEW_LINE
Check if given strings can be made same by swapping two characters of same or different strings | Function to check if all strings can be made equal by swapping any pair of characters from the same or different strings ; Stores length of string ; Store frequency of each distinct character of the strings ; Traverse the array ; Iterate over the characters ; Update frequency of arr [ i ] [ j ] ; Traverse the array cntFreq [ ] ; If cntFreq [ i ] is divisible by N or not ; Driver Code ; Function Call
def isEqualStrings ( arr , N ) : NEW_LINE INDENT M = len ( arr [ 0 ] ) NEW_LINE cntFreq = [ 0 ] * 256 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT cntFreq [ ord ( arr [ i ] [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT for i in range ( 256 ) : NEW_LINE INDENT if ( cntFreq [ i ] % N != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " aab " , " bbc " , " cca " ] NEW_LINE N = len ( arr ) NEW_LINE if isEqualStrings ( arr , N ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Minimize a binary string by repeatedly removing even length substrings of same characters | Recursive function to print stack elements from bottom to top without changing their order ; If stack is empty ; Pop top element of the stack ; Recursively call the function PrintStack ; Print the stack element from the bottom ; Push the same element onto the stack to preserve the order ; Function to minimize binary string by removing substrings consisting of same character ; Declare a stack of characters ; Push the first character of the string into the stack ; Traverse the string s ; If Stack is empty ; Push current character into the stack ; Check if the current character is same as the top of the stack ; If true , pop the top of the stack ; Otherwise , push the current element ; Print stack from bottom to top ; Driver Code
def PrintStack ( s ) : NEW_LINE INDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT x = s [ - 1 ] ; NEW_LINE s . pop ( ) ; NEW_LINE PrintStack ( s ) ; NEW_LINE print ( x , end = " " ) ; NEW_LINE s . append ( x ) ; NEW_LINE DEDENT def minString ( s ) : NEW_LINE INDENT Stack = [ ] ; NEW_LINE Stack . append ( s [ 0 ] ) ; NEW_LINE for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT if ( len ( Stack ) == 0 ) : NEW_LINE INDENT Stack . append ( s [ i ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( Stack [ - 1 ] == s [ i ] ) : NEW_LINE INDENT Stack . pop ( ) ; NEW_LINE DEDENT else : NEW_LINE INDENT Stack . append ( s [ i ] ) ; NEW_LINE DEDENT DEDENT DEDENT PrintStack ( Stack ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "101001" ; NEW_LINE minString ( string ) ; NEW_LINE DEDENT
Probability of collision between two trucks | Function to calculate total number of accidents ; Function to calculate count of all possible collision ; Size of string ; Stores the count of collisions ; Total number of truck in lane b ; Count total number of collisions while traversing the string a ; Function to calculate the probability of collisions ; Evaluate total outcome that is all the possible accident ; Evaluate favourable outcome i . e . , count of collision of trucks ; Print desired probability ; Driver Code ; Function Call
def count_of_accident ( a , b ) : NEW_LINE INDENT n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE if ( n > m ) : NEW_LINE INDENT return ( m * ( m + 1 ) ) / 2 NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( n * ( n + 1 ) ) / 2 + ( m - n ) * n ) NEW_LINE DEDENT DEDENT def count_of_collision ( a , b ) : NEW_LINE INDENT n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE answer = 0 NEW_LINE count_of_truck_in_lane_b = 0 NEW_LINE for i in range ( 0 , m ) : NEW_LINE INDENT if ( b [ i ] == ' T ' ) : NEW_LINE INDENT count_of_truck_in_lane_b += 1 NEW_LINE DEDENT DEDENT i = 0 NEW_LINE while ( i < m and i < n ) : NEW_LINE INDENT if ( a [ i ] == ' T ' ) : NEW_LINE INDENT answer += count_of_truck_in_lane_b NEW_LINE DEDENT if ( b [ i ] == ' T ' ) : NEW_LINE INDENT count_of_truck_in_lane_b -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return answer NEW_LINE DEDENT def findProbability ( a , b ) : NEW_LINE INDENT total_outcome = count_of_accident ( a , b ) ; NEW_LINE favourable_outcome = count_of_collision ( a , b ) ; NEW_LINE print ( favourable_outcome / total_outcome ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " TCCBCTTB " NEW_LINE T = " BTCCBBTT " NEW_LINE findProbability ( S , T ) NEW_LINE DEDENT
Count alphanumeric palindromes of length N | Function to calculate ( x ^ y ) mod p ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Return the final result ; Given N ; Base Case ; Check whether n is even or odd ; Function Call ; Function Call
def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( y > 0 ) : NEW_LINE INDENT if ( ( y & 1 ) == 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT N = 3 NEW_LINE if ( ( N == 1 ) or ( N == 2 ) ) : NEW_LINE INDENT print ( 62 ) NEW_LINE DEDENT else : NEW_LINE INDENT m = ( 10 ** 9 ) + 7 NEW_LINE if ( N % 2 == 0 ) : NEW_LINE INDENT k = N // 2 NEW_LINE flag = True NEW_LINE DEDENT else : NEW_LINE INDENT k = ( N - 1 ) // 2 NEW_LINE flag = False NEW_LINE DEDENT if ( flag ) : NEW_LINE INDENT a = power ( 62 , k , m ) NEW_LINE print ( a ) NEW_LINE DEDENT else : NEW_LINE INDENT a = power ( 62 , ( k + 1 ) , m ) NEW_LINE print ( a ) NEW_LINE DEDENT DEDENT
Length of the longest substring with every character appearing even number of times | Function to find length of the longest substring with each element occurring even number of times ; Initialize unordered_map ; Stores the length of the longest required substring ; Traverse the string ; Stores the value of the digit present at current index ; Bitwise XOR of the mask with 1 left - shifted by val ; Check if the value of mask is already present in ind or not ; Update the final answer ; Otherwise ; Return the answer ; Driver Code ; Given string ; : Length of the given string ; Function Call
def lenOfLongestReqSubstr ( s , N ) : NEW_LINE INDENT ind = { } NEW_LINE mask = 0 NEW_LINE ind [ 0 ] = - 1 NEW_LINE ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT val = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE mask ^= ( 1 << val ) NEW_LINE if ( mask in ind ) : NEW_LINE INDENT ans = max ( ans , i - ind [ mask ] ) NEW_LINE DEDENT else : NEW_LINE INDENT ind [ mask ] = i NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "223015150" NEW_LINE N = len ( s ) NEW_LINE print ( lenOfLongestReqSubstr ( s , N ) ) NEW_LINE DEDENT
Count distinct strings possible by replacing each character by its Morse code | Function to count unique array elements by replacing each character by its Morse code ; Stores Morse code of all lowercase characters ; Stores distinct elements of String by replacing each character by Morse code ; Stores length of arr array ; Traverse the array ; Stores the Morse code of arr [ i ] ; Stores length of current String ; Update temp ; Insert temp into st ; Return count of elements in the set ; Driver code
def uniqueMorseRep ( arr ) : NEW_LINE INDENT morseCode = [ " . - " , " - . . . " , " - . - . " , " - . . " , " . " , " . . - . " , " - - . " , " . . . . " , " . . " , " . - - - " , " - . - " , " . - . . " , " - - " , " - . " , " - - - " , " . - - . " , " - - . - " , " . - . " , " . . . " , " - " , " . . - " , " . . . - " , " . - - " , " - . . - " , " - . - - " , " - - . . " ] ; NEW_LINE st = set ( ) ; NEW_LINE N = len ( arr ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT temp = " " ; NEW_LINE M = len ( arr [ i ] ) ; NEW_LINE for j in range ( M ) : NEW_LINE INDENT temp += morseCode [ ord ( arr [ i ] [ j ] ) - ord ( ' a ' ) ] ; NEW_LINE DEDENT st . add ( temp ) ; NEW_LINE DEDENT return len ( st ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " gig " , " zeg " , " gin " , " msn " ] ; NEW_LINE print ( uniqueMorseRep ( arr ) , " " ) ; NEW_LINE DEDENT
Maximum repeating character for every index in given String | Function to print the maximum repeating character at each index of the string ; Stores frequency of each distinct character ; Stores frequency of maximum repeating character ; Stores the character having maximum frequency ; Traverse the string ; Stores current character ; Update the frequency of strr [ i ] ; If frequency of current character exceeds max ; Update max ; Update charMax ; Print the required output ; Driver Code ; Stores length of strr
def findFreq ( strr , N ) : NEW_LINE INDENT freq = [ 0 ] * 256 NEW_LINE max = 0 NEW_LINE charMax = '0' NEW_LINE for i in range ( N ) : NEW_LINE INDENT ch = ord ( strr [ i ] ) NEW_LINE freq [ ch ] += 1 NEW_LINE if ( freq [ ch ] >= max ) : NEW_LINE INDENT max = freq [ ch ] NEW_LINE charMax = ch NEW_LINE DEDENT print ( chr ( charMax ) , " - > " , max ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " abbc " NEW_LINE N = len ( strr ) NEW_LINE findFreq ( strr , N ) NEW_LINE DEDENT
Minimize cost to convert given string to a palindrome | Function to find the minimum cost to convert the into a palindrome ; Length of the string ; If iointer is in the second half ; Reverse the string ; Find the farthest index needed to change on left side ; Find the farthest index needed to change on right side ; Changing the variable to make palindrome ; min distance to travel to make palindrome ; Total cost ; Return the minimum cost ; Driver Code ; Given S ; Given pointer P ; Function Call
def findMinCost ( strr , pos ) : NEW_LINE INDENT n = len ( strr ) NEW_LINE if ( pos >= n / 2 ) : NEW_LINE INDENT strr = strr [ : : - 1 ] NEW_LINE pos = n - pos - 1 NEW_LINE DEDENT left , right = pos , pos NEW_LINE for i in range ( pos , - 1 , - 1 ) : NEW_LINE INDENT if ( strr [ i ] != strr [ n - i - 1 ] ) : NEW_LINE INDENT left = i NEW_LINE DEDENT DEDENT for i in range ( pos , n // 2 ) : NEW_LINE INDENT if ( strr [ i ] != strr [ n - i - 1 ] ) : NEW_LINE INDENT right = i NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( left , right + 1 ) : NEW_LINE INDENT if ( strr [ i ] != strr [ n - i - 1 ] ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT dis = ( min ( ( 2 * ( pos - left ) + ( right - pos ) ) , ( 2 * ( right - pos ) + ( pos - left ) ) ) ) NEW_LINE ans = ans + dis NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " bass " NEW_LINE P = 3 NEW_LINE print ( findMinCost ( S , P ) ) NEW_LINE DEDENT
Minimize replacements by previous or next alphabet required to make all characters of a string the same | Function to find the minimum count of operations to make all characters of the string same ; Set min to some large value ; Find minimum operations for each character ; Initialize cnt ; Add the value to cnt ; Update minValue ; Return minValue ; Driver Code ; Given string str ; Function Call
def minCost ( s , n ) : NEW_LINE INDENT minValue = 100000000 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT cnt = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT cnt += min ( abs ( i - ( ord ( s [ j ] ) - ord ( ' a ' ) ) ) , 26 - abs ( i - ( ord ( s [ j ] ) - ord ( ' a ' ) ) ) ) NEW_LINE DEDENT minValue = min ( minValue , cnt ) NEW_LINE DEDENT return minValue NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " geeksforgeeks " NEW_LINE N = len ( st ) NEW_LINE print ( minCost ( st , N ) ) NEW_LINE DEDENT
Longest palindromic string possible by concatenating strings from a given array | Python3 program for the above approach ; Stores the distinct strings from the given array ; Insert the strings into set ; Stores the left and right substrings of the given string ; Stores the middle substring ; Traverse the array of strings ; Reverse the current string ; Checking if the is itself a palindrome or not ; Check if the reverse of the is present or not ; Append to the left substring ; Append to the right substring ; Erase both the strings from the set ; Print the left substring ; Print the middle substring ; Print the right substring ; Driver Code ; Function call
def max_len ( s , N , M ) : NEW_LINE INDENT set_str = { } NEW_LINE for i in s : NEW_LINE INDENT set_str [ i ] = 1 NEW_LINE DEDENT left_ans , right_ans = [ ] , [ ] 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 set_str ) : NEW_LINE INDENT left_ans . append ( s [ i ] ) NEW_LINE right_ans . append ( t ) NEW_LINE del set_str [ s [ i ] ] NEW_LINE del set_str [ t ] NEW_LINE DEDENT DEDENT for x in left_ans : NEW_LINE INDENT print ( x , end = " " ) NEW_LINE DEDENT print ( mid , end = " " ) NEW_LINE right_ans = right_ans [ : : - 1 ] NEW_LINE for x in right_ans : NEW_LINE INDENT print ( x , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE M = 3 NEW_LINE s = [ " omg " , " bbb " , " ffd " , " gmo " ] NEW_LINE max_len ( s , N , M ) NEW_LINE DEDENT