codeparrot/xlcost-text-to-code huggingface dataset:
Length of the longest substring with equal 1 s and 0 s | Function to check if a contains equal number of one and zeros or not ; Function to find the length of the longest balanced substring ; Driver code ; Function call
def isValid ( p ) : NEW_LINE INDENT n = len ( p ) NEW_LINE c1 = 0 NEW_LINE c0 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( p [ i ] == '0' ) : NEW_LINE INDENT c0 += 1 NEW_LINE DEDENT if ( p [ i ] == '1' ) : NEW_LINE INDENT c1 += 1 NEW_LINE DEDENT DEDENT if ( c0 == c1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def longestSub ( s ) : NEW_LINE INDENT max_len = 0 NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT if ( isValid ( s [ i : j - i + 1 ] ) and max_len < j - i + 1 ) : NEW_LINE INDENT max_len = j - i + 1 NEW_LINE DEDENT DEDENT DEDENT return max_len NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "101001000" NEW_LINE print ( longestSub ( s ) ) NEW_LINE DEDENT
Common characters in n strings | Python3 Program to find all the common characters in n strings ; primary array for common characters we assume all characters are seen before . ; for each strings ; secondary array for common characters Initially marked false ; for every character of ith strings ; if character is present in all strings before , mark it . ; copy whole secondary array into primary ; displaying common characters ; Driver 's Code
MAX_CHAR = 26 NEW_LINE def commonCharacters ( strings , n ) : NEW_LINE INDENT prim = [ True ] * MAX_CHAR NEW_LINE for i in range ( n ) : NEW_LINE INDENT sec = [ False ] * MAX_CHAR NEW_LINE for j in range ( len ( strings [ i ] ) ) : NEW_LINE INDENT if ( prim [ ord ( strings [ i ] [ j ] ) - ord ( ' a ' ) ] ) : NEW_LINE INDENT sec [ ord ( strings [ i ] [ j ] ) - ord ( ' a ' ) ] = True NEW_LINE DEDENT DEDENT for i in range ( MAX_CHAR ) : NEW_LINE INDENT prim [ i ] = sec [ i ] NEW_LINE DEDENT DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( prim [ i ] ) : NEW_LINE INDENT print ( " % c ▁ " % ( i + ord ( ' a ' ) ) , end = " " ) NEW_LINE DEDENT DEDENT DEDENT strings = [ " geeksforgeeks " , " gemkstones " , " acknowledges " , " aguelikes " ] NEW_LINE n = len ( strings ) NEW_LINE commonCharacters ( strings , n ) NEW_LINE
Number of positions where a letter can be inserted such that a string becomes palindrome | Function to check if the string is palindrome ; to know the length of string ; if the given string is a palindrome ( Case - I ) ; Sub - case - III ) ; if ( n % 2 == 0 ) : if the length is even ; count = 2 * count + 1 sub - case - I ; count = 2 * count + 2 sub - case - II ; insertion point ; Case - I ; Case - II ; Driver code
def isPalindrome ( s , i , j ) : NEW_LINE INDENT p = j NEW_LINE for k in range ( i , p + 1 ) : NEW_LINE INDENT if ( s [ k ] != s [ p ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT p -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def countWays ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE count = 0 NEW_LINE if ( isPalindrome ( s , 0 , n - 1 ) ) : NEW_LINE INDENT for i in range ( n // 2 , n ) : NEW_LINE INDENT if ( s [ i ] == s [ i + 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT count += 1 NEW_LINE DEDENT else : NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( n // 2 ) : NEW_LINE INDENT if ( s [ i ] != s [ n - 1 - i ] ) : NEW_LINE INDENT j = n - 1 - i NEW_LINE if ( isPalindrome ( s , i , n - 2 - i ) ) : NEW_LINE INDENT for k in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ k ] != s [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT count += 1 NEW_LINE DEDENT count += 1 NEW_LINE DEDENT if ( isPalindrome ( s , i + 1 , n - 1 - i ) ) : NEW_LINE INDENT for k in range ( n - i , n ) : NEW_LINE INDENT if ( s [ k ] != s [ i ] ) : NEW_LINE INDENT break NEW_LINE DEDENT count += 1 NEW_LINE DEDENT count += 1 NEW_LINE DEDENT break NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abca " NEW_LINE print ( countWays ( s ) ) NEW_LINE DEDENT
Count of substrings of a binary string containing K ones | method returns total number of substring having K ones ; initialize index having zero sum as 1 ; loop over binary characters of string ; update countOfOne variable with value of ith character ; if value reaches more than K , then update result ; add frequency of indices , having sum ( current sum - K ) , to the result ; update frequency of one 's count ; Driver code
def countOfSubstringWithKOnes ( s , K ) : NEW_LINE INDENT N = len ( s ) NEW_LINE res = 0 NEW_LINE countOfOne = 0 NEW_LINE freq = [ 0 for i in range ( N + 1 ) ] NEW_LINE freq [ 0 ] = 1 NEW_LINE for i in range ( 0 , N , 1 ) : NEW_LINE INDENT countOfOne += ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( countOfOne >= K ) : NEW_LINE INDENT res += freq [ countOfOne - K ] NEW_LINE DEDENT freq [ countOfOne ] += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "10010" NEW_LINE K = 1 NEW_LINE print ( countOfSubstringWithKOnes ( s , K ) ) NEW_LINE DEDENT
Generate two output strings depending upon occurrence of character in input string . | Python3 program to print two strings made of character occurring once and multiple times ; function to print two strings generated from single string one with characters occurring onces other with character occurring multiple of times ; initialize hashtable with zero entry ; perform hashing for input string ; generate string ( str1 ) consisting char occurring once and string ( str2 ) consisting char occurring multiple times ; print both strings ; Driver Code
MAX_CHAR = 256 NEW_LINE def printDuo ( string ) : NEW_LINE INDENT countChar = [ 0 for i in range ( MAX_CHAR ) ] NEW_LINE n = len ( string ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT countChar [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT str1 = " " NEW_LINE str2 = " " NEW_LINE for i in range ( MAX_CHAR ) : NEW_LINE INDENT if ( countChar [ i ] > 1 ) : NEW_LINE INDENT str2 = str2 + chr ( i + ord ( ' a ' ) ) NEW_LINE DEDENT elif ( countChar [ i ] == 1 ) : NEW_LINE INDENT str1 = str1 + chr ( i + ord ( ' a ' ) ) NEW_LINE DEDENT DEDENT print ( " String ▁ with ▁ characters ▁ occurring ▁ once : " ,   " " , str1 ) NEW_LINE print ( " String ▁ with ▁ characters ▁ occurring " , " multiple ▁ times : " ,   " " , str2 ) NEW_LINE DEDENT string = " lovetocode " NEW_LINE printDuo ( string ) NEW_LINE
Next higher palindromic number using the same set of digits | function to reverse the digits in the range i to j in 'num ; function to find next higher palindromic number using the same set of digits ; if length of number is less than '3' then no higher palindromic number can be formed ; find the index of last digit in the 1 st half of 'num ; Start from the ( mid - 1 ) th digit and find the first digit that is smaller than the digit next to it . ; If no such digit is found , then all digits are in descending order which means there cannot be a greater palindromic number with same set of digits ; Find the smallest digit on right side of ith digit which is greater than num [ i ] up to index 'mid ; swap num [ i ] with num [ smallest ] ; as the number is a palindrome , the same swap of digits should be performed in the 2 nd half of 'num ; reverse digits in the range ( i + 1 ) to mid ; if n is even , then reverse digits in the range mid + 1 to n - i - 2 ; else if n is odd , then reverse digits in the range mid + 2 to n - i - 2 ; required next higher palindromic number ; Driver Code
' NEW_LINE def reverse ( num , i , j ) : NEW_LINE INDENT while ( i < j ) : NEW_LINE INDENT temp = num [ i ] NEW_LINE num [ i ] = num [ j ] NEW_LINE num [ j ] = temp NEW_LINE i = i + 1 NEW_LINE j = j - 1 NEW_LINE DEDENT DEDENT def nextPalin ( num , n ) : NEW_LINE INDENT if ( n <= 3 ) : NEW_LINE INDENT print " Not ▁ Possible " NEW_LINE return NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT mid = n / 2 - 1 NEW_LINE i = mid - 1 NEW_LINE while i >= 0 : NEW_LINE INDENT if ( num [ i ] < num [ i + 1 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT i = i - 1 NEW_LINE DEDENT if ( i < 0 ) : NEW_LINE INDENT print " Not ▁ Possible " NEW_LINE return NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT smallest = i + 1 NEW_LINE j = i + 2 NEW_LINE while j <= mid : NEW_LINE INDENT if ( num [ j ] > num [ i ] and num [ j ] < num [ smallest ] ) : NEW_LINE INDENT smallest = j NEW_LINE DEDENT j = j + 1 NEW_LINE DEDENT temp = num [ i ] NEW_LINE num [ i ] = num [ smallest ] NEW_LINE num [ smallest ] = temp NEW_LINE DEDENT ' NEW_LINE INDENT temp = num [ n - i - 1 ] NEW_LINE num [ n - i - 1 ] = num [ n - smallest - 1 ] NEW_LINE num [ n - smallest - 1 ] = temp NEW_LINE reverse ( num , i + 1 , mid ) NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT reverse ( num , mid + 1 , n - i - 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT reverse ( num , mid + 2 , n - i - 2 ) NEW_LINE DEDENT result = ' ' . join ( num ) NEW_LINE print " Next ▁ Palindrome : ▁ " , result NEW_LINE DEDENT st = "4697557964" NEW_LINE num = list ( st ) NEW_LINE n = len ( st ) NEW_LINE nextPalin ( num , n ) NEW_LINE
Print N | function to generate n digit numbers ; if number generated ; Append 1 at the current number and reduce the remaining places by one ; If more ones than zeros , append 0 to the current number and reduce the remaining places by one ; Driver Code ; Function call
def printRec ( number , extraOnes , remainingPlaces ) : NEW_LINE INDENT if ( 0 == remainingPlaces ) : NEW_LINE INDENT print ( number , end = " ▁ " ) NEW_LINE return NEW_LINE DEDENT printRec ( number + "1" , extraOnes + 1 , remainingPlaces - 1 ) NEW_LINE if ( 0 < extraOnes ) : NEW_LINE INDENT printRec ( number + "0" , extraOnes - 1 , remainingPlaces - 1 ) NEW_LINE DEDENT DEDENT def printNums ( n ) : NEW_LINE INDENT str = " " NEW_LINE printRec ( str , 0 , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE printNums ( n ) NEW_LINE DEDENT
Longest Common Substring in an Array of Strings | function to find the stem ( longestcommon substring ) from the string array ; Determine size of the array ; Take first word from array as reference ; generating all possible substrings of our reference string arr [ 0 ] i . e s ; Check if the generated stem is common to all words ; If current substring is present in all strings and its length is greater than current result ; Driver Code ; Function call
def findstem ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE s = arr [ 0 ] NEW_LINE l = len ( s ) NEW_LINE res = " " NEW_LINE for i in range ( l ) : NEW_LINE INDENT for j in range ( i + 1 , l + 1 ) : NEW_LINE INDENT stem = s [ i : j ] NEW_LINE k = 1 NEW_LINE for k in range ( 1 , n ) : NEW_LINE INDENT if stem not in arr [ k ] : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( k + 1 == n and len ( res ) < len ( stem ) ) : NEW_LINE INDENT res = stem NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " grace " , " graceful " , " disgraceful " , " gracefully " ] NEW_LINE stems = findstem ( arr ) NEW_LINE print ( stems ) NEW_LINE DEDENT
Make a string from another by deletion and rearrangement of characters | Python 3 program to find if it is possible to make a string from characters present in other string . ; Returns true if it is possible to make s1 from characters present in s2 . ; Count occurrences of all characters present in s2 . . ; For every character , present in s1 , reduce its count if count is more than 0. ; Driver code
MAX_CHAR = 256 NEW_LINE def isPossible ( s1 , s2 ) : NEW_LINE INDENT count = [ 0 ] * MAX_CHAR NEW_LINE for i in range ( len ( s2 ) ) : NEW_LINE INDENT count [ ord ( s2 [ i ] ) ] += 1 NEW_LINE DEDENT for i in range ( len ( s1 ) ) : NEW_LINE INDENT if ( count [ ord ( s1 [ i ] ) ] == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT count [ ord ( s1 [ i ] ) ] -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " GeeksforGeeks " NEW_LINE s2 = " rteksfoGrdsskGeggehes " NEW_LINE if ( isPossible ( s1 , s2 ) ) : NEW_LINE INDENT print ( " Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT DEDENT
Next higher number using atmost one swap operation | function to find the next higher number using atmost one swap operation ; to store the index of the largest digit encountered so far from the right ; to store the index of rightmost digit which has a digit greater to it on its right side ; finding the ' index ' of rightmost digit which has a digit greater to it on its right side ; required digit found , store its ' index ' and break ; if no such digit is found which has a larger digit on its right side ; to store the index of the smallest digit greater than the digit at ' index ' and right to it ; finding the index of the smallest digit greater than the digit at ' index ' and right to it ; swapping the digits ; required number ; Driver program to test above
def nextHighUsingAtMostOneSwap ( st ) : NEW_LINE INDENT num = list ( st ) NEW_LINE l = len ( num ) NEW_LINE posRMax = l - 1 NEW_LINE index = - 1 NEW_LINE i = l - 2 NEW_LINE while i >= 0 : NEW_LINE INDENT if ( num [ i ] >= num [ posRMax ] ) : NEW_LINE INDENT posRMax = i NEW_LINE DEDENT else : NEW_LINE INDENT index = i NEW_LINE break NEW_LINE DEDENT i = i - 1 NEW_LINE DEDENT if ( index == - 1 ) : NEW_LINE INDENT return " Not ▁ Possible " NEW_LINE DEDENT greatSmallDgt = - 1 NEW_LINE i = l - 1 NEW_LINE while i > index : NEW_LINE INDENT if ( num [ i ] > num [ index ] ) : NEW_LINE INDENT if ( greatSmallDgt == - 1 ) : NEW_LINE INDENT greatSmallDgt = i NEW_LINE DEDENT elif ( num [ i ] <= num [ greatSmallDgt ] ) : NEW_LINE INDENT greatSmallDgt = i NEW_LINE DEDENT DEDENT i = i - 1 NEW_LINE DEDENT temp = num [ index ] NEW_LINE num [ index ] = num [ greatSmallDgt ] ; NEW_LINE num [ greatSmallDgt ] = temp ; NEW_LINE return ' ' . join ( num ) NEW_LINE DEDENT num = "218765" NEW_LINE print " Original ▁ number : ▁ " , num NEW_LINE print " Next ▁ higher ▁ number : ▁ " , nextHighUsingAtMostOneSwap ( num ) NEW_LINE
Longest substring of vowels | Python3 program to find the longest substring of vowels . ; Increment current count if s [ i ] is vowel ; check previous value is greater then or not ; Driver code
def isVowel ( c ) : NEW_LINE INDENT return ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) NEW_LINE DEDENT def longestVowel ( s ) : NEW_LINE INDENT count , res = 0 , 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( isVowel ( s [ i ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT res = max ( res , count ) NEW_LINE count = 0 NEW_LINE DEDENT DEDENT return max ( res , count ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " theeare " NEW_LINE print ( longestVowel ( s ) ) NEW_LINE DEDENT
Number of substrings with count of each character as k | Python3 program to count number of substrings with counts of distinct characters as k . ; Returns true if all values in freq [ ] are either 0 or k . ; Returns count of substrings where frequency of every present character is k ; Pick a starting point ; Initialize all frequencies as 0 for this starting point ; One by one pick ending points ; Increment frequency of current char ; If frequency becomes more than k , we can 't have more substrings  starting with i ; If frequency becomes k , then check other frequencies as well ; Driver Code
MAX_CHAR = 26 NEW_LINE def check ( freq , k ) : NEW_LINE INDENT for i in range ( 0 , MAX_CHAR ) : NEW_LINE INDENT if ( freq [ i ] and freq [ i ] != k ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def substrings ( s , k ) : NEW_LINE INDENT for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT freq = [ 0 ] * MAX_CHAR NEW_LINE for j in range ( i , len ( s ) ) : NEW_LINE INDENT index = ord ( s [ j ] ) - ord ( ' a ' ) NEW_LINE freq [ index ] += 1 NEW_LINE if ( freq [ index ] > k ) : NEW_LINE INDENT break NEW_LINE DEDENT elif ( freq [ index ] == k and check ( freq , k ) == True ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " aabbcc " NEW_LINE k = 2 NEW_LINE print ( substrings ( s , k ) ) NEW_LINE s = " aabbc " ; NEW_LINE k = 2 ; NEW_LINE print ( substrings ( s , k ) ) NEW_LINE DEDENT
Number of substrings with count of each character as k |
from collections import defaultdict NEW_LINE def have_same_frequency ( freq : defaultdict , k : int ) : NEW_LINE INDENT return all ( [ freq [ i ] == k or freq [ i ] == 0 for i in freq ] ) NEW_LINE DEDENT def count_substrings ( s : str , k : int ) -> int : NEW_LINE INDENT count = 0 NEW_LINE distinct = len ( set ( [ i for i in s ] ) ) NEW_LINE for length in range ( 1 , distinct + 1 ) : NEW_LINE INDENT window_length = length * k NEW_LINE freq = defaultdict ( int ) NEW_LINE window_start = 0 NEW_LINE window_end = window_start + window_length - 1 NEW_LINE for i in range ( window_start , min ( window_end + 1 , len ( s ) ) ) : NEW_LINE INDENT freq [ s [ i ] ] += 1 NEW_LINE DEDENT while window_end < len ( s ) : NEW_LINE INDENT if have_same_frequency ( freq , k ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT freq [ s [ window_start ] ] -= 1 NEW_LINE window_start += 1 NEW_LINE window_end += 1 NEW_LINE if window_end < len ( s ) : NEW_LINE INDENT freq [ s [ window_end ] ] += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " aabbcc " NEW_LINE k = 2 NEW_LINE print ( count_substrings ( s , k ) ) NEW_LINE s = " aabbc " NEW_LINE k = 2 NEW_LINE print ( count_substrings ( s , k ) ) NEW_LINE DEDENT
Frequency of a string in an array of strings | Python3 program to count number of times a string appears in an array of strings ; To store number of times a string is present . It is 0 is string is not present ; function to insert a string into the Trie ; calculation ascii value ; If the given node is not already present in the Trie than create the new node ; Returns count of occurrences of s in Trie ; inserting in Trie ; searching the strings in Trie ; Driver code
MAX_CHAR = 26 NEW_LINE class Trie : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . cnt = 0 NEW_LINE self . node = [ None for i in range ( MAX_CHAR ) ] NEW_LINE DEDENT DEDENT def insert ( root , s ) : NEW_LINE INDENT temp = root NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT index = ord ( s [ i ] ) - ord ( ' a ' ) NEW_LINE if ( not root . node [ index ] ) : NEW_LINE INDENT root . node [ index ] = Trie ( ) NEW_LINE DEDENT root = root . node [ index ] NEW_LINE DEDENT root . cnt += 1 NEW_LINE return temp NEW_LINE DEDENT def search ( root , s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT index = ord ( s [ i ] ) - ord ( ' a ' ) NEW_LINE if ( not root . node [ index ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT root = root . node [ index ] NEW_LINE DEDENT return root . cnt NEW_LINE DEDENT def answerQueries ( arr , n , q , m ) : NEW_LINE INDENT root = Trie ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT root = insert ( root , arr [ i ] ) NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT print ( search ( root , q [ i ] ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " aba " , " baba " , " aba " , " xzxb " ] NEW_LINE n = len ( arr ) NEW_LINE q = [ " aba " , " xzxb " , " ab " ] NEW_LINE m = len ( q ) NEW_LINE answerQueries ( arr , n , q , m ) NEW_LINE DEDENT
Longest subsequence where every character appears at | Python3 program to Find longest subsequence where every character appears at - least k times ; Count frequencies of all characters ; Traverse given string again and print all those characters whose frequency is more than or equal to k . ; Driver code
MAX_CHARS = 26 NEW_LINE def longestSubseqWithK ( str , k ) : NEW_LINE INDENT n = len ( str ) NEW_LINE freq = [ 0 ] * MAX_CHARS NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] >= k ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeksforgeeks " NEW_LINE k = 2 NEW_LINE longestSubseqWithK ( str , k ) NEW_LINE DEDENT
Generating distinct subsequences of a given string in lexicographic order | Finds and stores result in st for a given string s . ; If current string is not already present . ; Traverse current string , one by one remove every character and recur . ; Driver Code
def generate ( st , s ) : NEW_LINE INDENT if len ( s ) == 0 : NEW_LINE INDENT return NEW_LINE DEDENT if s not in st : NEW_LINE INDENT st . add ( s ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT t = list ( s ) . copy ( ) NEW_LINE t . remove ( s [ i ] ) NEW_LINE t = ' ' . join ( t ) NEW_LINE generate ( st , t ) NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " xyz " NEW_LINE st = set ( ) NEW_LINE generate ( st , s ) NEW_LINE for i in st : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT DEDENT
Recursive solution to count substrings with same first and last characters | Function to count substrings with same first and last characters ; base cases ; driver code
def countSubstrs ( str , i , j , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = ( countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ) NEW_LINE if ( str [ i ] == str [ j ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT str = " abcab " NEW_LINE n = len ( str ) NEW_LINE print ( countSubstrs ( str , 0 , n - 1 , n ) ) NEW_LINE
Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character | Counts the no of manipulations required ; store the count of character ; iterate though the first String and update count ; iterate through the second string update char_count . if character is not found in char_count then increase count ; Driver code
def countManipulations ( s1 , s2 ) : NEW_LINE INDENT count = 0 NEW_LINE char_count = [ 0 ] * 26 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT char_count [ i ] = 0 NEW_LINE DEDENT for i in range ( len ( s1 ) ) : NEW_LINE INDENT char_count [ ord ( s1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( len ( s2 ) ) : NEW_LINE INDENT char_count [ ord ( s2 [ i ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if char_count [ i ] != 0 : NEW_LINE INDENT count += abs ( char_count [ i ] ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " ddcf " NEW_LINE s2 = " cedk " NEW_LINE print ( countManipulations ( s1 , s2 ) ) NEW_LINE DEDENT
Least number of manipulations needed to ensure two strings have identical characters | Python3 program to count least number of manipulations to have two strings set of same characters ; return the count of manipulations required ; count the number of different characters in both strings ; check the difference in characters by comparing count arrays ; Driver Code
MAX_CHAR = 26 NEW_LINE def leastCount ( s1 , s2 , n ) : NEW_LINE INDENT count1 = [ 0 ] * MAX_CHAR NEW_LINE count2 = [ 0 ] * MAX_CHAR NEW_LINE for i in range ( n ) : NEW_LINE INDENT count1 [ ord ( s1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE count2 [ ord ( s2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( MAX_CHAR ) : NEW_LINE INDENT if ( count1 [ i ] != 0 ) : NEW_LINE INDENT res += abs ( count1 [ i ] - count2 [ i ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " abc " NEW_LINE s2 = " cdd " NEW_LINE l = len ( s1 ) NEW_LINE res = leastCount ( s1 , s2 , l ) NEW_LINE print ( res ) NEW_LINE DEDENT