codeparrot/xlcost-text-to-code huggingface dataset:
Check if a string is a subsequence of another string ( using Stacks ) | Function to check if target is a subsequence of string S ; Declare a stack ; Push the characters of target into the stack ; Traverse the string S in reverse ; If the stack is empty ; If S [ i ] is same as the top of the stack ; Pop the top of stack ; Stack s is empty ; Driver Code
def checkforSubsequence ( S , target ) : NEW_LINE INDENT s = [ ] NEW_LINE for i in range ( len ( target ) ) : NEW_LINE INDENT s . append ( target [ i ] ) NEW_LINE DEDENT for i in range ( len ( S ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE return NEW_LINE DEDENT if ( S [ i ] == s [ - 1 ] ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT DEDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " KOTTAYAM " NEW_LINE target = " KOTA " NEW_LINE checkforSubsequence ( S , target ) NEW_LINE DEDENT
Program to construct a DFA to check if a given integer is unsigned or not | Python3 program for the above approach ; Function to construct DFA as per the given conditions ; If at state 0 and a digit has occurred then set it to state 1 ; Similarly for all other states ; Function to build and connect the DFA states ; Connect all the states to the dead state ; Function call to make DFA as per the given conditions ; Function call to check whether an integer in the form of is unsigned integer or not ; Build the DFA ; Stores the current state ; Traverse the string ; If at a certain state a digit occurs then change the current state according to the DFA ; Or + / - sign ; Or decimal occurred ; Or any other character ; Or e / E or exponent sign ; State 1 , 4 , 8 will give the final answer ; Driver Code
digits , sign = "0123456789" , " + - " NEW_LINE dot , ex = " . " , " eE " NEW_LINE dfa = [ [ 0 for i in range ( 5 ) ] for i in range ( 11 ) ] NEW_LINE def makeDFA ( ) : NEW_LINE INDENT global dfa NEW_LINE dfa [ 0 ] [ 0 ] = 1 NEW_LINE dfa [ 1 ] [ 0 ] = 1 NEW_LINE dfa [ 1 ] [ 2 ] = 3 NEW_LINE dfa [ 1 ] [ 3 ] = 2 NEW_LINE dfa [ 1 ] [ 4 ] = 6 NEW_LINE dfa [ 3 ] [ 0 ] = 4 NEW_LINE dfa [ 4 ] [ 0 ] = 4 NEW_LINE dfa [ 4 ] [ 3 ] = 5 NEW_LINE dfa [ 4 ] [ 4 ] = 6 NEW_LINE dfa [ 6 ] [ 0 ] = 8 NEW_LINE dfa [ 6 ] [ 1 ] = 7 NEW_LINE dfa [ 7 ] [ 0 ] = 8 NEW_LINE dfa [ 8 ] [ 0 ] = 8 NEW_LINE dfa [ 8 ] [ 3 ] = 9 NEW_LINE DEDENT def buildDFA ( ) : NEW_LINE INDENT global dfa NEW_LINE for i in range ( 11 ) : NEW_LINE INDENT for j in range ( 5 ) : NEW_LINE INDENT dfa [ i ] [ j ] = 10 NEW_LINE DEDENT DEDENT makeDFA ( ) NEW_LINE DEDENT def checkDFA ( s ) : NEW_LINE INDENT buildDFA ( ) NEW_LINE currentstate = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] in digits ) : NEW_LINE INDENT currentstate = dfa [ currentstate ] [ 0 ] NEW_LINE DEDENT elif ( s [ i ] in sign ) : NEW_LINE INDENT currentstate = dfa [ currentstate ] [ 1 ] NEW_LINE DEDENT elif ( s [ i ] in dot ) : NEW_LINE INDENT currentstate = dfa [ currentstate ] [ 2 ] NEW_LINE DEDENT elif ( s [ i ] in ex ) : NEW_LINE INDENT currentstate = dfa [ currentstate ] [ 4 ] NEW_LINE DEDENT else : NEW_LINE INDENT currentstate = dfa [ currentstate ] [ 3 ] NEW_LINE DEDENT DEDENT if ( currentstate == 1 or currentstate == 4 or currentstate == 8 ) : NEW_LINE INDENT print ( " Unsigned ▁ integer " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ an ▁ unsigned ▁ integer " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "1729" NEW_LINE checkDFA ( S ) NEW_LINE DEDENT
Convert a Mobile Numeric Keypad sequence to equivalent sentence | Function to convert mobile numeric keypad sequence into its equivalent string ; Store the mobile keypad mappings ; Traverse the str1ing str1 ; If the current character is ' . ' , then continue to the next iteration ; Stores the number of continuous clicks ; Iterate a loop to find the count of same characters ; 2 , 3 , 4 , 5 , 6 and 8 keys will have maximum of 3 letters ; 7 and 9 keys will have maximum of 4 keys ; Handle the end condition ; Check if the current pressed key is 7 or 9 ; Else , the key pressed is either 2 , 3 , 4 , 5 , 6 or 8 ; Driver Code
def printSentence ( str1 ) : NEW_LINE INDENT nums = [ " " , " " , " ABC " , " DEF " , " GHI " , " JKL " , " MNO " , " PQRS " , " TUV " , " WXYZ " ] NEW_LINE i = 0 NEW_LINE while ( i < len ( str1 ) ) : NEW_LINE INDENT if ( str1 [ i ] == ' . ' ) : NEW_LINE INDENT i += 1 NEW_LINE continue NEW_LINE DEDENT count = 0 NEW_LINE while ( i + 1 < len ( str1 ) and str1 [ i + 1 ] and str1 [ i ] == str1 [ i + 1 ] ) : NEW_LINE INDENT if ( count == 2 and ( ( str1 [ i ] >= '2' and str1 [ i ] <= '6' ) or ( str1 [ i ] == '8' ) ) ) : NEW_LINE INDENT break NEW_LINE DEDENT elif ( count == 3 and ( str1 [ i ] == '7' or str1 [ i ] == '9' ) ) : NEW_LINE INDENT break NEW_LINE DEDENT count += 1 NEW_LINE i += 1 NEW_LINE if ( i < len ( str ) ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( str1 [ i ] == '7' or str1 [ i ] == '9' ) : NEW_LINE INDENT print ( nums [ ord ( str1 [ i ] ) - 48 ] [ count % 4 ] , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( nums [ ord ( str1 [ i ] ) - 48 ] [ count % 3 ] , end = " " ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = "234" NEW_LINE printSentence ( str1 ) NEW_LINE DEDENT
Lexicographically largest string with sum of characters equal to N | Function to construct the lexicographically largest string having sum of characters as N ; Stores the resulting string ; Iterate until N is at least 26 ; Append ' z ' to the string ans ; Decrement N by 26 ; Append character at index ( N + ' a ' ) ; Return the resultant string ; Driver Code
def getString ( N ) : NEW_LINE INDENT ans = " " NEW_LINE while ( N >= 26 ) : NEW_LINE INDENT ans += ' z ' NEW_LINE N -= 26 NEW_LINE DEDENT ans += chr ( N + ord ( ' a ' ) - 1 ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 30 NEW_LINE print ( getString ( N ) ) NEW_LINE DEDENT
Length of all prefixes that are also the suffixes of given string | Function to find the length of all prefixes of the given that are also suffixes of the same string ; Stores the prefix string ; Traverse the S ; Add the current character to the prefix string ; Store the suffix string ; Check if both the strings are equal or not ; Driver Code
def countSamePrefixSuffix ( s , n ) : NEW_LINE INDENT prefix = " " NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT prefix += s [ i ] NEW_LINE suffix = s [ n - 1 - i : 2 * n - 2 - i ] NEW_LINE if ( prefix == suffix ) : NEW_LINE INDENT print ( len ( prefix ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " ababababab " NEW_LINE N = len ( S ) NEW_LINE countSamePrefixSuffix ( S , N ) NEW_LINE DEDENT
Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array | Function to count newly created pairs by swapping the first characters of any pairs of strings ; Stores the count all possible pair of strings ; Push all the strings into the Unordered Map ; Generate all possible pairs of strings from the array arr [ ] ; Store the current pair of strings ; Swap the first character ; Check if both string are not present in map ; Print the result ; Driver Code
def countStringPairs ( a , n ) : NEW_LINE INDENT ans = 0 NEW_LINE s = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT s [ a [ i ] ] = s . get ( a [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT p = [ i for i in a [ i ] ] NEW_LINE q = [ j for j in a [ j ] ] NEW_LINE if ( p [ 0 ] != q [ 0 ] ) : NEW_LINE INDENT p [ 0 ] , q [ 0 ] = q [ 0 ] , p [ 0 ] NEW_LINE if ( ( " " . join ( p ) not in s ) and ( " " . join ( q ) not in s ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " good " , " bad " , " food " ] NEW_LINE N = len ( arr ) NEW_LINE countStringPairs ( arr , N ) NEW_LINE DEDENT
Check if it is possible to obtain a Balanced Parenthesis by shifting brackets to either end at most K times | Function to check if a valid parenthesis can be obtained by moving characters to either end at most K number of times ; Base Case 1 ; Count of ' ( ' and ') ; Base Case 2 ; Store the count of moves required to make a valid parenthesis ; Traverse the string ; Increment cnt if opening bracket has occurred ; Otherwise , decrement cnt by 1 ; Decrement cnt by 1 ; If cnt is negative ; Update the cnt ; Increment the ans ; If ans is at most K , then print Yes . Otherwise print No ; Driver Code
def minimumMoves ( s , n , k ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT countOpen = s . count ( ' ( ' ) NEW_LINE countClose = s . count ( ' ) ' ) NEW_LINE if ( countOpen != countClose ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT ans = 0 NEW_LINE cnt = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == ' ( ' ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt -= 1 NEW_LINE if ( cnt < 0 ) : NEW_LINE INDENT cnt = 0 NEW_LINE ans += 1 NEW_LINE DEDENT DEDENT DEDENT if ( ans <= k ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : S = " ) ( " NEW_LINE INDENT K = 1 NEW_LINE minimumMoves ( S , len ( S ) , K ) NEW_LINE DEDENT
Generate a random Binary String of length N | Python3 program for the above approach ; Function to find a random number between 0 or 1 ; Generate the random number ; Return the generated number ; Function to generate a random binary string of length N ; Stores the empty string ; Iterate over the range [ 0 , N - 1 ] ; Store the random number ; Append it to the string ; Print the resulting string ; Driver Code
import random NEW_LINE def findRandom ( ) : NEW_LINE INDENT num = random . randint ( 0 , 1 ) NEW_LINE return num NEW_LINE DEDENT def generateBinaryString ( N ) : NEW_LINE INDENT S = " " NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = findRandom ( ) NEW_LINE S += str ( x ) NEW_LINE DEDENT print ( S ) NEW_LINE DEDENT N = 7 NEW_LINE generateBinaryString ( N ) NEW_LINE
Partition a string into palindromic strings of at least length 2 with every character present in a single string | Function to check if a string can be split into palindromic strings of at least length 2 by including every character exactly once ; Stores the frequency of each character ; Store the frequency of characters with frequencies 1 and even respectively ; Traverse the string s ; Iterate over all the characters ; If the frequency is 1 ; If frequency is even ; Print the result ; Stores the number of characters with frequency equal to 1 that are not part of a palindromic string ; Iterate over all the characters ; If o becomes less than 0 , then break out of the loop ; If frequency of the current character is > 2 and is odd ; Update the value of o ; If a single character is still remaining ; Increment o by 1 ; Set a [ i ] to 1 ; Print the result ; Driver Code
def checkPalindrome ( s ) : NEW_LINE INDENT a = [ 0 ] * 26 NEW_LINE o , e = 0 , 0 NEW_LINE for i in s : NEW_LINE INDENT a [ ord ( i ) - 97 ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( a [ i ] == 1 ) : NEW_LINE INDENT o += 1 NEW_LINE DEDENT elif ( a [ i ] % 2 == 0 and a [ i ] != 0 ) : NEW_LINE INDENT e += ( a [ i ] // 2 ) NEW_LINE DEDENT DEDENT if ( e >= o ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT o = o - e NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( o <= 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( o > 0 and a [ i ] % 2 == 1 and a [ i ] > 2 ) : NEW_LINE INDENT k = o NEW_LINE o = o - a [ i ] // 2 NEW_LINE if ( o > 0 or 2 * k + 1 == a [ i ] ) : NEW_LINE INDENT o += 1 NEW_LINE a [ i ] = 1 NEW_LINE DEDENT DEDENT DEDENT if ( o <= 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " abbbaddzcz " NEW_LINE checkPalindrome ( S ) NEW_LINE DEDENT
Maximize time by replacing ' _ ' in a given 24 Hour format time | Function to find the maximum time possible by replacing each ' _ ' with any digit ; If the first character is '_ ; If s [ 1 ] is ' _ ' or s [ 1 ] is less than 4 ; Update s [ 0 ] as 2 ; Otherwise , update s [ 0 ] = 1 ; If s [ 1 ] is equal to '_ ; If s [ 0 ] is equal to '2 ; Otherwise ; If S [ 3 ] is equal to '_ ; If s [ 4 ] is equal to '_ ; Return the modified string ; Driver Code
def maximumTime ( s ) : NEW_LINE INDENT s = list ( s ) NEW_LINE DEDENT ' NEW_LINE INDENT if ( s [ 0 ] == ' _ ' ) : NEW_LINE INDENT if ( ( s [ 1 ] == ' _ ' ) or ( s [ 1 ] >= '0' and s [ 1 ] < '4' ) ) : NEW_LINE INDENT s [ 0 ] = '2' NEW_LINE DEDENT else : NEW_LINE INDENT s [ 0 ] = '1' NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT if ( s [ 1 ] == ' _ ' ) : NEW_LINE DEDENT ' NEW_LINE INDENT if ( s [ 0 ] == '2' ) : NEW_LINE INDENT s [ 1 ] = '3' NEW_LINE DEDENT else : NEW_LINE INDENT s [ 1 ] = '9' NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( s [ 3 ] == ' _ ' ) : NEW_LINE INDENT s [ 3 ] = '5' NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( s [ 4 ] == ' _ ' ) : NEW_LINE INDENT s [ 4 ] = '9' NEW_LINE DEDENT s = ' ' . join ( s ) NEW_LINE return s NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "0 _ : 4 _ " NEW_LINE print ( maximumTime ( S ) ) NEW_LINE DEDENT
How to create half of the string in uppercase and the other half in lowercase ? |
import math NEW_LINE string1 = ' geeks ▁ for ▁ geeks ' NEW_LINE string1_len = len ( string1 ) NEW_LINE half_string = math . ceil ( string1_len / 2 ) NEW_LINE part_a = ' ' NEW_LINE part_b = ' ' NEW_LINE part_a = string1 [ : half_string ] NEW_LINE new_part_a = part_a . upper ( ) NEW_LINE part_b = string1 [ half_string : string1_len ] NEW_LINE changed_string = new_part_a + part_b NEW_LINE print ( changed_string ) NEW_LINE
Nearest power of 2 of frequencies of each digit of a given number | Python3 program for the above approach ; Function to find the nearest power of 2 for all frequencies in the Map freq ; Traverse the Map ; Calculate log of the current array element ; Find the nearest power of 2 for the current frequency ; Function to find nearest power of 2 for frequency of each digit of num ; Length of string ; Stores the frequency of each character in the string ; Traverse the string S ; Function call to generate nearest power of 2 for each frequency ; Driver Code
from math import log2 , pow NEW_LINE def nearestPowerOfTwoUtil ( freq ) : NEW_LINE INDENT temp = { } NEW_LINE for key , value in freq . items ( ) : NEW_LINE INDENT lg = int ( log2 ( value ) ) NEW_LINE a = int ( pow ( 2 , lg ) ) NEW_LINE b = int ( pow ( 2 , lg + 1 ) ) NEW_LINE if ( ( value - a ) < ( b - value ) ) : NEW_LINE INDENT temp [ ( int ( a ) ) ] = key NEW_LINE DEDENT else : NEW_LINE INDENT temp [ ( int ( b ) ) ] = key NEW_LINE DEDENT DEDENT for key , value in temp . items ( ) : NEW_LINE INDENT print ( value , " - > " , key ) NEW_LINE DEDENT DEDENT def nearestPowerOfTwo ( S ) : NEW_LINE INDENT N = len ( S ) NEW_LINE freq = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( S [ i ] in freq ) : NEW_LINE INDENT freq [ S [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ S [ i ] ] = 1 NEW_LINE DEDENT DEDENT nearestPowerOfTwoUtil ( freq ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = "16333331163" NEW_LINE nearestPowerOfTwo ( N ) NEW_LINE DEDENT
Smallest string divisible by two given strings | Function to calculate GCD of two numbers ; Function to calculate LCM of two numbers ; Function to find the smallest string which is divisible by strings S and T ; Store the length of both strings ; Store LCM of n and m ; Temporary strings to store concatenated strings ; Concatenate s1 ( l / n ) times ; Concatenate t1 ( l / m ) times ; If s1 and t1 are equal ; Otherwise , pr - 1 ; Driver Code
def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def lcm ( a , b ) : NEW_LINE INDENT return ( a // gcd ( a , b ) ) * b NEW_LINE DEDENT def findSmallestString ( s , t ) : NEW_LINE INDENT n , m = len ( s ) , len ( t ) NEW_LINE l = lcm ( n , m ) NEW_LINE s1 , t1 = " " , " " NEW_LINE for i in range ( l // n ) : NEW_LINE INDENT s1 += s NEW_LINE DEDENT for i in range ( l // m ) : NEW_LINE INDENT t1 += t NEW_LINE DEDENT if ( s1 == t1 ) : NEW_LINE INDENT print ( s1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S , T = " baba " , " ba " NEW_LINE findSmallestString ( S , T ) NEW_LINE DEDENT
Minimum substring flips required to convert a Binary String to another | Function to count the minimum number of reversals required to make the given binary strings s1 and s2 same ; Stores the minimum count of reversal of substrings required ; If the length of the strings are not the same then return - 1 ; Iterate over each character ; If s1 [ i ] is not equal to s2 [ i ] ; Iterate until s1 [ i ] != s2 [ i ] ; Increment answer by 1 ; Return the resultant count of reversal of subrequired ; Driver Code ; Function Call
def canMakeSame ( s1 , s2 ) : NEW_LINE INDENT ans = - 1 NEW_LINE if ( len ( s1 ) != len ( s2 ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT N = len ( s1 ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( s1 [ i ] != s2 [ i ] ) : NEW_LINE INDENT while ( i < len ( s1 ) and s1 [ i ] != s2 [ i ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT S1 = "100001" NEW_LINE S2 = "110111" NEW_LINE print ( canMakeSame ( S1 , S2 ) ) NEW_LINE
Count points which are revisited while following the path specified by a given string | Function to find the number of times already visited position is revisited after starting traversal from { X , Y } ; Stores the x and y temporarily ; Stores the number of times an already visited position is revisited ; Initialize hashset ; Insert the starting coordinates ; Traverse over the string ; Update the coordinates according to the current directions ; If the new { X , Y } has been visited before , then increment the count by 1 ; Otherwise ; Insert new { x , y } ; Driver Code
def count ( S , X , Y ) : NEW_LINE INDENT N = len ( S ) NEW_LINE temp_x , temp_y = 0 , 0 NEW_LINE count = 0 NEW_LINE s = { } NEW_LINE s [ ( X , Y ) ] = 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT temp_x = X NEW_LINE temp_y = Y NEW_LINE if ( S [ i ] == ' U ' ) : NEW_LINE INDENT X += 1 NEW_LINE DEDENT elif ( S [ i ] == ' D ' ) : NEW_LINE INDENT X -= 1 NEW_LINE DEDENT elif ( S [ i ] == ' R ' ) : NEW_LINE INDENT Y += 1 NEW_LINE DEDENT else : NEW_LINE INDENT Y -= 1 NEW_LINE DEDENT if ( ( temp_x + X , temp_y + Y ) in s ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT s [ ( temp_x + X , temp_y + Y ) ] = 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " RDDUDL " NEW_LINE X , Y = 0 , 0 NEW_LINE print ( count ( S , X , Y ) ) NEW_LINE DEDENT
Sum of frequencies of characters of a string present in another string | Function to find sum of frequencies of characters of S1 present in S2 ; Insert all characters of string S1 in the set ; Traverse the string S2 ; Check if X is present in bset or not ; Increment count by 1 ; Finally , print the count ; Given strings
def countTotalFrequencies ( S1 , S2 ) : NEW_LINE INDENT bset = set ( S1 ) NEW_LINE count = 0 NEW_LINE for x in S2 : NEW_LINE INDENT if x in bset : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT S1 = " geEksFOR " NEW_LINE S2 = " GeEksforgeEKS " NEW_LINE countTotalFrequencies ( S1 , S2 ) NEW_LINE
Make a given Binary String non | Function to return the length of smallest subsequence required to be removed to make the given string non - decreasing ; Length of the string ; Count of zeros and ones ; Traverse the string ; Count minimum removals to obtain strings of the form "00000 . . . . " or "11111 . . . " ; Increment count ; Remove 1 s and remaining 0 s ; Driver Code
def min_length ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE total_zeros = 0 NEW_LINE total_ones = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT total_zeros += 1 NEW_LINE DEDENT else : NEW_LINE INDENT total_ones += 1 NEW_LINE DEDENT DEDENT ans = min ( total_zeros , total_ones ) NEW_LINE cur_zeros = 0 NEW_LINE cur_ones = 0 NEW_LINE for x in str : NEW_LINE INDENT if ( x == '0' ) : NEW_LINE INDENT cur_zeros += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cur_ones += 1 NEW_LINE DEDENT ans = min ( ans , cur_ones + ( total_zeros - cur_zeros ) ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "10011" NEW_LINE min_length ( str ) NEW_LINE DEDENT
Print all words occurring in a sentence exactly K times | Function to print all the words occurring k times in a string ; Stores the words ; Traverse the list ; Check for count ; Print the word ; Remove from list ; Driver Code ; Given string ; Given value of K ; Function call to find all words occuring K times
def kFreqWords ( S , K ) : NEW_LINE INDENT l = list ( S . split ( " ▁ " ) ) NEW_LINE for i in l : NEW_LINE INDENT if l . count ( i ) == K : NEW_LINE INDENT print ( i ) NEW_LINE l . remove ( i ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " banana ▁ is ▁ in ▁ yellow ▁ and ▁ sun ▁ flower ▁ is ▁ also ▁ in ▁ yellow " NEW_LINE K = 2 NEW_LINE kFreqWords ( S , K ) NEW_LINE DEDENT
Check if substrings from three given strings can be concatenated to form a palindrome | Function to check if substrings from three given strings can be concatenated to form a palindrome ; Mask for S1 and S2 ; Set ( i - ' a ' ) th bit in maskA ; Set ( i - ' a ' ) th bit in maskC ; If the bitwise AND is > 0 ; Driver Code
def make_palindrome ( S1 , S2 , S3 ) : NEW_LINE INDENT maskA , maskC = 0 , 0 NEW_LINE for i in S1 : NEW_LINE INDENT maskA |= ( 1 << ( ord ( i ) - ord ( ' a ' ) ) ) NEW_LINE DEDENT for i in S3 : NEW_LINE INDENT maskC |= ( 1 << ( ord ( i ) - ord ( ' a ' ) ) ) NEW_LINE DEDENT if ( ( maskA & maskC ) > 0 ) : NEW_LINE INDENT return " YES " NEW_LINE DEDENT return " NO " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S1 , S2 , S3 = " adcb " , " bcdb " , " abe " NEW_LINE print ( make_palindrome ( S1 , S2 , S3 ) ) NEW_LINE DEDENT
Length of longest prefix anagram which are common in given two strings | Python3 program for the above approach ; Function to check if two arrays are identical or not ; Iterate over the range [ 0 , SIZE ] ; If frequency any character is not same in both the strings ; Otherwise ; Function to find the maximum length of the required string ; Store the count of characters in str1 ; Store the count of characters in str2 ; Stores the maximum length ; Minimum length of str1 and str2 ; Increment the count of characters of str1 [ i ] in freq1 [ ] by one ; Increment the count of characters of stord ( r2 [ i ] ) in freq2 [ ] by one ; Checks if prefixes are anagram or not ; Finally prthe ans ; Driver Code ; Function Call
SIZE = 26 NEW_LINE def longHelper ( freq1 , freq2 ) : NEW_LINE INDENT for i in range ( 26 ) : NEW_LINE INDENT if ( freq1 [ i ] != freq2 [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def longCommomPrefixAnagram ( s1 , s2 , n1 , n2 ) : NEW_LINE INDENT freq1 = [ 0 ] * 26 NEW_LINE freq2 = [ 0 ] * 26 NEW_LINE ans = 0 NEW_LINE mini_len = min ( n1 , n2 ) NEW_LINE for i in range ( mini_len ) : NEW_LINE INDENT freq1 [ ord ( s1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE freq2 [ ord ( s2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE if ( longHelper ( freq1 , freq2 ) ) : NEW_LINE INDENT ans = i + 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " abaabcdezzwer " NEW_LINE str2 = " caaabbttyh " NEW_LINE N = len ( str1 ) NEW_LINE M = len ( str2 ) NEW_LINE longCommomPrefixAnagram ( str1 , str2 , N , M ) NEW_LINE DEDENT