codeparrot/xlcost-text-to-code huggingface dataset:
Count of palindromic strings of size upto N consisting of first K alphabets occurring at most twice | Function of return the number of palindromic strings of length N with first K alphabets possible ; If N is odd , half + 1 position can be filled to cope with the extra middle element ; K is reduced by one , because count of choices for the next position is reduced by 1 as a element can only once ; Return the possible count ; Function to find the count of palindromic string of first K characters according to the given criteria ; If N = 1 , then only K palindromic strings possible . ; If N = 2 , the 2 * K palindromic strings possible , K for N = 1 and K for N = 2 ; Initialize ans with the count of strings possible till N = 2 ; Return the possible count ; Driver Code
def lengthNPalindrome ( N , K ) : NEW_LINE INDENT half = N // 2 ; NEW_LINE if ( N & 1 ) : NEW_LINE INDENT half += 1 ; NEW_LINE DEDENT ans = 1 ; NEW_LINE for i in range ( 1 , half + 1 ) : NEW_LINE INDENT ans *= K ; NEW_LINE K -= 1 ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT def palindromicStrings ( N , K ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT return K ; NEW_LINE DEDENT if ( N == 2 ) : NEW_LINE INDENT return 2 * K ; NEW_LINE DEDENT ans = 0 ; NEW_LINE ans += ( 2 * K ) ; NEW_LINE for i in range ( 3 , N + 1 ) : NEW_LINE INDENT ans += lengthNPalindrome ( i , K ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 ; K = 3 ; NEW_LINE print ( palindromicStrings ( N , K ) ) ; NEW_LINE DEDENT
Minimum number of characters required to be added to a String such that all lowercase alphabets occurs as a subsequence in increasing order | Python3 program for the above approach ; Function to find the LCS of strings S and string T ; Base Case ; Already Calculated State ; If the characters are the same ; Otherwise ; Function to find the minimum number of characters that needs to be appended in the string to get all lowercase alphabets as a subsequences ; String containing all the characters ; Stores the result of overlapping subproblems ; Return the minimum characters required ; Driver Code
import numpy as np NEW_LINE def LCS ( S , N , T , M , dp ) : NEW_LINE INDENT if ( N == 0 or M == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ N ] [ M ] != 0 ) : NEW_LINE INDENT return dp [ N ] [ M ] ; NEW_LINE DEDENT if ( S [ N - 1 ] == T [ M - 1 ] ) : NEW_LINE INDENT dp [ N ] [ M ] = 1 + LCS ( S , N - 1 , T , M - 1 , dp ) ; NEW_LINE return dp [ N ] [ M ] NEW_LINE DEDENT dp [ N ] [ M ] = max ( LCS ( S , N - 1 , T , M , dp ) , LCS ( S , N , T , M - 1 , dp ) ) ; NEW_LINE return dp [ N ] [ M ] NEW_LINE DEDENT def minimumCharacter ( S ) : NEW_LINE INDENT T = " abcdefghijklmnopqrstuvwxyz " ; NEW_LINE N = len ( S ) ; M = len ( T ) ; NEW_LINE dp = np . zeros ( ( N + 1 , M + 1 ) ) ; NEW_LINE return ( 26 - LCS ( S , N , T , M , dp ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " abcdadc " ; NEW_LINE print ( minimumCharacter ( S ) ) ; NEW_LINE DEDENT
Count ways to make the number formed by K concatenations of a numeric string divisible by 5 | Python program for the above approach ; Function to find the value of a ^ b modulo 1000000007 ; Stores the resultant value a ^ b ; Find the value of a ^ b ; Function to count the number of ways such that the formed number is divisible by 5 by removing digits ; Stores the count of ways ; Find the count for string S ; If the digit is 5 or 0 ; Find the count of string for K concatenation of string S ; Find the total count ; Driver Code
MOD = 1000000007 NEW_LINE def exp_mod ( a , b ) : NEW_LINE INDENT ret = 1 NEW_LINE while ( b ) : NEW_LINE INDENT if ( b & 1 ) : NEW_LINE INDENT ret = ret * a % MOD NEW_LINE DEDENT b >>= 1 NEW_LINE a = a * a % MOD NEW_LINE DEDENT return ret NEW_LINE DEDENT def countOfWays ( s , k ) : NEW_LINE INDENT N = len ( s ) NEW_LINE ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( s [ i ] == '5' or s [ i ] == '0' ) : NEW_LINE INDENT ans = ( ans + exp_mod ( 2 , i ) ) % MOD NEW_LINE DEDENT DEDENT q = exp_mod ( 2 , N ) NEW_LINE qk = exp_mod ( q , k ) NEW_LINE inv = exp_mod ( q - 1 , MOD - 2 ) NEW_LINE ans = ans * ( qk - 1 ) % MOD NEW_LINE ans = ans * inv % MOD NEW_LINE return ans NEW_LINE DEDENT S = "1256" NEW_LINE K = 1 NEW_LINE print ( countOfWays ( S , K ) ) NEW_LINE
Find the string present at the middle of a lexicographically increasing sequence of strings from S to T | Function to print the string at the middle of lexicographically increasing sequence of strings from S to T ; Stores the base 26 digits after addition ; Iterete from right to left and add carry to next position ; Reduce the number to find the middle string by dividing each position by 2 ; If current value is odd , carry 26 to the next index value ; Driver Code
def printMiddleString ( S , T , N ) : NEW_LINE INDENT a1 = [ 0 ] * ( N + 1 ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT a1 [ i + 1 ] = ord ( S [ i ] ) - ord ( " a " ) + ord ( T [ i ] ) - ord ( " a " ) ; NEW_LINE DEDENT for i in range ( N , 1 , - 1 ) : NEW_LINE INDENT a1 [ i - 1 ] += a1 [ i ] // 26 ; NEW_LINE a1 [ i ] %= 26 ; NEW_LINE DEDENT for i in range ( N + 1 ) : NEW_LINE INDENT if ( a1 [ i ] & 1 ) : NEW_LINE if ( i + 1 <= N ) : NEW_LINE INDENT a1 [ i + 1 ] += 26 ; NEW_LINE DEDENT a1 [ i ] = a1 [ i ] // 2 ; NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( chr ( a1 [ i ] + ord ( " a " ) ) , end = " " ) ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT N = 5 ; NEW_LINE S = " afogk " ; NEW_LINE T = " asdji " ; NEW_LINE printMiddleString ( S , T , N ) ; NEW_LINE
Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | Python program to solve the above approach ; initializing first and second variable as to store how many string ' s ' and string ' t ' will be appended ; storing tmp length ; if length of string tmp is greater than k , then we have reached our destination string now we can return character at index k ; appending s to tmp , f times ; appending t to tmp , s times ; extracting output character ; Driver code
def KthCharacter ( s , t , k ) : NEW_LINE INDENT f = 1 NEW_LINE ss = 2 NEW_LINE tmp = " " NEW_LINE lenn = len ( tmp ) NEW_LINE while ( lenn < k ) : NEW_LINE INDENT tf = f NEW_LINE ts = ss NEW_LINE while ( tf != 0 ) : NEW_LINE INDENT tf -= 1 NEW_LINE tmp += s NEW_LINE DEDENT while ( ts != 0 ) : NEW_LINE INDENT ts -= 1 NEW_LINE tmp += t NEW_LINE DEDENT f += 2 NEW_LINE ss += 2 NEW_LINE lenn = len ( tmp ) NEW_LINE DEDENT output = tmp [ k - 1 ] NEW_LINE return output NEW_LINE DEDENT S1 = " a " NEW_LINE S2 = " bc " NEW_LINE k = 4 NEW_LINE ans = KthCharacter ( S1 , S2 , k ) NEW_LINE print ( ans ) NEW_LINE
Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | Python program to solve the above approach ; storing length ; variables for temporary strings of s and t ; final output variable ; if k is greater than even after adding string ' s ' ' first ' times ( let ' s ▁ call ▁ it ▁ x ) ▁ we ' ll subtract x from k ; incrementing first by 2 , as said in problem statement ; if k is greater than even after adding string ' t ' ' second ' times ( let ' s ▁ call ▁ it ▁ y ) ▁ we ' ll subtract y from k ; incrementing second by two as said in problem statement ; if k is smaller than y , then the character will be at position k % m . ; returning character at k index ; if k is smaller than x , then the character is at position k % n ; returning character at k index ; Driver code
def KthCharacter ( s , t , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE m = len ( t ) NEW_LINE first = 1 NEW_LINE second = 2 NEW_LINE output = ' ? ' NEW_LINE while ( k > 0 ) : NEW_LINE INDENT if ( k > first * n ) : NEW_LINE INDENT x = first * n NEW_LINE k = k - x NEW_LINE first = first + 2 NEW_LINE if ( k > second * m ) : NEW_LINE INDENT y = second * m NEW_LINE k = k - y NEW_LINE second = second + 2 NEW_LINE DEDENT else : NEW_LINE INDENT ind = k % m NEW_LINE ind -= 1 NEW_LINE if ( ind < 0 ) : NEW_LINE INDENT ind = m - 1 NEW_LINE DEDENT output = t [ ind ] NEW_LINE break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT ind = k % n NEW_LINE ind -= 1 NEW_LINE if ( ind < 0 ) : NEW_LINE INDENT ind = n - 1 NEW_LINE DEDENT output = s [ ind ] NEW_LINE break NEW_LINE DEDENT DEDENT return output NEW_LINE DEDENT S1 = " a " NEW_LINE S2 = " bc " NEW_LINE k = 4 NEW_LINE ans = KthCharacter ( S1 , S2 , k ) NEW_LINE print ( ans ) NEW_LINE
Count of ways to rearrange N digits and M alphabets keeping all alphabets together | Python program of the above approach ; Function to find the factorial of the given number ; Function to count ways to rearrange characters of the string such that all alphabets are adjacent . ; Stores factorial of ( N + 1 ) ; Stores factorial of ; Driver Code ; Given a and b ; Function call
import math NEW_LINE def fact ( a ) : NEW_LINE INDENT return math . factorial ( a ) NEW_LINE DEDENT def findComb ( N , M ) : NEW_LINE INDENT x = fact ( N + 1 ) NEW_LINE y = fact ( M ) NEW_LINE return ( x * y ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE M = 2 NEW_LINE print ( findComb ( N , M ) ) NEW_LINE DEDENT
Count of camel case characters present in a given string | Function to count all the camelcase characters in the string S ; Stores the total count of camelcase characters ; Traverse the string S ; If ASCII value of character lies over the range [ 65 , 91 ] then increment the count ; Print the total count obtained ; Driver Code
def countCamelCase ( S ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT if ( ord ( S [ i ] ) >= 65 and ord ( S [ i ] ) <= 91 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " ckjkUUYII " NEW_LINE countCamelCase ( S ) NEW_LINE DEDENT
Decode a given string by removing duplicate occurrences | Function to count the appearances of each character ; If the character is lower case ; If the character is uppercase ; If the character is a punctuation mark ; Function to decode the given encoded string ; Iterate the given string str ; Find the index of the next character to be printed ; Driver code
def findRepetition ( a ) : NEW_LINE INDENT if a <= ' z ' and a >= ' a ' : NEW_LINE INDENT return ord ( a ) - 97 NEW_LINE DEDENT elif a <= ' Z ' and a >= ' A ' : NEW_LINE INDENT return ord ( a ) - 65 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def decodeString ( str_ ) : NEW_LINE INDENT output = " " NEW_LINE i = 0 NEW_LINE while ( i < len ( str_ ) ) : NEW_LINE INDENT output += str_ [ i ] NEW_LINE i += findRepetition ( str_ [ i ] ) + 1 NEW_LINE DEDENT print ( " Decrypted ▁ code ▁ is ▁ { " + output + " } " ) NEW_LINE DEDENT str_ = " abbbb ▁ acccdddd " NEW_LINE decodeString ( str_ ) NEW_LINE
Program to perform a letter frequency attack on a monoalphabetic substitution cipher | Function to decrypt a monoalphabetic substitution cipher using the letter frequency attack ; Stores final 5 possible deciphered plaintext ; Store the frequency of each letter in cipher text ; Stores the frequency of each letter in cipher text in descending order ; Store which alphabet is used already ; Traverse the string S ; Copy the frequency array ; Stores the string formed from concatanating the english letters in the decreasing frequency in the english language ; Sort the array in descending order ; Itearate over the range [ 0 , 5 ] ; Iterate over the range [ 0 , 26 ] ; Store the numerical equivalent of letter at ith index of array letter_frequency ; Calculate the probable shift used in monoalphabetic cipher ; Temporary string to generate one plaintext at a time ; Generate the probable ith plaintext string using the shift calculated above ; Insert whitespaces as it is ; Shift the kth letter of the cipher by x ; Add the kth calculated / shifted letter to temporary string ; Print the generated 5 possible plaintexts ; Given string ; Function Call
def printString ( S , N ) : NEW_LINE INDENT plaintext = [ None ] * 5 NEW_LINE freq = [ 0 ] * 26 NEW_LINE freqSorted = [ None ] * 26 NEW_LINE used = [ 0 ] * 26 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if S [ i ] != ' ▁ ' : NEW_LINE INDENT freq [ ord ( S [ i ] ) - 65 ] += 1 NEW_LINE DEDENT DEDENT for i in range ( 26 ) : NEW_LINE INDENT freqSorted [ i ] = freq [ i ] NEW_LINE DEDENT T = " ETAOINSHRDLCUMWFGYPBVKJXQZ " NEW_LINE freqSorted . sort ( reverse = True ) NEW_LINE for i in range ( 5 ) : NEW_LINE INDENT ch = - 1 NEW_LINE for j in range ( 26 ) : NEW_LINE INDENT if freqSorted [ i ] == freq [ j ] and used [ j ] == 0 : NEW_LINE INDENT used [ j ] = 1 NEW_LINE ch = j NEW_LINE break NEW_LINE DEDENT DEDENT if ch == - 1 : NEW_LINE INDENT break NEW_LINE DEDENT x = ord ( T [ i ] ) - 65 NEW_LINE x = x - ch NEW_LINE curr = " " NEW_LINE for k in range ( N ) : NEW_LINE INDENT if S [ k ] == ' ▁ ' : NEW_LINE INDENT curr += " ▁ " NEW_LINE continue NEW_LINE DEDENT y = ord ( S [ k ] ) - 65 NEW_LINE y += x NEW_LINE if y < 0 : NEW_LINE INDENT y += 26 NEW_LINE DEDENT if y > 25 : NEW_LINE INDENT y -= 26 NEW_LINE DEDENT curr += chr ( y + 65 ) NEW_LINE DEDENT plaintext [ i ] = curr NEW_LINE DEDENT for i in range ( 5 ) : NEW_LINE INDENT print ( plaintext [ i ] ) NEW_LINE DEDENT DEDENT S = " B ▁ TJNQMF ▁ NFTTBHF " NEW_LINE N = len ( S ) NEW_LINE printString ( S , N ) NEW_LINE
Count of substrings in a Binary String that contains more 1 s than 0 s | Function to merge two partitions such that the merged array is sorted ; Counting inversions ; Function to implement merge sort ; Function to calculate number of inversions in a given array ; Calculate the number of inversions ; Return the number of inversions ; Function to count the number of substrings that contains more 1 s than 0 s ; Stores the prefix sum array ; Stores the count of valid substrings ; Driver Code ; Given Input ; Function Call
def merge ( v , left , mid , right , inversions ) : NEW_LINE INDENT temp = [ 0 for i in range ( right - left + 1 ) ] NEW_LINE i = left NEW_LINE j = mid + 1 NEW_LINE k = 0 NEW_LINE cnt = 0 NEW_LINE while ( i <= mid and j <= right ) : NEW_LINE INDENT if ( v [ i ] <= v [ j ] ) : NEW_LINE INDENT temp [ k ] = v [ i ] NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT inversions += ( mid - i + 1 ) NEW_LINE temp [ k ] = v [ j ] NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT while ( i <= mid ) : NEW_LINE INDENT temp [ k ] = v [ i ] NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT while ( j <= right ) : NEW_LINE INDENT temp [ k ] = v [ j ] NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT k = 0 NEW_LINE for a in range ( left , right + 1 , 1 ) : NEW_LINE INDENT v [ a ] = temp [ k ] NEW_LINE k += 1 NEW_LINE DEDENT DEDENT def mergeSort ( v , left , right , inversions ) : NEW_LINE INDENT if ( left < right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE mergeSort ( v , left , mid , inversions ) NEW_LINE mergeSort ( v , mid + 1 , right , inversions ) NEW_LINE merge ( v , left , mid , right , inversions ) NEW_LINE DEDENT DEDENT def CountInversions ( v ) : NEW_LINE INDENT n = len ( v ) NEW_LINE inversions = 0 NEW_LINE mergeSort ( v , 0 , n - 1 , inversions ) NEW_LINE return inversions NEW_LINE DEDENT def getSubsCount ( input ) : NEW_LINE INDENT n = len ( input ) NEW_LINE nums = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT nums [ i ] = ord ( input [ i ] ) - 48 NEW_LINE if ( nums [ i ] == 0 ) : NEW_LINE INDENT nums [ i ] = - 1 NEW_LINE DEDENT DEDENT pref = [ 0 for i in range ( n ) ] NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += nums [ i ] NEW_LINE pref [ i ] = sum NEW_LINE DEDENT cnt = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( pref [ i ] > 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT pref = pref [ : - 1 ] NEW_LINE inversions = CountInversions ( pref ) NEW_LINE ans = cnt + inversions + 1 NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT input = "101" NEW_LINE ans = getSubsCount ( input ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Check if a string consisting only of a , b , c can be made empty by removing substring " abc " recursively | Function to check if the given string S can be made empty ; Stores the characters of the string S ; Traverse the given string ; If the character is c ; If stack size is greater than 2 ; Pop from the stack ; Top two characters in the stack should be ' b ' and ' a ' respectively ; Otherwise , print No ; If character is ' a ' or ' b ' push to stack ; If stack is empty , then print Yes . Otherwise print No ; Driver code
def canMadeEmpty ( s , n ) : NEW_LINE INDENT st = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if s [ i ] == ' c ' : NEW_LINE INDENT if len ( st ) >= 2 : NEW_LINE INDENT b = st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE a = st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE if a != ' a ' or b != ' b ' : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT st . append ( s [ i ] ) NEW_LINE DEDENT DEDENT if len ( st ) == 0 : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT s = " aabcbc " NEW_LINE n = len ( s ) NEW_LINE print ( canMadeEmpty ( s , n ) ) NEW_LINE
Check if summation of two words is equal to target word | Function to check weather summation of two words equal to target word ; Store the length of each string ; Reverse the strings A , B and C ; Stores the remainder ; Iterate in the range [ 0 , max ( L , max ( M , N ) ) ] ; Stores the integer at ith position from the right in the sum of A and B ; If i is less than L ; If i is less than M ; Update rem and curr ; If i is less than N and curr is not equal to C [ i ] - ' a ' , return " No " ; If rem is greater than 0 , return " No " ; Otherwise , return " Yes " ; Driver Code ; Given Input ; Function Call
def isSumEqual ( A , B , C ) : NEW_LINE INDENT L = len ( A ) NEW_LINE M = len ( B ) NEW_LINE N = len ( A ) NEW_LINE A = A [ : : - 1 ] NEW_LINE B = B [ : : - 1 ] NEW_LINE C = C [ : : - 1 ] NEW_LINE rem = 0 NEW_LINE for i in range ( max ( L , max ( M , N ) ) ) : NEW_LINE INDENT curr = rem NEW_LINE if ( i < L ) : NEW_LINE INDENT curr += ord ( A [ i ] ) - ord ( ' a ' ) NEW_LINE DEDENT if ( i < M ) : NEW_LINE INDENT curr += ord ( B [ i ] ) - ord ( ' a ' ) NEW_LINE DEDENT rem = curr // 10 NEW_LINE curr %= 10 NEW_LINE if ( i < N and curr != ord ( C [ i ] ) - ord ( ' a ' ) ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT if ( rem ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT else : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = " acb " NEW_LINE B = " cba " NEW_LINE C = " cdb " NEW_LINE print ( isSumEqual ( A , B , C ) ) NEW_LINE DEDENT
Minimum K such that every substring of length at least K contains a character c | Set | Function to find minimum value of K such that there exist atleast one K amazing character ; Stores the answer ; Update the S ; Iterate over the characters in range [ a , z ] ; Stores the last index where c appears ; Stores the maximum possible length ; Update S ; Iterate over characters of string S ; If S [ i ] is equal to c ; Stores the distance between positions of two same c ; Update max_len ; Update the value of prev ; Update the value of ans ; Return the answer ; Driver Code ; Given Input ; Function Call
def MinimumLengthSubstring ( S , N ) : NEW_LINE INDENT ans = N NEW_LINE S = "0" + S + "0" NEW_LINE S = [ i for i in S ] NEW_LINE for c in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT prev = 0 NEW_LINE max_len = 0 NEW_LINE S [ 0 ] = chr ( c ) NEW_LINE S [ N + 1 ] = chr ( c ) NEW_LINE for i in range ( 1 , N + 2 ) : NEW_LINE INDENT if ( S [ i ] == chr ( c ) ) : NEW_LINE INDENT len = i - prev NEW_LINE max_len = max ( max_len , len ) NEW_LINE prev = i NEW_LINE DEDENT DEDENT ans = min ( ans , max_len ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abcde " NEW_LINE N = len ( S ) NEW_LINE print ( MinimumLengthSubstring ( S , N ) ) NEW_LINE DEDENT
Minimize hamming distance in Binary String by setting only one K size substring bits | Function to find minimum Hamming Distance after atmost one operation ; Store the size of the string ; Store the prefix sum of 1 s ; Create Prefix Sum array ; Initialize cnt as number of ones in string S ; Store the required result ; Traverse the string , S ; Store the number of 1 s in the substring S [ i , i + K - 1 ] ; Update the answer ; Return the result ; Driver Code ; Given Input ; Function Call
def minimumHammingDistance ( S , K ) : NEW_LINE INDENT n = len ( S ) NEW_LINE pref = [ 0 ] * n NEW_LINE pref [ 0 ] = ord ( S [ 0 ] ) - ord ( '0' ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + ( ord ( S [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT cnt = pref [ n - 1 ] NEW_LINE ans = cnt NEW_LINE for i in range ( n - K ) : NEW_LINE INDENT value = pref [ i + K - 1 ] - ( pref [ i - 1 ] if ( i - 1 ) >= 0 else 0 ) NEW_LINE ans = min ( ans , cnt - value + ( K - value ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "101" NEW_LINE K = 2 NEW_LINE print ( minimumHammingDistance ( s , K ) ) NEW_LINE DEDENT
Check if a permutation of S2 can be obtained by adding or removing characters from S1 | Python 3 program for the above approach ; Function to check if the given number is prime or not ; If the number is less than 2 ; If the number is 2 ; If N is a multiple of 2 ; Otherwise , check for the odds values ; Function to check if S1 can be a permutation of S2 by adding or removing characters from S1 ; Initialize a frequency array ; Decrement the frequency for occurrence in s1 ; Increment the frequency for occurence in s2 ; If frequency of current char is same in s1 and s2 ; Check the frequency for the current char is not prime ; Print the result ; Driver Code
from math import sqrt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT elif ( n == 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT elif ( n % 2 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 3 , int ( sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def checkPermutation ( s1 , s2 ) : NEW_LINE INDENT freq = [ 0 for i in range ( 26 ) ] NEW_LINE for ch in s1 : NEW_LINE INDENT if ord ( ch ) - 97 in freq : NEW_LINE INDENT freq [ ord ( ch ) - 97 ] -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ ord ( ch ) - 97 ] = 1 NEW_LINE DEDENT DEDENT for ch in s2 : NEW_LINE INDENT if ord ( ch ) - 97 in freq : NEW_LINE INDENT freq [ ord ( ch ) - 97 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ ord ( ch ) - 97 ] = 1 NEW_LINE DEDENT DEDENT isAllChangesPrime = True NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( isPrime ( abs ( freq [ i ] ) ) == False ) : NEW_LINE INDENT isAllChangesPrime = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( isAllChangesPrime == False ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S1 = " gekforgk " NEW_LINE S2 = " geeksforgeeks " NEW_LINE checkPermutation ( S1 , S2 ) NEW_LINE DEDENT