codeparrot/xlcost-text-to-code huggingface dataset:
Maximum length of balanced string after swapping and removal of characters | Function to return the length of the longest balanced sub - string ; To store the count of parentheses ; Traversing the string ; Check type of parentheses and incrementing count for it ; Sum all pair of balanced parentheses ; Driven code
def maxBalancedStr ( s ) : NEW_LINE INDENT open1 = 0 NEW_LINE close1 = 0 NEW_LINE open2 = 0 NEW_LINE close2 = 0 NEW_LINE open3 = 0 NEW_LINE close3 = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == ' ( ' ) : open1 += 1 continue if s [ i ] == ' ) ' : NEW_LINE INDENT close1 += 1 NEW_LINE continue NEW_LINE DEDENT if s [ i ] == ' { ' : NEW_LINE INDENT open2 += 1 NEW_LINE continue NEW_LINE DEDENT if s [ i ] == ' } ' : NEW_LINE INDENT close2 += 1 NEW_LINE continue NEW_LINE DEDENT if s [ i ] == ' [ ' : NEW_LINE INDENT open3 += 1 NEW_LINE continue NEW_LINE DEDENT if s [ i ] == ' ] ' : NEW_LINE INDENT close3 += 1 NEW_LINE continue NEW_LINE DEDENT DEDENT maxLen = ( 2 * min ( open1 , close1 ) + 2 * min ( open2 , close2 ) + 2 * min ( open3 , close3 ) ) NEW_LINE return maxLen NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : s = " ) ) [ ] ] ( ( " NEW_LINE INDENT print ( maxBalancedStr ( s ) ) NEW_LINE DEDENT
Bitwise OR of N binary strings | Function to return the bitwise OR of all the binary strings ; Get max size and reverse each string Since we have to perform OR operation on bits from right to left Reversing the string will make it easier to perform operation from left to right ; Add 0 s to the end of strings if needed ; Perform OR operation on each bit ; Reverse the resultant string to get the final string ; Return the final string ; Driver code
def strBitwiseOR ( arr , n ) : NEW_LINE INDENT res = " " NEW_LINE max_size = - ( 2 ** 32 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT max_size = max ( max_size , len ( arr [ i ] ) ) NEW_LINE arr [ i ] = arr [ i ] [ : : - 1 ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT s = " " NEW_LINE for j in range ( max_size - len ( arr [ i ] ) ) : NEW_LINE INDENT s += '0' NEW_LINE DEDENT arr [ i ] = arr [ i ] + s NEW_LINE DEDENT for i in range ( max_size ) : NEW_LINE INDENT curr_bit = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT curr_bit = curr_bit | ord ( arr [ j ] [ i ] ) NEW_LINE DEDENT res += chr ( curr_bit ) NEW_LINE DEDENT res = res [ : : - 1 ] NEW_LINE return res NEW_LINE DEDENT arr = [ "10" , "11" , "1000001" ] NEW_LINE n = len ( arr ) NEW_LINE print ( strBitwiseOR ( arr , n ) ) NEW_LINE
Minimum cost to make two strings same | Function to return the minimum cost to make the configuration of both the strings same ; Iterate and find the cost ; Find the minimum cost ; Driver Code
def findCost ( s1 , s2 , a , b , c , d , n ) : NEW_LINE INDENT cost = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s1 [ i ] == s2 [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT if ( ( s1 [ i ] == '1' and s2 [ i ] == '2' ) or ( s2 [ i ] == '1' and s1 [ i ] == '2' ) ) : NEW_LINE INDENT cost += min ( d , min ( a , b + c ) ) NEW_LINE DEDENT elif ( ( s1 [ i ] == '2' and s2 [ i ] == '3' ) or ( s2 [ i ] == '2' and s1 [ i ] == '3' ) ) : NEW_LINE INDENT cost += min ( d , min ( b , a + c ) ) NEW_LINE DEDENT elif ( ( s1 [ i ] == '1' and s2 [ i ] == '3' ) or ( s2 [ i ] == '1' and s1 [ i ] == '3' ) ) : NEW_LINE INDENT cost += min ( d , min ( c , a + b ) ) NEW_LINE DEDENT DEDENT DEDENT return cost NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = "121" NEW_LINE s2 = "223" NEW_LINE a = 2 NEW_LINE b = 3 NEW_LINE c = 4 NEW_LINE d = 10 NEW_LINE n = len ( s1 ) NEW_LINE print ( findCost ( s1 , s2 , a , b , c , d , n ) ) NEW_LINE DEDENT
Count the number of common divisors of the given strings | Function that returns true if sub - string s [ 0. . . k ] is repeated a number of times to generate String s ; Function to return the count of common divisors ; If the length of the sub - string divides length of both the strings ; If prefixes match in both the strings ; If both the strings can be generated ; Driver code
def check ( s , k ) : NEW_LINE INDENT for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT if ( s [ i ] != s [ i % k ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def countCommonDivisors ( a , b ) : NEW_LINE INDENT ct = 0 NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE for i in range ( 1 , min ( n , m ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 and m % i == 0 ) : NEW_LINE INDENT if ( a [ 0 : i ] == b [ 0 : i ] ) : NEW_LINE INDENT if ( check ( a , i ) and check ( b , i ) ) : NEW_LINE INDENT ct = ct + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return ct NEW_LINE DEDENT a = " xaxa " NEW_LINE b = " xaxaxaxa " NEW_LINE print ( countCommonDivisors ( a , b ) ) NEW_LINE
Level order traversal line by line | Set 3 ( Using One Queue ) | Python3 program to print levels line by line ; A Binary Tree Node ; Function to do level order traversal line by line ; Create an empty queue for level order traversal ; Pushing root node into the queue . ; Pushing delimiter into the queue . ; Condition to check occurrence of next level . ; Pushing left child of current node . ; Pushing left child current node ; Pushing right child of current node . ; Driver code ; Let us create binary tree shown above
from collections import deque as queue NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def levelOrder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT q = queue ( ) NEW_LINE q . append ( root ) NEW_LINE q . append ( None ) NEW_LINE while ( len ( q ) > 1 ) : NEW_LINE INDENT curr = q . popleft ( ) NEW_LINE if ( curr == None ) : NEW_LINE q . append ( None ) NEW_LINE print ( ) NEW_LINE else : NEW_LINE INDENT if ( curr . left ) : NEW_LINE INDENT q . append ( curr . left ) NEW_LINE DEDENT if ( curr . right ) : NEW_LINE INDENT q . append ( curr . right ) NEW_LINE DEDENT print ( curr . data , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . right = Node ( 6 ) NEW_LINE levelOrder ( root ) NEW_LINE DEDENT
Convert given string so that it holds only distinct characters | Function to return the index of the character that has 0 occurrence starting from index i ; If current character has 0 occurrence ; If no character has 0 occurrence ; Function to return the modified string which consists of distinct characters ; String cannot consist of all distinct characters ; Count the occurrences for each of the character ; Index for the first character that hasn 't appeared in the string ; If current character appeared more than once then it has to be replaced with some character that hasn 't occurred yet ; Decrement current character 's occurrence by 1 ; Replace the character ; Update the new character ' s ▁ occurrence ▁ ▁ This ▁ step ▁ can ▁ also ▁ be ▁ skipped ▁ as ▁ ▁ we ' ll never encounter this character in the string because it has been added just now ; Find the next character that hasn 't occurred yet ; Driver code
def nextZero ( i , occurrences ) : NEW_LINE INDENT while i < 26 : NEW_LINE INDENT if occurrences [ i ] == 0 : NEW_LINE INDENT return i NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT def getModifiedString ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE if n > 26 : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT ch = str NEW_LINE ch = list ( ch ) NEW_LINE occurrences = [ 0 ] * 26 NEW_LINE for i in range ( n ) : NEW_LINE INDENT occurrences [ ord ( ch [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT index = nextZero ( 0 , occurrences ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if occurrences [ ord ( ch [ i ] ) - ord ( ' a ' ) ] > 1 : NEW_LINE INDENT occurrences [ ord ( ch [ i ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE ch [ i ] = chr ( ord ( ' a ' ) + index ) NEW_LINE occurrences [ index ] = 1 NEW_LINE index = nextZero ( index + 1 , occurrences ) NEW_LINE DEDENT DEDENT ch = ' ' . join ( ch ) NEW_LINE print ( ch ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeksforgeeks " NEW_LINE getModifiedString ( str ) NEW_LINE DEDENT
Replace all occurrences of a string with space | Function to extract the secret message ; Replacing all occurrences of Sub in Str by empty spaces ; Removing unwanted spaces in the start and end of the string ; Driver code
def extractSecretMessage ( Str , Sub ) : NEW_LINE INDENT Str = Str . replace ( Sub , " ▁ " ) NEW_LINE return Str . strip ( ) NEW_LINE DEDENT Str = " LIELIEILIEAMLIECOOL " NEW_LINE Sub = " LIE " NEW_LINE print ( extractSecretMessage ( Str , Sub ) ) NEW_LINE
Sub | Function to return the count of sub - strings starting from startIndex that are also the prefixes of string ; Function to return the count of all possible sub - strings of string that are also the prefixes of string ; If current character is equal to the starting character of str ; Driver Code ; Function Call
def subStringsStartingHere ( string , n , startIndex ) : NEW_LINE INDENT count = 0 NEW_LINE i = startIndex + 1 NEW_LINE while ( i <= n ) : NEW_LINE INDENT if string . startswith ( string [ startIndex : i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def countSubStrings ( string , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if string [ i ] == string [ 0 ] : NEW_LINE INDENT count += subStringsStartingHere ( string , n , i ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " ababc " NEW_LINE n = len ( string ) NEW_LINE print ( countSubStrings ( string , n ) ) NEW_LINE DEDENT
Binary Search a String | Returns index of x if it is present in arr [ ] , else return - 1 ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; Driver Code
def binarySearch ( arr , x ) : NEW_LINE INDENT l = 0 NEW_LINE r = len ( arr ) NEW_LINE while ( l <= r ) : NEW_LINE INDENT m = l + ( ( r - l ) // 2 ) NEW_LINE res = ( x == arr [ m ] ) NEW_LINE if ( res == 0 ) : NEW_LINE INDENT return m - 1 NEW_LINE DEDENT if ( res > 0 ) : NEW_LINE INDENT l = m + 1 NEW_LINE DEDENT else : NEW_LINE INDENT r = m - 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " contribute " , " geeks " , " ide " , " practice " ] ; NEW_LINE x = " ide " NEW_LINE result = binarySearch ( arr , x ) NEW_LINE if ( result == - 1 ) : NEW_LINE INDENT print ( " Element ▁ not ▁ present " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Element ▁ found ▁ at ▁ index " , result ) NEW_LINE DEDENT DEDENT
Count characters in a string whose ASCII values are prime | Python3 implementation of above approach ; Function to find prime characters in the string ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a Boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; 0 and 1 are not primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Traverse all the characters ; Driver program ; print required answer
from math import sqrt NEW_LINE max_val = 257 NEW_LINE def PrimeCharacters ( s ) : NEW_LINE INDENT prime = [ True ] * ( max_val + 1 ) NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( sqrt ( max_val ) ) + 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( 2 * p , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT count = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( prime [ ord ( s [ i ] ) ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geeksforgeeks " ; NEW_LINE print ( PrimeCharacters ( S ) ) NEW_LINE DEDENT
Students with maximum average score of three subjects | Function to find the list of students having maximum average score ; Variables to store maximum average score ; List to store names of students having maximum average score ; Traversing the file data ; finding average score of a student ; Clear the list and add name of student having current maximum average score in the list ; Printing the maximum average score and names of students having this maximum average score as per the order in the file . ; Driver Code ; Number of elements in string array
def getStudentsList ( file ) : NEW_LINE INDENT maxAvgScore = 0 NEW_LINE names = [ ] NEW_LINE for i in range ( 0 , len ( file ) , 4 ) : NEW_LINE INDENT avgScore = ( int ( file [ i + 1 ] ) + int ( file [ i + 2 ] ) + int ( file [ i + 3 ] ) ) // 3 NEW_LINE if avgScore > maxAvgScore : NEW_LINE INDENT maxAvgScore = avgScore NEW_LINE names . clear ( ) NEW_LINE names . append ( file [ i ] ) NEW_LINE DEDENT elif avgScore == maxAvgScore : NEW_LINE INDENT names . add ( file [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( names ) ) : NEW_LINE INDENT print ( names [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( maxAvgScore ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT file = [ " Shrikanth " , "20" , "30" , "10" , " Ram " , "100" , "50" , "10" ] NEW_LINE getStudentsList ( file ) NEW_LINE DEDENT
Number of sub | Python3 program to find the number of sub - strings of s1 which are anagram of any sub - string of s2 ; This function returns true if contents of arr1 [ ] and arr2 [ ] are same , otherwise false . ; This function search for all permutations of string pat [ ] in string txt [ ] ; countP [ ] : Store count of all characters of pattern countTW [ ] : Store count of current window of text ; Traverse through remaining characters of pattern ; Compare counts of current window of text with counts of pattern [ ] ; cout << pat << " ▁ " << txt << " ▁ " ; ; Add current character to current window ; Remove the first character of previous window ; Check for the last window in text ; Function to return the number of sub - strings of s1 that are anagrams of any sub - string of s2 ; initializing variables ; outer loop for picking starting point ; loop for different length of substrings ; If s2 has any substring which is anagram of s1 . substr ( i , len ) ; increment the count ; Driver Code
ALL_CHARS = 256 NEW_LINE def compare ( arr1 , arr2 ) : NEW_LINE INDENT for i in range ( ALL_CHARS ) : NEW_LINE INDENT if arr1 [ i ] != arr2 [ i ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def search ( pat , txt ) : NEW_LINE INDENT M = len ( pat ) NEW_LINE N = len ( txt ) NEW_LINE countP = [ 0 ] * ALL_CHARS NEW_LINE countTW = [ 0 ] * ALL_CHARS NEW_LINE for i in range ( M ) : NEW_LINE INDENT countP [ ord ( pat [ i ] ) ] += 1 NEW_LINE countTW [ ord ( txt [ i ] ) ] += 1 NEW_LINE DEDENT for i in range ( M , N ) : NEW_LINE INDENT if compare ( countP , countTW ) : NEW_LINE INDENT return True NEW_LINE DEDENT countTW [ ord ( txt [ i ] ) ] += 1 NEW_LINE countTW [ ord ( txt [ i - M ] ) ] -= 1 NEW_LINE DEDENT if compare ( countP , countTW ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def calculateSubString ( s1 , s2 , n ) : NEW_LINE INDENT count , j , x = 0 , 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for length in range ( 1 , n - i + 1 ) : NEW_LINE INDENT if search ( s1 [ i : i + length ] , s2 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " PLEASEHELPIMTRAPPED " NEW_LINE str2 = " INAKICKSTARTFACTORY " NEW_LINE length = len ( str1 ) NEW_LINE print ( calculateSubString ( str1 , str2 , length ) ) NEW_LINE DEDENT
Sum of the alphabetical values of the characters of a string | Function to find string score ; Driver code
def strScore ( str , s , n ) : NEW_LINE INDENT score = 0 NEW_LINE index = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == s ) : NEW_LINE INDENT for j in range ( len ( s ) ) : NEW_LINE INDENT score += ( ord ( s [ j ] ) - ord ( ' a ' ) + 1 ) NEW_LINE DEDENT index = i + 1 NEW_LINE break NEW_LINE DEDENT DEDENT score = score * index NEW_LINE return score NEW_LINE DEDENT str = [ " sahil " , " shashanak " , " sanjit " , " abhinav " , " mohit " ] NEW_LINE s = " abhinav " NEW_LINE n = len ( str ) NEW_LINE score = strScore ( str , s , n ) ; NEW_LINE print ( score ) NEW_LINE
Minimum swaps to group similar characters side by side ? | checks whether a string has similar characters side by side ; If similar chars side by side , continue ; If we have found a char equal to current char and does not exist side to it , return false ; counts min swap operations to convert a string that has similar characters side by side ; Base case ; considering swapping of i and l chars ; Backtrack ; not considering swapping of i and l chars ; taking min of above two ; Driver Code
from sys import maxsize NEW_LINE def sameCharAdj ( string ) : NEW_LINE INDENT n = len ( string ) NEW_LINE st = set ( ) NEW_LINE st . add ( string [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if string [ i ] == string [ i - 1 ] : NEW_LINE INDENT continue NEW_LINE DEDENT if string [ i ] in st : NEW_LINE INDENT return False NEW_LINE DEDENT st . add ( string [ i ] ) NEW_LINE DEDENT return True NEW_LINE DEDENT def minSwaps ( string , l , r , cnt , minm ) : NEW_LINE INDENT if l == r : NEW_LINE INDENT if sameCharAdj ( string ) : NEW_LINE INDENT return cnt NEW_LINE DEDENT else : NEW_LINE INDENT return maxsize NEW_LINE DEDENT DEDENT for i in range ( l + 1 , r + 1 , 1 ) : NEW_LINE INDENT string [ i ] , string [ l ] = string [ l ] , string [ i ] NEW_LINE cnt += 1 NEW_LINE x = minSwaps ( string , l + 1 , r , cnt , minm ) NEW_LINE string [ i ] , string [ l ] = string [ l ] , string [ i ] NEW_LINE cnt -= 1 NEW_LINE y = minSwaps ( string , l + 1 , r , cnt , minm ) NEW_LINE minm = min ( minm , min ( x , y ) ) NEW_LINE DEDENT return minm NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " abbaacb " NEW_LINE string = list ( string ) NEW_LINE n = len ( string ) NEW_LINE cnt = 0 NEW_LINE minm = maxsize NEW_LINE print ( minSwaps ( string , 0 , n - 1 , cnt , minm ) ) NEW_LINE DEDENT
Union | Naive implementation of find ; Naive implementation of union ( )
def find ( parent , i ) : NEW_LINE INDENT if ( parent [ i ] == - 1 ) : NEW_LINE INDENT return i NEW_LINE DEDENT return find ( parent , parent [ i ] ) NEW_LINE DEDENT def Union ( parent , x , y ) : NEW_LINE INDENT xset = find ( parent , x ) NEW_LINE yset = find ( parent , y ) NEW_LINE parent [ xset ] = yset NEW_LINE DEDENT
Get K | Function to return the K - th letter from new String . ; finding size = length of new string S ; get the K - th letter ; Driver Code
def K_thletter ( S , K ) : NEW_LINE INDENT N = len ( S ) NEW_LINE size = 0 NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT if S [ i ] . isdigit ( ) : NEW_LINE INDENT size = size * int ( S [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT size += 1 NEW_LINE DEDENT DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT K %= size NEW_LINE if K == 0 and S [ i ] . isalpha ( ) : NEW_LINE INDENT return S [ i ] NEW_LINE DEDENT if S [ i ] . isdigit ( ) : NEW_LINE INDENT size = size // int ( S [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT size -= 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geeks2for2" NEW_LINE K = 15 NEW_LINE print ( K_thletter ( S , K ) ) NEW_LINE DEDENT
Minimum number of Parentheses to be added to make it valid | Function to return required minimum number ; maintain balance of string ; It is guaranteed bal >= - 1 ; Driver code ; Function to print required answer
def minParentheses ( p ) : NEW_LINE INDENT bal = 0 NEW_LINE ans = 0 NEW_LINE for i in range ( 0 , len ( p ) ) : NEW_LINE INDENT if ( p [ i ] == ' ( ' ) : NEW_LINE INDENT bal += 1 NEW_LINE DEDENT else : NEW_LINE INDENT bal += - 1 NEW_LINE DEDENT if ( bal == - 1 ) : NEW_LINE INDENT ans += 1 NEW_LINE bal += 1 NEW_LINE DEDENT DEDENT return bal + ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT p = " ( ) ) " NEW_LINE print ( minParentheses ( p ) ) NEW_LINE DEDENT
Check if suffix and prefix of a string are palindromes | Function to check whether the string is a palindrome ; Reverse the string and assign it to new variable for comparison ; check if both are same ; Function to check whether the string has prefix and suffix substrings of length greater than 1 which are palindromes . ; check all prefix substrings ; check if the prefix substring is a palindrome ; If we did not find any palindrome prefix of length greater than 1. ; check all suffix substrings , as the string is reversed now ; check if the suffix substring is a palindrome ; If we did not find a suffix ; Driver code
def isPalindrome ( r ) : NEW_LINE INDENT p = r [ : : - 1 ] NEW_LINE return r == p NEW_LINE DEDENT def CheckStr ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE i = 0 NEW_LINE for i in range ( 2 , l + 1 ) : NEW_LINE INDENT if isPalindrome ( s [ 0 : i ] ) == True : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if i == ( l + 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , l + 1 ) : NEW_LINE INDENT if isPalindrome ( s [ l - i : l ] ) == True : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abccbarfgdbd " NEW_LINE if CheckStr ( s ) == True : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Rotations of a Binary String with Odd Value | function to calculate total odd equivalent ; Driver code
def oddEquivalent ( s , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "1011011" NEW_LINE n = len ( s ) NEW_LINE print ( oddEquivalent ( s , n ) ) NEW_LINE DEDENT
Minimum operation require to make first and last character same | Python3 program to find minimum operation require to make first and last character same ; Return the minimum operation require to make string first and last character same . ; Store indexes of first occurrences of characters . ; Initialize result ; Traverse through all characters ; Find first occurrence ; Update result for subsequent occurrences ; Driver Code
MAX = 256 NEW_LINE def minimumOperation ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE first_occ = [ - 1 ] * MAX NEW_LINE res = float ( ' inf ' ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT x = s [ i ] NEW_LINE if first_occ [ ord ( x ) ] == - 1 : NEW_LINE INDENT first_occ [ ord ( x ) ] = i NEW_LINE DEDENT else : NEW_LINE INDENT last_occ = n - i - 1 NEW_LINE res = min ( res , first_occ [ ord ( x ) ] + last_occ ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " bacdefghipalop " NEW_LINE print ( minimumOperation ( s ) ) NEW_LINE DEDENT
Lexicographically smallest and largest substring of size k | Python 3 program to find lexicographically largest and smallest substrings of size k . ; Initialize min and max as first substring of size k ; Consider all remaining substrings . We consider every substring ending with index i . ; Print result . ; Driver Code
def getSmallestAndLargest ( s , k ) : NEW_LINE INDENT currStr = s [ : k ] NEW_LINE lexMin = currStr NEW_LINE lexMax = currStr NEW_LINE for i in range ( k , len ( s ) ) : NEW_LINE INDENT currStr = currStr [ 1 : k ] + s [ i ] NEW_LINE if ( lexMax < currStr ) : NEW_LINE INDENT lexMax = currStr NEW_LINE DEDENT if ( lexMin > currStr ) : NEW_LINE INDENT lexMin = currStr NEW_LINE DEDENT DEDENT print ( lexMin ) NEW_LINE print ( lexMax ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " GeeksForGeeks " NEW_LINE k = 3 NEW_LINE getSmallestAndLargest ( str1 , k ) NEW_LINE DEDENT