codeparrot/xlcost-text-to-code huggingface dataset:
Longest Repeating Subsequence | This function mainly returns LCS ( str , str ) with a condition that same characters at same index are not considered . ; Create and initialize DP table ; Fill dp table ( similar to LCS loops ) ; If characters match and indexes are not same ; If characters do not match ; Driver Program
def findLongestRepeatingSubSeq ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( str [ i - 1 ] == str [ j - 1 ] and i != j ) : NEW_LINE INDENT dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ n ] [ n ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " aabb " NEW_LINE print ( " The ▁ length ▁ of ▁ the ▁ largest ▁ subsequence ▁ that ▁ repeats ▁ itself ▁ is ▁ : ▁ " , findLongestRepeatingSubSeq ( str ) ) NEW_LINE DEDENT
Count total number of N digit numbers such that the difference between sum of even and odd digits is 1 | Memoization based recursive function to count numbers with even and odd digit sum difference as 1. This function considers leading zero as a digit ; Base Case ; If current subproblem is already computed ; Initialize result ; If the current digit is odd , then add it to odd sum and recur ; Add to even sum and recur ; Store current result in lookup table and return the same ; This is mainly a wrapper over countRec . It explicitly handles leading digit and calls countRec ( ) for remaining digits . ; Initialize number digits considered so far ; Initialize all entries of lookup table ; Initialize final answer ; Initialize even and odd sums ; Explicitly handle first digit and call recursive function countRec for remaining digits . Note that the first digit is considered as even digit ; Driver Code ; A lookup table used for memoization .
def countRec ( digits , esum , osum , isOdd , n ) : NEW_LINE INDENT if digits == n : NEW_LINE INDENT return ( esum - osum == 1 ) NEW_LINE DEDENT if lookup [ digits ] [ esum ] [ osum ] [ isOdd ] != - 1 : NEW_LINE INDENT return lookup [ digits ] [ esum ] [ osum ] [ isOdd ] NEW_LINE DEDENT ans = 0 NEW_LINE if isOdd : NEW_LINE INDENT for i in range ( 10 ) : NEW_LINE INDENT ans += countRec ( digits + 1 , esum , osum + i , False , n ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( 10 ) : NEW_LINE INDENT ans += countRec ( digits + 1 , esum + i , osum , True , n ) NEW_LINE DEDENT DEDENT lookup [ digits ] [ esum ] [ osum ] [ isOdd ] = ans NEW_LINE return ans NEW_LINE DEDENT def finalCount ( n ) : NEW_LINE INDENT global lookup NEW_LINE digits = 0 NEW_LINE lookup = [ [ [ [ - 1 , - 1 ] for i in range ( 500 ) ] for j in range ( 500 ) ] for k in range ( 50 ) ] NEW_LINE ans = 0 NEW_LINE esum = 0 NEW_LINE osum = 0 NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT ans += countRec ( digits + 1 , esum + i , osum , True , n ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT lookup = [ ] NEW_LINE n = 3 NEW_LINE print ( " Count ▁ of ▁ % d ▁ digit ▁ numbers ▁ is ▁ % d " % ( n , finalCount ( n ) ) ) NEW_LINE DEDENT
Count all possible paths from top left to bottom right of a mXn matrix | Returns count of possible paths to reach cell at row number m and column number n from the topmost leftmost cell ( cell at 1 , 1 ) ; Create a 1D array to store results of subproblems ; Driver Code
def numberOfPaths ( p , q ) : NEW_LINE INDENT dp = [ 1 for i in range ( q ) ] NEW_LINE for i in range ( p - 1 ) : NEW_LINE INDENT for j in range ( 1 , q ) : NEW_LINE INDENT dp [ j ] += dp [ j - 1 ] NEW_LINE DEDENT DEDENT return dp [ q - 1 ] NEW_LINE DEDENT print ( numberOfPaths ( 3 , 3 ) ) NEW_LINE
Longest Arithmetic Progression | DP | Returns length of the longest AP subset in a given set ; Driver program to test above function
class Solution : NEW_LINE INDENT def Solve ( self , A ) : NEW_LINE INDENT ans = 2 NEW_LINE n = len ( A ) NEW_LINE if n <= 2 : NEW_LINE INDENT return n NEW_LINE DEDENT llap = [ 2 ] * n NEW_LINE A . sort ( ) NEW_LINE for j in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT i = j - 1 NEW_LINE k = j + 1 NEW_LINE while ( i >= 0 and k < n ) : NEW_LINE INDENT if A [ i ] + A [ k ] == 2 * A [ j ] : NEW_LINE INDENT llap [ j ] = max ( llap [ k ] + 1 , llap [ j ] ) NEW_LINE ans = max ( ans , llap [ j ] ) NEW_LINE i -= 1 NEW_LINE k += 1 NEW_LINE DEDENT elif A [ i ] + A [ k ] < 2 * A [ j ] : NEW_LINE INDENT k += 1 NEW_LINE DEDENT else : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT obj = Solution ( ) NEW_LINE a = [ 9 , 4 , 7 , 2 , 10 ] NEW_LINE print ( obj . Solve ( a ) ) NEW_LINE
Minimum numbers to be appended such that mean of Array is equal to 1 | Function to calculate minimum Number of operations ; Storing sum of array arr [ ] ; Driver Code ; Function Call
def minumumOperation ( N , arr ) : NEW_LINE INDENT sum_arr = sum ( arr ) NEW_LINE if sum_arr >= N : NEW_LINE INDENT print ( sum_arr - N ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT DEDENT N = 4 NEW_LINE arr = [ 8 , 4 , 6 , 2 ] NEW_LINE minumumOperation ( N , arr ) NEW_LINE
Find Nth term of series 1 , 4 , 15 , 72 , 420. . . | Function to find factorial of N with recursion ; base condition ; use recursion ; calculate Nth term of series ; Driver code
def factorial ( N ) : NEW_LINE INDENT if N == 0 or N == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return N * factorial ( N - 1 ) NEW_LINE DEDENT def nthTerm ( N ) : NEW_LINE INDENT return ( factorial ( N ) * ( N + 2 ) // 2 ) NEW_LINE DEDENT N = 6 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE
Check if a string is substring of another | Python program for the above approach ; Iterate from 0 to Len - 1 ; Driver code
def Substr ( Str , target ) : NEW_LINE INDENT t = 0 NEW_LINE Len = len ( Str ) NEW_LINE i = 0 NEW_LINE for i in range ( Len ) : NEW_LINE INDENT if ( t == len ( target ) ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( Str [ i ] == target [ t ] ) : NEW_LINE INDENT t += 1 NEW_LINE DEDENT else : NEW_LINE INDENT t = 0 NEW_LINE DEDENT DEDENT if ( t < len ( target ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ( i - t ) NEW_LINE DEDENT DEDENT print ( Substr ( " GeeksForGeeks " , " Fr " ) ) NEW_LINE print ( Substr ( " GeeksForGeeks " , " For " ) ) NEW_LINE
Count anagrams having first character as a consonant and no pair of consonants or vowels placed adjacently | ''Python 3 program for the above approach include <bits/stdc++.h> ; ''Function to compute factorials till N ; '' Iterate in the range [1, N] ; '' Update ans to ans*i ; ''Store the value of ans  in fac[i] ; ''Function to check whether the current character is a vowel or not ; ''Function to count the number of anagrams of S satisfying the given condition ; '' Store the factorials upto N ; '' Function Call to generate  all factorials upto n ; '' Create a hashmap to store  frequencies of all characters ; '' Store the count of  vowels and consonants ; '' Iterate through all  characters in the string ; ''Update the frequency  of current character ; ''Check if the character  is vowel or consonant ; '' Check if C==V+1 or C==V ; ''Store the denominator ; '' Calculate the denominator of the expression ; '' Multiply denominator by factorial of counts of all letters ; '' Store the numerator ; '' Store the answer by dividing  numerator by denominator ; '' Print the answer ; ''Otherwise, print 0 ; ''Driver Code
mod = 1000000007 NEW_LINE N = 1000001 NEW_LINE fac = { } NEW_LINE def Precomputefact ( ) : NEW_LINE INDENT global fac NEW_LINE DEDENT ans = 1 NEW_LINE for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT ans = ( ans * i ) % mod NEW_LINE DEDENT fac [ i ] = ans NEW_LINE INDENT return NEW_LINE DEDENT def isVowel ( a ) : NEW_LINE INDENT if ( a == ' A ' or a == ' E ' or a == ' I ' or a == ' O ' or a == ' U ' ) : NEW_LINE DEDENT return True NEW_LINE INDENT else : NEW_LINE DEDENT return False NEW_LINE def countAnagrams ( s , n ) : NEW_LINE global fac NEW_LINE INDENT Precomputefact ( ) NEW_LINE count = { } NEW_LINE DEDENT vo = 0 NEW_LINE co = 0 NEW_LINE for i in range ( n ) : NEW_LINE if s [ i ] in count : NEW_LINE INDENT count [ s [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count [ s [ i ] ] = 1 NEW_LINE DEDENT if ( isVowel ( s [ i ] ) ) : NEW_LINE INDENT vo += 1 NEW_LINE else : NEW_LINE DEDENT co += 1 NEW_LINE INDENT if ( ( co == vo + 1 ) or ( co == vo ) ) : NEW_LINE deno = 1 NEW_LINE DEDENT for key , value in count . items ( ) : NEW_LINE deno = ( deno * fac [ value ] ) % mod NEW_LINE INDENT nume = fac [ co ] % mod NEW_LINE DEDENT nume = ( nume * fac [ vo ] ) % mod NEW_LINE ans = nume // deno NEW_LINE INDENT print ( ans ) NEW_LINE else : NEW_LINE print ( 0 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = " GADO " NEW_LINE l = len ( S ) NEW_LINE countAnagrams ( S , l ) NEW_LINE DEDENT
Modify string by increasing each character by its distance from the end of the word | Function to transform and return the transformed word ; Stores resulting word ; Iterate over the word ; Add the position value to the letter ; Convert it back to character ; Add it to the string ; Function to transform the given string ; Size of string ; Stores resultant string ; Iterate over given string ; End of word is reached ; print ( s [ j : j + i ] ) Append the word ; For the last word ; Driver code ; Given string ; Function call
def util ( sub ) : NEW_LINE INDENT n = len ( sub ) NEW_LINE i = 0 NEW_LINE ret = " " NEW_LINE while i < n : NEW_LINE INDENT t = ( ord ( sub [ i ] ) - 97 ) + n - 1 - i NEW_LINE ch = chr ( t % 26 + 97 ) NEW_LINE ret = ret + ch NEW_LINE i = i + 1 NEW_LINE DEDENT return ret NEW_LINE DEDENT def manipulate ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE res = " " NEW_LINE while i < n : NEW_LINE INDENT if s [ i ] == ' ▁ ' : NEW_LINE INDENT res += util ( s [ j : j + i ] ) NEW_LINE res = res + " ▁ " NEW_LINE j = i + 1 NEW_LINE i = j + 1 NEW_LINE DEDENT else : NEW_LINE INDENT i = i + 1 NEW_LINE DEDENT DEDENT res = res + util ( s [ j : j + i ] ) NEW_LINE print ( res ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " acm ▁ fkz " NEW_LINE manipulate ( s ) NEW_LINE DEDENT
Check if given words are present in a string | Helper Function to the Finding bigString ; Initialize leftBigIdx and rightBigIdx and leftSmallIdx variables ; Iterate until leftBigIdx variable reaches less than or equal to rightBigIdx ; Check if bigString [ leftBigIdx ] is not equal to smallString [ leftSmallIdx ] or Check if bigString [ rightBigIdx ] is not equal to smallString [ rightSmallIdx ] than return false otherwise increment leftBigIdx and leftSmallIdx decrement rightBigIdx and rightSmallIdx ; Function to the bigString ; iterate in the bigString ; Check if length of smallString + i is greater than the length of bigString ; call the function isInBigStringHelper ; Function to the multiStringSearch ; iterate in the smallString ; calling the isInBigString Function ; Driver code ; initialize string ; initialize vector string ; Function call ; Print answers ; Check if ans [ i ] is equal to 1 then Print true otherwise print false
def isInBigStringHelper ( bigString , smallString , startIdx ) : NEW_LINE INDENT leftBigIdx = startIdx NEW_LINE rightBigIdx = startIdx + len ( smallString ) - 1 NEW_LINE leftSmallIdx = 0 NEW_LINE rightSmallIdx = len ( smallString ) - 1 NEW_LINE while ( leftBigIdx <= rightBigIdx ) : NEW_LINE INDENT if ( bigString [ leftBigIdx ] != smallString [ leftSmallIdx ] or bigString [ rightBigIdx ] != smallString [ rightSmallIdx ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT leftBigIdx += 1 NEW_LINE rightBigIdx -= 1 NEW_LINE leftSmallIdx += 1 NEW_LINE rightSmallIdx -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def isInBigString ( bigString , smallString ) : NEW_LINE INDENT for i in range ( len ( bigString ) ) : NEW_LINE INDENT if ( i + len ( smallString ) > len ( bigString ) ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( isInBigStringHelper ( bigString , smallString , i ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def multiStringSearch ( bigString , smallStrings ) : NEW_LINE INDENT solution = [ ] NEW_LINE for smallString in smallStrings : NEW_LINE INDENT solution . append ( isInBigString ( bigString , smallString ) ) NEW_LINE DEDENT return solution NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " this ▁ is ▁ a ▁ big ▁ string " NEW_LINE substr = [ " this " , " yo " , " is " , " a " , " bigger " , " string " , " kappa " ] NEW_LINE ans = multiStringSearch ( str1 , substr ) NEW_LINE for i in range ( len ( ans ) ) : NEW_LINE INDENT if ( ans [ i ] == 1 ) : NEW_LINE INDENT print ( " true " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " false " , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT
Count of lexicographically smaller characters on right | Function to count the smaller characters on the right of index i ; store the length of String ; initialize each elements of arr to zero ; array to store count of smaller characters on the right side of that index ; initialize the variable to store the count of characters smaller than that at index i ; adding the count of characters smaller than index i ; print the count of characters smaller than index i stored in ans array ; Driver Code
def countSmaller ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE arr = [ 0 ] * 26 ; NEW_LINE ans = [ 0 ] * n ; NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT arr [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE ct = 0 ; NEW_LINE for j in range ( ord ( str [ i ] ) - ord ( ' a ' ) ) : NEW_LINE INDENT ct += arr [ j ] ; NEW_LINE DEDENT ans [ i ] = ct ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " edcbaa " ; NEW_LINE countSmaller ( str ) ; NEW_LINE DEDENT
Find the longest sub | Z - algorithm function ; bit update function which updates values from index " idx " to last by value " val " ; Query function in bit ; Driver Code ; BIT array ; Making the z array ; update in the bit array from index z [ i ] by increment of 1 ; if the value in z [ i ] is not equal to ( n - i ) then no need to move further ; queryng for the maximum length substring from bit array
def z_function ( s ) : NEW_LINE INDENT global z , n NEW_LINE n = len ( s ) NEW_LINE z = [ 0 ] * n NEW_LINE l , r = 0 , 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if i <= r : NEW_LINE INDENT z [ i ] = min ( r - i + 1 , z [ i - 1 ] ) NEW_LINE DEDENT while ( i + z [ i ] < n and s [ z [ i ] ] == s [ i + z [ i ] ] ) : NEW_LINE INDENT z [ i ] += 1 NEW_LINE DEDENT if ( i + z [ i ] - 1 > r ) : NEW_LINE INDENT l = i NEW_LINE r = i + z [ i ] - 1 NEW_LINE DEDENT DEDENT return z NEW_LINE DEDENT def update ( idx , val ) : NEW_LINE INDENT global bit NEW_LINE if idx == 0 : NEW_LINE INDENT return NEW_LINE DEDENT while idx <= n : NEW_LINE INDENT bit [ idx ] += val NEW_LINE idx += ( idx & - idx ) NEW_LINE DEDENT DEDENT def pref ( idx ) : NEW_LINE INDENT global bit NEW_LINE ans = 0 NEW_LINE while idx > 0 : NEW_LINE INDENT ans += bit [ idx ] NEW_LINE idx -= ( idx & - idx ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 0 NEW_LINE length = 0 NEW_LINE bit = [ 0 ] * 1000005 NEW_LINE z = [ ] NEW_LINE m = dict ( ) NEW_LINE s = " geeksisforgeeksinplatformgeeks " NEW_LINE z = z_function ( s ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT update ( z [ i ] , 1 ) NEW_LINE DEDENT for i in range ( n - 1 , 1 , - 1 ) : NEW_LINE INDENT if z [ i ] != n - i : NEW_LINE INDENT continue NEW_LINE DEDENT if ( pref ( n ) - pref ( z [ i ] - 1 ) ) >= 2 : NEW_LINE INDENT length = max ( length , z [ i ] ) NEW_LINE DEDENT DEDENT if not length : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( s [ : length ] ) NEW_LINE DEDENT DEDENT
Count of words ending at the given suffix in Java | Function declared to return the count of words in the given sentence that end with the given suffix ; Variable to store count ; split function used to extract words from sentence in form of list ; using for loop with ' in ' to extract elements of list ; returning the count ; Driver Code ; printing the final cde
def endingWith ( str , suff ) : NEW_LINE INDENT c = 0 NEW_LINE wrd = str . split ( " ▁ " ) NEW_LINE for l in wrd : NEW_LINE INDENT if l . endswith ( suff ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT str = " GeeksForGeeks ▁ is ▁ a ▁ computer ▁ science ▁ portal ▁ for ▁ geeks " NEW_LINE suff = " ks " NEW_LINE print ( endingWith ( str , suff ) ) NEW_LINE
Character whose frequency is equal to the sum of frequencies of other characters of the given string | Function that returns true if some character exists in the given string whose frequency is equal to the sum frequencies of other characters of the string ; If string is of odd length ; To store the frequency of each character of the string ; Update the frequencies of the characters ; No such character exists ; Driver code
def isFrequencyEqual ( string , length ) : NEW_LINE INDENT if length % 2 == 1 : NEW_LINE INDENT return False NEW_LINE DEDENT freq = [ 0 ] * 26 NEW_LINE for i in range ( 0 , length ) : NEW_LINE INDENT freq [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 0 , 26 ) : NEW_LINE INDENT if freq [ i ] == length // 2 : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeksforgeeks " NEW_LINE length = len ( string ) NEW_LINE if isFrequencyEqual ( string , length ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Minimum replacements to make adjacent characters unequal in a ternary string | Function to count the number of minimal replacements ; Find the length of the string ; Iterate in the string ; Check if adjacent is similar ; If not the last pair ; Check for character which is not same in i + 1 and i - 1 ; Last pair ; Check for character which is not same in i - 1 index ; Driver Code
def countMinimalReplacements ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE cnt = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( s [ i ] == s [ i - 1 ] ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE if ( i != ( n - 1 ) ) : NEW_LINE INDENT s = list ( s ) NEW_LINE for j in "012" : NEW_LINE INDENT if ( j != s [ i + 1 ] and j != s [ i - 1 ] ) : NEW_LINE INDENT s [ i ] = j NEW_LINE break NEW_LINE DEDENT DEDENT s = ' ' . join ( s ) NEW_LINE DEDENT else : NEW_LINE INDENT s = list ( s ) NEW_LINE for k in "012" : NEW_LINE INDENT if ( k != s [ i - 1 ] ) : NEW_LINE INDENT s [ i ] = k NEW_LINE break NEW_LINE DEDENT DEDENT s = ' ' . join ( s ) NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "201220211" NEW_LINE print ( countMinimalReplacements ( s ) ) NEW_LINE DEDENT
Check whether the frequencies of all the characters in a string are prime or not | Python3 implementation of above approach ; function that returns true if n is prime else false ; 1 is not prime ; check if there is any factor or not ; function that returns true if the frequencies of all the characters of s are prime ; create a map to store the frequencies of characters ; update the frequency ; check whether all the frequencies are prime or not ; Driver code ; if all the frequencies are prime
import math as mt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT i = 2 NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , mt . ceil ( mt . sqrt ( n ) ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def check_frequency ( s ) : NEW_LINE INDENT m = dict ( ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if s [ i ] in m . keys ( ) : NEW_LINE INDENT m [ s [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ s [ i ] ] = 1 NEW_LINE DEDENT DEDENT for ch in m : NEW_LINE INDENT if m [ ch ] > 0 and isPrime ( m [ ch ] ) == False : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE if ( check_frequency ( s ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find the count of substrings in alphabetic order | Function to find number of substrings ; Iterate over string length ; if any two chars are in alphabetic order ; find next char not in order ; return the result ; Driver Code
def findSubstringCount ( str ) : NEW_LINE INDENT result = 0 NEW_LINE n = len ( str ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( ord ( str [ i ] ) + 1 == ord ( str [ i + 1 ] ) ) : NEW_LINE INDENT result += 1 NEW_LINE while ( ord ( str [ i ] ) + 1 == ord ( str [ i + 1 ] ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " alphabet " NEW_LINE print ( findSubstringCount ( str ) ) NEW_LINE DEDENT
First non | '' Python3 implementation to find non repeating character using 1D array and one traversal ; The function returns index of the first non - repeating character in a string . If all characters are repeating then returns INT_MAX ; initialize all character as absent ; After below loop , the value of arr [ x ] is going to be index of of x if x appears only once . Else the value is going to be either - 1 or - 2. ; If this character occurs only once and appears before the current result , then update the result ; Driver prohram to test above function
import math as mt NEW_LINE NO_OF_CHARS = 256 NEW_LINE def firstNonRepeating ( string ) : NEW_LINE INDENT arr = [ - 1 for i in range ( NO_OF_CHARS ) ] NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if arr [ ord ( string [ i ] ) ] == - 1 : NEW_LINE INDENT arr [ ord ( string [ i ] ) ] = i NEW_LINE DEDENT else : NEW_LINE INDENT arr [ ord ( string [ i ] ) ] = - 2 NEW_LINE DEDENT DEDENT res = 10 ** 18 NEW_LINE for i in range ( NO_OF_CHARS ) : NEW_LINE INDENT if arr [ i ] >= 0 : NEW_LINE INDENT res = min ( res , arr [ i ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT string = " geeksforgeeks " NEW_LINE index = firstNonRepeating ( string ) NEW_LINE if index == 10 ** 18 : NEW_LINE INDENT print ( " Either ▁ all ▁ characters ▁ are ▁ repeating ▁ or ▁ string ▁ is ▁ empty " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " First ▁ non - repeating ▁ character ▁ is " , string [ index ] ) NEW_LINE DEDENT
Character replacement after removing duplicates from a string | Function to minimize string ; Duplicate characters are removed ; checks if character has previously occurred or not if not then add it to the minimized string 'mstr ; Utility function to print the minimized , replaced string ; Creating final string by replacing character ; index calculation ; Driver program
def minimize ( string ) : NEW_LINE INDENT mstr = " ▁ " NEW_LINE flagchar = [ 0 ] * 26 NEW_LINE l = len ( string ) NEW_LINE for i in range ( 0 , len ( string ) ) : NEW_LINE INDENT ch = string [ i ] NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if flagchar [ ord ( ch ) - 97 ] == 0 : NEW_LINE INDENT mstr = mstr + ch NEW_LINE flagchar [ ord ( ch ) - 97 ] = 1 NEW_LINE DEDENT DEDENT def replaceMinimizeUtil ( string ) : NEW_LINE INDENT finalStr = " " NEW_LINE l = len ( string ) NEW_LINE for i in range ( 0 , len ( minimizedStr ) ) : NEW_LINE INDENT ch = ord ( minimizedStr [ i ] ) NEW_LINE index = ( ch * ch ) % l NEW_LINE finalStr = finalStr + string [ index ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeks " NEW_LINE replaceMinimizeUtil ( string ) NEW_LINE DEDENT