codeparrot/xlcost-text-to-code huggingface dataset:
Encryption and Decryption of String according to given technique | Python3 implementation for Custom Encryption and Decryption of String ; Function to encrypt the ; Matrix to generate the Encrypted String ; Fill the matrix row - wise ; Loop to generate encrypted ; Function to decrypt the ; Matrix to generate the Encrypted String ; Fill the matrix column - wise ; Loop to generate decrypted ; Driver Code ; Encryption of String ; Decryption of String
from math import ceil , floor , sqrt NEW_LINE def encryption ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE b = ceil ( sqrt ( l ) ) NEW_LINE a = floor ( sqrt ( l ) ) NEW_LINE encrypted = " " NEW_LINE if ( b * a < l ) : NEW_LINE INDENT if ( min ( b , a ) == b ) : NEW_LINE INDENT b = b + 1 NEW_LINE DEDENT else : NEW_LINE INDENT a = a + 1 NEW_LINE DEDENT DEDENT arr = [ [ ' ▁ ' for i in range ( a ) ] for j in range ( b ) ] NEW_LINE k = 0 NEW_LINE for j in range ( a ) : NEW_LINE INDENT for i in range ( b ) : NEW_LINE INDENT if ( k < l ) : NEW_LINE INDENT arr [ j ] [ i ] = s [ k ] NEW_LINE DEDENT k += 1 NEW_LINE DEDENT DEDENT for j in range ( b ) : NEW_LINE INDENT for i in range ( a ) : NEW_LINE INDENT encrypted = encrypted + arr [ i ] [ j ] NEW_LINE DEDENT DEDENT return encrypted NEW_LINE DEDENT def decryption ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE b = ceil ( sqrt ( l ) ) NEW_LINE a = floor ( sqrt ( l ) ) NEW_LINE decrypted = " " NEW_LINE arr = [ [ ' ▁ ' for i in range ( a ) ] for j in range ( b ) ] NEW_LINE k = 0 NEW_LINE for j in range ( b ) : NEW_LINE INDENT for i in range ( a ) : NEW_LINE INDENT if ( k < l ) : NEW_LINE INDENT arr [ j ] [ i ] = s [ k ] NEW_LINE DEDENT k += 1 NEW_LINE DEDENT DEDENT for j in range ( a ) : NEW_LINE INDENT for i in range ( b ) : NEW_LINE INDENT decrypted = decrypted + arr [ i ] [ j ] NEW_LINE DEDENT DEDENT return decrypted NEW_LINE DEDENT s = " Geeks ▁ For ▁ Geeks " NEW_LINE encrypted = " " NEW_LINE decrypted = " " NEW_LINE encrypted = encryption ( s ) NEW_LINE print ( encrypted ) NEW_LINE decrypted = decryption ( encrypted ) NEW_LINE print ( decrypted ) NEW_LINE
Program to accept String starting with Capital letter | Function to check if first character is Capital ; Function to check ; Driver function
def checkIfStartsWithCapital ( string ) : NEW_LINE INDENT if ( string [ 0 ] >= ' A ' and string [ 0 ] <= ' Z ' ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT def check ( string ) : NEW_LINE INDENT if ( checkIfStartsWithCapital ( string ) ) : NEW_LINE INDENT print ( " Accepted " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Accepted " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " GeeksforGeeks " ; NEW_LINE check ( string ) ; NEW_LINE string = " geeksforgeeks " ; NEW_LINE check ( string ) ; NEW_LINE DEDENT
Program to accept a Strings which contains all the Vowels | Function to to check that a string contains all vowels ; Hash Array of size 5 such that the index 0 , 1 , 2 , 3 and 4 represent the vowels a , e , i , o and u ; Loop the string to mark the vowels which are present ; Loop to check if there is any vowel which is not present in the string ; Function to to check that a string contains all vowels ; Driver Code
def checkIfAllVowels ( string ) : NEW_LINE INDENT hash = [ 0 ] * 5 ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if ( string [ i ] == ' A ' or string [ i ] == ' a ' ) : NEW_LINE INDENT hash [ 0 ] = 1 ; NEW_LINE DEDENT elif ( string [ i ] == ' E ' or string [ i ] == ' e ' ) : NEW_LINE INDENT hash [ 1 ] = 1 ; NEW_LINE DEDENT elif ( string [ i ] == ' I ' or string [ i ] == ' i ' ) : NEW_LINE INDENT hash [ 2 ] = 1 ; NEW_LINE DEDENT elif ( string [ i ] == ' O ' or string [ i ] == ' o ' ) : NEW_LINE INDENT hash [ 3 ] = 1 ; NEW_LINE DEDENT elif ( string [ i ] == ' U ' or string [ i ] == ' u ' ) : NEW_LINE INDENT hash [ 4 ] = 1 ; NEW_LINE DEDENT DEDENT for i in range ( 5 ) : NEW_LINE INDENT if ( hash [ i ] == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT DEDENT return 0 ; NEW_LINE DEDENT def checkIfAllVowelsArePresent ( string ) : NEW_LINE INDENT if ( checkIfAllVowels ( string ) ) : NEW_LINE INDENT print ( " Not ▁ Accepted " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Accepted " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " aeioubc " ; NEW_LINE checkIfAllVowelsArePresent ( string ) ; NEW_LINE DEDENT
Smallest odd number with even sum of digits from the given number N | Function to find the smallest odd number whose sum of digits is even from the given string ; Converting the given string to a list of digits ; An empty array to store the digits ; For loop to iterate through each digit ; If the given digit is odd then the digit is appended to the array b ; Sorting the list of digits ; If the size of the list is greater than 1 then a 2 digit smallest odd number is returned Since the sum of two odd digits is always even ; Else , - 1 is returned ; Driver code
def smallest ( s ) : NEW_LINE INDENT a = list ( s ) NEW_LINE b = [ ] NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT if ( int ( a [ i ] ) % 2 != 0 ) : NEW_LINE INDENT b . append ( a [ i ] ) NEW_LINE DEDENT DEDENT b = sorted ( b ) NEW_LINE if ( len ( b ) > 1 ) : NEW_LINE INDENT return int ( b [ 0 ] ) * 10 + int ( b [ 1 ] ) NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( smallest ( "15470" ) ) NEW_LINE DEDENT
Number formed by deleting digits such that sum of the digits becomes even and the number odd | Function to convert a number into odd number such that digit - sum is odd ; Loop to find any first two odd number such that their sum is even and number is odd ; Print the result ; Driver Code
def converthenumber ( n ) : NEW_LINE INDENT s = str ( n ) ; NEW_LINE res = " " ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == '1' or s [ i ] == '3' or s [ i ] == '5' or s [ i ] == '7' or s [ i ] == '9' ) : NEW_LINE INDENT res += s [ i ] ; NEW_LINE DEDENT if ( len ( res ) == 2 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( len ( res ) == 2 ) : NEW_LINE INDENT print ( res ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 18720 ; NEW_LINE converthenumber ( n ) ; NEW_LINE DEDENT
Longest palindromic String formed using concatenation of given strings in any order | Function to find the longest palindromic from given array of strings ; Loop to find the pair of strings which are reverse of each other ; Loop to find if any palindromic is still left in the array ; Update the answer with all strings of pair1 ; Update the answer with palindromic s1 ; Update the answer with all strings of pair2 ; Driver Code
def longestPalindrome ( a , n ) : NEW_LINE INDENT pair1 = [ 0 ] * n NEW_LINE pair2 = [ 0 ] * n NEW_LINE r = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = a [ i ] NEW_LINE s = s [ : : - 1 ] NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( a [ i ] != " " and a [ j ] != " " ) : NEW_LINE INDENT if ( s == a [ j ] ) : NEW_LINE INDENT pair1 [ r ] = a [ i ] NEW_LINE pair2 [ r ] = a [ j ] NEW_LINE r += 1 NEW_LINE a [ i ] = " " NEW_LINE a [ j ] = " " NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT s1 = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = a [ i ] NEW_LINE a [ i ] = a [ i ] [ : : - 1 ] NEW_LINE if ( a [ i ] != " " ) : NEW_LINE INDENT if ( a [ i ] == s ) : NEW_LINE INDENT s1 = a [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT ans = " " NEW_LINE for i in range ( r ) : NEW_LINE INDENT ans = ans + pair1 [ i ] NEW_LINE DEDENT if ( s1 != " " ) : NEW_LINE INDENT ans = ans + s1 NEW_LINE DEDENT for j in range ( r - 1 , - 1 , - 1 ) : NEW_LINE INDENT ans = ans + pair2 [ j ] NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT a1 = [ " aba " , " aba " ] NEW_LINE n1 = len ( a1 ) NEW_LINE longestPalindrome ( a1 , n1 ) NEW_LINE a2 = [ " abc " , " dba " , " kop " , " abd " , " cba " ] NEW_LINE n2 = len ( a2 ) NEW_LINE longestPalindrome ( a2 , n2 ) NEW_LINE
Program to print the given digit in words | Function to return the word of the corresponding digit ; For digit 0 ; For digit 1 ; For digit 2 ; For digit 3 ; For digit 4 ; For digit 5 ; For digit 6 ; For digit 7 ; For digit 8 ; For digit 9 ; Function to iterate through every digit in the given number ; Finding each digit of the number ; Print the digit in words ; Driver code
def printValue ( digit ) : NEW_LINE INDENT if digit == '0' : NEW_LINE INDENT print ( " Zero ▁ " , end = " ▁ " ) NEW_LINE DEDENT elif digit == '1' : NEW_LINE INDENT print ( " One ▁ " , end = " ▁ " ) NEW_LINE DEDENT elif digit == '2' : NEW_LINE INDENT print ( " Two ▁ " , end = " ▁ " ) NEW_LINE DEDENT elif digit == '3' : NEW_LINE INDENT print ( " Three " , end = " ▁ " ) NEW_LINE DEDENT elif digit == '4' : NEW_LINE INDENT print ( " Four ▁ " , end = " ▁ " ) NEW_LINE DEDENT elif digit == '5' : NEW_LINE INDENT print ( " Five ▁ " , end = " ▁ " ) NEW_LINE DEDENT elif digit == '6' : NEW_LINE INDENT print ( " Six ▁ " , end = " ▁ " ) NEW_LINE DEDENT elif digit == '7' : NEW_LINE INDENT print ( " Seven " , end = " ▁ " ) NEW_LINE DEDENT elif digit == '8' : NEW_LINE INDENT print ( " Eight " , end = " ▁ " ) NEW_LINE DEDENT elif digit == '9' : NEW_LINE INDENT print ( " Nine ▁ " , end = " ▁ " ) NEW_LINE DEDENT DEDENT def printWord ( N ) : NEW_LINE INDENT i = 0 NEW_LINE length = len ( N ) NEW_LINE while i < length : NEW_LINE INDENT printValue ( N [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT N = "123" NEW_LINE printWord ( N ) NEW_LINE
Jaro and Jaro | Python3 implementation of above approach ; Function to calculate the Jaro Similarity of two s ; If the s are equal ; Length of two s ; Maximum distance upto which matching is allowed ; Count of matches ; Hash for matches ; Traverse through the first ; Check if there is any matches ; If there is a match ; If there is no match ; Number of transpositions ; Count number of occurrences where two characters match but there is a third matched character in between the indices ; Find the next matched character in second ; Return the Jaro Similarity ; Driver code ; Prjaro Similarity of two s
from math import floor , ceil NEW_LINE def jaro_distance ( s1 , s2 ) : NEW_LINE INDENT if ( s1 == s2 ) : NEW_LINE INDENT return 1.0 NEW_LINE DEDENT len1 = len ( s1 ) NEW_LINE len2 = len ( s2 ) NEW_LINE max_dist = floor ( max ( len1 , len2 ) / 2 ) - 1 NEW_LINE match = 0 NEW_LINE hash_s1 = [ 0 ] * len ( s1 ) NEW_LINE hash_s2 = [ 0 ] * len ( s2 ) NEW_LINE for i in range ( len1 ) : NEW_LINE INDENT for j in range ( max ( 0 , i - max_dist ) , min ( len2 , i + max_dist + 1 ) ) : NEW_LINE INDENT if ( s1 [ i ] == s2 [ j ] and hash_s2 [ j ] == 0 ) : NEW_LINE INDENT hash_s1 [ i ] = 1 NEW_LINE hash_s2 [ j ] = 1 NEW_LINE match += 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if ( match == 0 ) : NEW_LINE INDENT return 0.0 NEW_LINE DEDENT t = 0 NEW_LINE point = 0 NEW_LINE for i in range ( len1 ) : NEW_LINE INDENT if ( hash_s1 [ i ] ) : NEW_LINE INDENT while ( hash_s2 [ point ] == 0 ) : NEW_LINE INDENT point += 1 NEW_LINE DEDENT if ( s1 [ i ] != s2 [ point ] ) : NEW_LINE INDENT point += 1 NEW_LINE t += 1 NEW_LINE DEDENT DEDENT DEDENT t = t // 2 NEW_LINE return ( match / len1 + match / len2 + ( match - t + 1 ) / match ) / 3.0 NEW_LINE DEDENT s1 = " CRATE " NEW_LINE s2 = " TRACE " NEW_LINE print ( round ( jaro_distance ( s1 , s2 ) , 6 ) ) NEW_LINE
Find the resultant String after replacing X with Y and removing Z | Function to replace and remove ; Two pointer start and end points to beginning and end position in the string ; If start is having Z find X pos in end and replace Z with another character ; Find location for having different character instead of Z ; If found swap character at start and end ; Else increment start Also checkin for X at start position ; Driver code
def replaceRemove ( s , X , Y , Z ) : NEW_LINE INDENT s = list ( s ) ; NEW_LINE start = 0 ; NEW_LINE end = len ( s ) - 1 ; NEW_LINE while ( start <= end ) : NEW_LINE INDENT if ( s [ start ] == Z ) : NEW_LINE INDENT while ( end >= 0 and s [ end ] == Z ) : NEW_LINE INDENT end -= 1 ; NEW_LINE DEDENT if ( end > start ) : NEW_LINE INDENT s [ start ] , s [ end ] = s [ end ] , s [ start ] NEW_LINE if ( s [ start ] == X ) : NEW_LINE INDENT s [ start ] = Y ; NEW_LINE DEDENT start += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( s [ start ] == X ) : NEW_LINE INDENT s [ start ] = Y ; NEW_LINE DEDENT start += 1 ; NEW_LINE DEDENT DEDENT while ( len ( s ) > 0 and s [ len ( s ) - 1 ] == Z ) : NEW_LINE INDENT s . pop ( ) ; NEW_LINE DEDENT return " " . join ( s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " batman " ; NEW_LINE X = ' a ' ; Y = ' d ' ; Z = ' b ' ; NEW_LINE string = replaceRemove ( string , X , Y , Z ) ; NEW_LINE if ( len ( string ) == 0 ) : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( string ) ; NEW_LINE DEDENT DEDENT
Check if all bits can be made same by flipping two consecutive bits | Function to check if Binary string can be made equal ; Counting occurence of zero and one in binary string ; From above observation ; Driver code
def canMake ( s ) : NEW_LINE INDENT o = 0 ; z = 0 ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( ord ( s [ i ] ) - ord ( '0' ) == 1 ) : NEW_LINE INDENT o += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT z += 1 ; NEW_LINE DEDENT DEDENT if ( o % 2 == 1 and z % 2 == 1 ) : NEW_LINE INDENT return " NO " ; NEW_LINE DEDENT else : NEW_LINE INDENT return " YES " ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "01011" ; NEW_LINE print ( canMake ( s ) ) ; NEW_LINE DEDENT
Count of non | Python3 implementation of the above approach ; Function to find the count of the number of strings ; Loop to iterate over the frequency of character of string ; Reduce the frequency of current element ; Recursive call ; Backtrack ; Function to count the number of non - empty sequences ; store the frequency of each character ; Maintain the frequency ; Driver Code ; Function Call
count = 0 NEW_LINE def countNumberOfStringsUtil ( freq , Count ) : NEW_LINE INDENT global count NEW_LINE count = Count NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] > 0 ) : NEW_LINE INDENT freq [ i ] -= 1 NEW_LINE count += 1 NEW_LINE countNumberOfStringsUtil ( freq , count ) ; NEW_LINE freq [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT def countNumberOfStrings ( s ) : NEW_LINE INDENT global count NEW_LINE global freq NEW_LINE freq = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' A ' ) ] += 1 NEW_LINE DEDENT countNumberOfStringsUtil ( freq , count ) ; NEW_LINE return count NEW_LINE DEDENT s = " AAABBC " NEW_LINE print ( countNumberOfStrings ( s ) ) NEW_LINE
Minimum swaps required to make a binary string divisible by 2 ^ k | Function to return the minimum swaps required ; To store the final answer ; To store the count of one and zero ; Loop from end of the string ; If s [ i ] = 1 ; If s [ i ] = 0 ; If c_zero = k ; If the result can 't  be achieved ; Return the final answer ; Driver code
def findMinSwaps ( s , k ) : NEW_LINE INDENT ans = 0 ; NEW_LINE c_one = 0 ; c_zero = 0 ; NEW_LINE for i in range ( len ( s ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT c_one += 1 ; NEW_LINE DEDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT c_zero += 1 ; NEW_LINE ans += c_one ; NEW_LINE DEDENT if ( c_zero == k ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( c_zero < k ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "100111" ; NEW_LINE k = 2 ; NEW_LINE print ( findMinSwaps ( s , k ) ) ; NEW_LINE DEDENT
Remove vowels from a string stored in a Binary Tree | Structure Representing the Node in the Binary tree ; Function to perform a level order insertion of a Node in the Binary tree ; If the root is empty , make it point to the Node ; In case there are elements in the Binary tree , perform a level order traversal using a Queue ; If the left child does not exist , insert the Node as the left child ; In case the right child does not exist , insert the Node as the right child ; Function to print the level order traversal of the Binary tree ; Function to check if the character is a vowel or not . ; Function to remove the vowels in the Binary tree ; Declaring the root of the tree ; If the given character is not a vowel , add it to the Binary tree ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , _val ) : NEW_LINE INDENT self . data = _val NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def addinBT ( root , data ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT root = Node ( data ) NEW_LINE DEDENT else : NEW_LINE INDENT Q = [ ] NEW_LINE Q . append ( root ) NEW_LINE while ( len ( Q ) > 0 ) : NEW_LINE INDENT temp = Q [ - 1 ] NEW_LINE Q . pop ( ) NEW_LINE if ( temp . left == None ) : NEW_LINE INDENT temp . left = Node ( data ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT Q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right == None ) : NEW_LINE INDENT temp . right = Node ( data ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT Q . append ( temp . right ) NEW_LINE DEDENT DEDENT DEDENT return root NEW_LINE DEDENT def print_ ( root ) : NEW_LINE INDENT Q = [ ] NEW_LINE Q . append ( root ) NEW_LINE while ( len ( Q ) > 0 ) : NEW_LINE INDENT temp = Q [ - 1 ] NEW_LINE Q . pop ( ) NEW_LINE print ( temp . data , end = " ▁ " ) NEW_LINE if ( temp . left != None ) : NEW_LINE INDENT Q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right != None ) : NEW_LINE INDENT Q . append ( temp . right ) NEW_LINE DEDENT DEDENT DEDENT def checkvowel ( ch ) : NEW_LINE INDENT ch = ch . lower ( ) NEW_LINE if ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def removevowels ( root ) : NEW_LINE INDENT Q = [ ] NEW_LINE Q . append ( root ) NEW_LINE root1 = None NEW_LINE while ( len ( Q ) > 0 ) : NEW_LINE INDENT temp = Q [ - 1 ] NEW_LINE Q . pop ( ) NEW_LINE if ( not checkvowel ( temp . data ) ) : NEW_LINE INDENT root1 = addinBT ( root1 , temp . data ) NEW_LINE DEDENT if ( temp . left != None ) : NEW_LINE INDENT Q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right != None ) : NEW_LINE INDENT Q . append ( temp . right ) NEW_LINE DEDENT DEDENT return root1 NEW_LINE DEDENT s = " geeks " NEW_LINE root = None NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT root = addinBT ( root , s [ i ] ) NEW_LINE DEDENT root = removevowels ( root ) NEW_LINE print_ ( root ) NEW_LINE
Number of sub | Function to return the count of the required substrings ; To store the final answer ; Loop to find the answer ; Condition to update the answer ; Driver code
def countSubStr ( strr , lenn ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( strr [ i ] == '0' ) : NEW_LINE INDENT ans += ( i + 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT strr = "10010" NEW_LINE lenn = len ( strr ) NEW_LINE print ( countSubStr ( strr , lenn ) ) NEW_LINE
Smallest non | Function to return the length of the smallest subdivisible by 2 ^ k ; To store the final answer ; Left pointer ; Right pointer ; Count of the number of zeros and ones in the current substring ; Loop for two pointers ; Condition satisfied ; Updated the answer ; Update the pointer and count ; Update the pointer and count ; Driver code
def findLength ( s , k ) : NEW_LINE INDENT ans = 10 ** 9 NEW_LINE l = 0 NEW_LINE r = 0 NEW_LINE cnt_zero = 0 NEW_LINE cnt_one = 0 NEW_LINE while ( l < len ( s ) and r <= len ( s ) ) : NEW_LINE INDENT if ( cnt_zero >= k and cnt_one >= 1 ) : NEW_LINE INDENT ans = min ( ans , r - l ) NEW_LINE l += 1 NEW_LINE if ( s [ l - 1 ] == '0' ) : NEW_LINE INDENT cnt_zero -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt_one -= 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( r == len ( s ) ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( s [ r ] == '0' ) : NEW_LINE INDENT cnt_zero += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt_one += 1 NEW_LINE DEDENT r += 1 NEW_LINE DEDENT DEDENT if ( ans == 10 ** 9 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT s = "100" NEW_LINE k = 2 NEW_LINE print ( findLength ( s , k ) ) NEW_LINE
Largest sub | Function to return the largest substring divisible by 2 ; While the last character of the string is '1' , pop it ; If the original string had no '0 ; Driver code
def largestSubStr ( s ) : NEW_LINE INDENT while ( len ( s ) and s [ len ( s ) - 1 ] == '1' ) : NEW_LINE INDENT s = s [ : len ( s ) - 1 ] ; NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT return " - 1" ; NEW_LINE DEDENT else : NEW_LINE INDENT return s ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "11001" ; NEW_LINE print ( largestSubStr ( s ) ) ; NEW_LINE DEDENT
Find whether X exists in Y after jumbling X | Python3 implementation of the approach ; Function that returns true if both the arrays have equal values ; Test the equality ; Function that returns true if any permutation of X exists as a sub in Y ; Base case ; To store cumulative frequency ; Finding the frequency of characters in X ; Finding the frequency of characters in Y upto the length of X ; Equality check ; Two pointer approach to generate the entire cumulative frequency ; Remove the first character of the previous window ; Add the last character of the current window ; Equality check ; Driver Code
MAX = 26 NEW_LINE def areEqual ( a , b ) : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT if ( a [ i ] != b [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def xExistsInY ( x , y ) : NEW_LINE INDENT if ( len ( x ) > len ( y ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT cnt_x = [ 0 ] * MAX NEW_LINE cnt = [ 0 ] * MAX NEW_LINE for i in range ( len ( x ) ) : NEW_LINE INDENT cnt_x [ ord ( x [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT for i in range ( len ( x ) ) : NEW_LINE INDENT cnt [ ord ( y [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT if ( areEqual ( cnt_x , cnt ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( 1 , len ( y ) - len ( x ) + 1 ) : NEW_LINE INDENT cnt [ ord ( y [ i - 1 ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE cnt [ ord ( y [ i + len ( x ) - 1 ] ) - ord ( ' a ' ) ] += 1 NEW_LINE if ( areEqual ( cnt , cnt_x ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = " skege " NEW_LINE y = " geeksforgeeks " NEW_LINE if ( xExistsInY ( x , y ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Reverse substrings between each pair of parenthesis | Function to return the modified string ; Push the index of the current opening bracket ; Reverse the substarting after the last encountered opening bracket till the current character ; To store the modified string ; Driver code
def reverseParentheses ( strr , lenn ) : NEW_LINE INDENT st = [ ] NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( strr [ i ] == ' ( ' ) : NEW_LINE INDENT st . append ( i ) NEW_LINE DEDENT elif ( strr [ i ] == ' ) ' ) : NEW_LINE INDENT temp = strr [ st [ - 1 ] : i + 1 ] NEW_LINE strr = strr [ : st [ - 1 ] ] + temp [ : : - 1 ] + strr [ i + 1 : ] NEW_LINE del st [ - 1 ] NEW_LINE DEDENT DEDENT res = " " NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( strr [ i ] != ' ) ' and strr [ i ] != ' ( ' ) : NEW_LINE INDENT res += ( strr [ i ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " ( skeeg ( for ) skeeg ) " NEW_LINE lenn = len ( strr ) NEW_LINE st = [ i for i in strr ] NEW_LINE print ( reverseParentheses ( strr , lenn ) ) NEW_LINE DEDENT