codeparrot/xlcost-text-to-code huggingface dataset:
Find the count of sub | Function to return the count of required occurrence ; To store the count of occurrences ; Check first four characters from ith position ; Variables for counting the required characters ; Check the four contiguous characters which can be reordered to form ' clap ' ; If all four contiguous characters are present then increment cnt variable ; Driver code
< ? php function countOcc ( $ s ) { $ cnt = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) - 3 ; $ i ++ ) { $ c = 0 ; $ l = 0 ; $ a = 0 ; $ p = 0 ; for ( $ j = $ i ; $ j < $ i + 4 ; $ j ++ ) { switch ( $ s [ $ j ] ) { case ' c ' : $ c ++ ; break ; case ' l ' : $ l ++ ; break ; case ' a ' : $ a ++ ; break ; case ' p ' : $ p ++ ; break ; } } if ( $ c == 1 && $ l == 1 && $ a == 1 && $ p == 1 ) $ cnt ++ ; } return $ cnt ; } $ s = " clapc " ; echo countOcc ( strtolower ( $ s ) ) ; ? >
Number of ways in which the substring in range [ L , R ] can be formed using characters out of the range | Function to return the number of ways to form the sub - string ; Initialize a hash - table with 0 ; Iterate in the string and count the frequency of characters that do not lie in the range L and R ; Out of range characters ; Stores the final number of ways ; Iterate for the sub - string in the range L and R ; If exists then multiply the number of ways and decrement the frequency ; If does not exist the sub - string cannot be formed ; Return the answer ; Driver code
< ? php function calculateWays ( $ s , $ n , $ l , $ r ) { $ freq = array ( ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { $ freq [ $ i ] = 0 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i < $ l $ i > $ r ) $ freq [ ord ( $ s [ $ i ] ) - 97 ] ++ ; } $ ways = 1 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) { if ( $ freq [ ord ( $ s [ $ i ] ) - 97 ] ) { $ ways = $ ways * $ freq [ ord ( $ s [ $ i ] ) - 97 ] ; $ freq [ ord ( $ s [ $ i ] ) - 97 ] -- ; } else { $ ways = 0 ; break ; } } return $ ways ; } $ s = " cabcaab " ; $ n = strlen ( $ s ) ; $ l = 1 ; $ r = 3 ; echo calculateWays ( $ s , $ n , $ l , $ r ) ; ? >
Convert the ASCII value sentence to its equivalent string | Function to print the character sequence for the given ASCII sentence ; Append the current digit ; If num is within the required range ; Convert num to char ; Reset num to 0 ; Driver code
< ? php function asciiToSentence ( $ string , $ length ) { $ num = 0 ; for ( $ i = 0 ; $ i < $ length ; $ i ++ ) { $ num = $ num * 10 + ( ord ( $ string [ $ i ] ) - ord ( '0' ) ) ; if ( $ num >= 32 && $ num <= 122 ) { $ ch = chr ( $ num ) ; print ( $ ch ) ; $ num = 0 ; } } } $ string = "7110110110711510211111471101101107115" ; $ length = strlen ( $ string ) ; asciiToSentence ( $ string , $ length ) ; ? >
Count of buttons pressed in a keypad mobile | Array to store how many times a button has to be pressed for typing a particular character ; Function to return the count of buttons pressed to type the given string ; Count the key presses ; Return the required count ; Driver code
< ? php $ arr = array ( 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 1 , 2 , 3 , 4 ) ; function countKeyPressed ( $ str , $ len ) { global $ arr ; $ count = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) $ count = $ count + $ arr [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ; return $ count ; } $ str = " abcdef " ; $ len = strlen ( $ str ) ; echo countKeyPressed ( $ str , $ len ) ; ? >
First string from the given array whose reverse is also present in the same array | Function that returns true if s1 is equal to reverse of s2 ; If both the strings differ in length ; In case of any character mismatch ; Function to return the first word whose reverse is also present in the array ; Check every string ; Pair with every other string appearing after the current string ; If first string is equal to the reverse of the second string ; No such string exists ; Driver code
< ? php function isReverseEqual ( $ s1 , $ s2 ) { if ( strlen ( $ s1 ) != strlen ( $ s2 ) ) return false ; $ len = strlen ( $ s1 ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) if ( $ s1 [ $ i ] != $ s2 [ $ len - $ i - 1 ] ) return false ; return true ; } function getWord ( $ str , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( isReverseEqual ( $ str [ $ i ] , $ str [ $ j ] ) ) return $ str [ $ i ] ; return " - 1" ; } $ str = array ( " geeks " , " for " , " skeeg " ) ; $ n = count ( $ str ) ; print ( getWord ( $ str , $ n ) ) ; ? >
Check if the given string is K | Function that return true if sub - of length $k starting at index $i is also a prefix of the string ; $k length sub - cannot start at index $i ; Character mismatch between the prefix and the sub - starting at index $i ; Function that returns true if $str is K - periodic ; Check whether all the sub - strings $str [ 0 , $k - 1 ] , $str [ $k , 2 k - 1 ] ... are equal to the $k length prefix of the ; Driver code
< ? php function isPrefix ( $ str , $ len , $ i , $ k ) { if ( $ i + $ k > $ len ) return false ; for ( $ j = 0 ; $ j < $ k ; $ j ++ ) { if ( $ str [ $ i ] != $ str [ $ j ] ) return false ; $ i ++ ; } return true ; } function isKPeriodic ( $ str , $ len , $ k ) { for ( $ i = $ k ; $ i < $ len ; $ i += $ k ) if ( ! isPrefix ( $ str , $ len , $ i , $ k ) ) return false ; return true ; } $ str = " geeksgeeks " ; $ len = strlen ( $ str ) ; $ k = 5 ; if ( isKPeriodic ( $ str , $ len , $ k ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Minimum number of letters needed to make a total of n | Function to return the minimum letters required to make a total of n ; Driver code
< ? php function minLettersNeeded ( $ n ) { if ( $ n % 26 == 0 ) return floor ( ( $ n / 26 ) ) ; else return floor ( ( $ n / 26 ) + 1 ) ; } $ n = 52 ; echo minLettersNeeded ( $ n ) ; ? >
Minimum number of replacements to make the binary string alternating | Set 2 | Function to return the minimum number of characters of the given binary string to be replaced to make the string alternating ; If there is 1 at even index positions ; If there is 0 at odd index positions ; Driver code
< ? php function minReplacement ( $ s , $ len ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ i % 2 == 0 && $ s [ $ i ] == '1' ) $ ans ++ ; if ( $ i % 2 == 1 && $ s [ $ i ] == '0' ) $ ans ++ ; } return min ( $ ans , $ len - $ ans ) ; } $ s = "1100" ; $ len = strlen ( $ s ) ; echo minReplacement ( $ s , $ len ) ; ? >
Group consecutive characters of same type in a string | Function to return the modified string ; Store original string ; Remove all white spaces ; To store the resultant string ; Traverse the string ; Group upper case characters ; Group numeric characters ; Group arithmetic operators ; Return the resultant string ; Driver code
< ? php function groupCharacters ( $ s , $ len ) { $ temp = " " ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) if ( $ s [ $ i ] != ' ▁ ' ) $ temp = $ temp . $ s [ $ i ] ; $ len = strlen ( $ temp ) ; $ ans = " " ; $ i = 0 ; while ( $ i < $ len ) { if ( ord ( $ temp [ $ i ] ) >= ord ( ' A ' ) && ord ( $ temp [ $ i ] ) <= ord ( ' Z ' ) ) { while ( $ i < $ len && ord ( $ temp [ $ i ] ) >= ord ( ' A ' ) && ord ( $ temp [ $ i ] ) <= ord ( ' Z ' ) ) { $ ans = $ ans . $ temp [ $ i ] ; $ i ++ ; } $ ans = $ ans . " ▁ " ; } else if ( ord ( $ temp [ $ i ] ) >= ord ( '0' ) && ord ( $ temp [ $ i ] ) <= ord ( '9' ) ) { while ( $ i < $ len && ord ( $ temp [ $ i ] ) >= ord ( '0' ) && ord ( $ temp [ $ i ] ) <= ord ( '9' ) ) { $ ans = $ ans . $ temp [ $ i ] ; $ i ++ ; } $ ans = $ ans . " ▁ " ; } else { while ( $ i < $ len && ord ( $ temp [ $ i ] ) >= ord ( ' * ' ) && ord ( $ temp [ $ i ] ) <= ord ( ' / ' ) ) { $ ans = $ ans . $ temp [ $ i ] ; $ i ++ ; } $ ans = $ ans . " ▁ " ; } } return $ ans ; } $ s = "34FTG234 + ▁ + - ▁ * " ; $ len = strlen ( $ s ) ; print ( groupCharacters ( $ s , $ len ) ) ; ? >
Find the minimum number of preprocess moves required to make two strings equal | Function to return the minimum number of pre - processing moves required on string A ; Length of the given strings ; To store the required answer ; To store frequency of 4 characters ; Run a loop upto n / 2 ; If size is 4 ; If size is 3 ; If size is 2 ; If n is odd ; Driver code
< ? php function Preprocess ( $ A , $ B ) { $ n = strlen ( $ A ) ; $ ans = 0 ; $ mp = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ mp [ $ A [ $ i ] ] = 0 ; for ( $ i = 0 ; $ i < floor ( $ n / 2 ) ; $ i ++ ) { $ mp [ $ A [ $ i ] ] ++ ; $ mp [ $ A [ $ n - $ i - 1 ] ] ++ ; $ mp [ $ B [ $ i ] ] ++ ; $ mp [ $ B [ $ n - $ i - 1 ] ] ++ ; $ sz = sizeof ( $ mp ) ; if ( $ sz == 4 ) $ ans += 2 ; else if ( $ sz == 3 ) if ( $ A [ $ i ] == $ A [ $ n - $ i - 1 ] ) $ ans += 1 ; else $ ans += 1 ; else if ( $ sz == 2 ) $ ans += $ mp [ $ A [ $ i ] ] != 2 ; } if ( $ n % 2 == 1 && ( $ A [ floor ( $ n / 2 ) ] != $ B [ floor ( $ n / 2 ) ] ) ) $ ans ++ ; return $ ans ; } $ A = " abacaba " ; $ B = " bacabaa " ; echo Preprocess ( $ A , $ B ) ; ? >
Check whether two strings are equivalent or not according to given condition | This function returns the least lexicogr aphical string obtained from its two halves ; Base Case - If string size is 1 ; Divide the string into its two halves ; Form least lexicographical string ; Driver Code
< ? php function leastLexiString ( $ s ) { if ( strlen ( $ s ) & 1 ) return $ s ; $ x = leastLexiString ( substr ( $ s , 0 , floor ( strlen ( $ s ) / 2 ) ) ) ; $ y = leastLexiString ( substr ( $ s , floor ( strlen ( $ s ) / 2 ) , strlen ( $ s ) ) ) ; return min ( $ x . $ y , $ y . $ x ) ; } function areEquivalent ( $ a , $ b ) { return ( leastLexiString ( $ a ) == leastLexiString ( $ b ) ) ; } $ a = " aaba " ; $ b = " abaa " ; if ( areEquivalent ( $ a , $ b ) ) echo " YES " , " STRNEWLINE " ; else echo " NO " , " STRNEWLINE " ; $ a = " aabb " ; $ b = " abab " ; if ( areEquivalent ( $ a , $ b ) ) echo " YES " , " STRNEWLINE " ; else echo " NO " , " STRNEWLINE " ; ? >
Generate a string consisting of characters ' a ' and ' b ' that satisfy the given conditions | Function to generate and print the required string ; More ' b ' , append " bba " ; More ' a ' , append " aab " ; Equal number of ' a ' and ' b ' append " ab " ; Driver code
< ? php function generateString ( $ A , $ B ) { $ rt = " " ; while ( 0 < $ A 0 < $ B ) { if ( $ A < $ B ) { if ( 0 < $ B -- ) { $ rt . = ( ' b ' ) ; } if ( 0 < $ B -- ) { $ rt . = ( ' b ' ) ; } if ( 0 < $ A -- ) { $ rt . = ( ' a ' ) ; } } else if ( $ B < $ A ) { if ( 0 < $ A -- ) { $ rt . = ( ' a ' ) ; } if ( 0 < $ A -- ) { $ rt . = ( ' a ' ) ; } if ( 0 < $ B -- ) { $ rt . = ( ' b ' ) ; } } else { if ( 0 < $ A -- ) { $ rt . = ( ' a ' ) ; } if ( 0 < $ B -- ) { $ rt . = ( ' b ' ) ; } } } echo ( $ rt ) ; } $ A = 2 ; $ B = 6 ; generateString ( $ A , $ B ) ; ? >
Lexicographically largest sub | Function to return the lexicographically largest sub - sequence of s ; Get the max character from the string ; Use all the occurrences of the current maximum character ; Repeat the steps for the remaining string ; Driver code
< ? php function getSubSeq ( $ s , $ n ) { $ res = " " ; $ cr = 0 ; while ( $ cr < $ n ) { $ mx = $ s [ $ cr ] ; for ( $ i = $ cr + 1 ; $ i < $ n ; $ i ++ ) $ mx = max ( $ mx , $ s [ $ i ] ) ; $ lst = $ cr ; for ( $ i = $ cr ; $ i < $ n ; $ i ++ ) if ( $ s [ $ i ] == $ mx ) { $ res . = $ s [ $ i ] ; $ lst = $ i ; } $ cr = $ lst + 1 ; } return $ res ; } $ s = " geeksforgeeks " ; $ n = strlen ( $ s ) ; echo getSubSeq ( $ s , $ n ) ; ? >
Count of sub | Function to return the count of sub - strings of str that are divisible by k ; Take all sub - strings starting from i ; If current sub - string is divisible by k ; Return the required count ; Driver code
< ? php function countSubStr ( $ str , $ len , $ k ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ n = 0 ; for ( $ j = $ i ; $ j < $ len ; $ j ++ ) { $ n = $ n * 10 + ( $ str [ $ j ] - '0' ) ; if ( $ n % $ k == 0 ) $ count ++ ; } } return $ count ; } $ str = "33445" ; $ len = strlen ( $ str ) ; $ k = 11 ; echo countSubStr ( $ str , $ len , $ k ) ; ? >
Find the resulting Colour Combination | Function to return Colour Combination ; Check for B * G = Y ; Check for B * Y = G ; Check for Y * G = B ; Driver Code
< ? php function Colour_Combination ( $ s ) { $ temp = $ s [ 0 ] ; for ( $ i = 1 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ temp != $ s [ $ i ] ) { if ( ( $ temp == ' B ' $ temp == ' G ' ) && ( $ s [ $ i ] == ' G ' $ s [ $ i ] == ' B ' ) ) $ temp = ' Y ' ; else if ( ( $ temp == ' B ' $ temp == ' Y ' ) && ( $ s [ $ i ] == ' Y ' $ s [ $ i ] == ' B ' ) ) $ temp = ' G ' ; else $ temp = ' B ' ; } } return $ temp ; } $ s = " GBYGB " ; echo Colour_Combination ( $ s ) ; ? >
Reverse Middle X Characters | Function to reverse the middle x characters in a string ; Find the position from where the characters have to be reversed ; Print the first n characters ; Print the middle x characters in reverse ; Print the last n characters ; Driver code
< ? php function reverse ( $ str , $ x ) { $ n = ( strlen ( $ str ) - $ x ) / 2 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ str [ $ i ] ) ; for ( $ i = $ n + $ x - 1 ; $ i >= $ n ; $ i -- ) echo ( $ str [ $ i ] ) ; for ( $ i = $ n + $ x ; $ i < strlen ( $ str ) ; $ i ++ ) echo $ str [ $ i ] ; } $ str = " geeksforgeeks " ; $ x = 3 ; reverse ( $ str , $ x ) ; ? >
Minimize the number of replacements to get a string with same number of ' a ' , ' b ' and ' c ' in it | Function to count numbers ; Count the number of ' a ' , ' b ' and ' c ' in string ; If equal previously ; If not a multiple of 3 ; Increase the number of a ' s ▁ by ▁ ▁ ▁ removing ▁ extra ▁ ' b ' and ;c;  ; Check if it is ' b ' and it is more than n / 3 ; Check if it is ' c ' and it more than n / 3 ; Increase the number of b ' s ▁ by ▁ ▁ ▁ removing ▁ extra ▁ ' c '  ; Check if it is ' c ' and it more than n / 3 ; Increase the number of c 's from back  ; Check if it is ' a ' and it is more than n / 3 ; Increase the number of b 's from back  ; Check if it is ' a ' and it is more than n / 3 ; Increase the number of c 's from back  ; Check if it is ' b ' and it more than n / 3 ; Driver Code
< ? php function lexoSmallest ( $ s , $ n ) { $ ca = 0 ; $ cb = 0 ; $ cc = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == ' a ' ) $ ca ++ ; else if ( $ s [ $ i ] == ' b ' ) $ cb ++ ; else $ cc ++ ; } if ( $ ca == $ cb && $ cb == $ cc ) { return $ s ; } $ cnt = floor ( $ n / 3 ) ; if ( $ cnt * 3 != $ n ) { return " - 1" ; } $ i = 0 ; while ( $ ca < $ cnt && $ i < $ n ) { if ( $ s [ $ i ] == ' b ' && $ cb > $ cnt ) { $ cb -- ; $ s [ $ i ] = ' a ' ; $ ca ++ ; } else if ( $ s [ $ i ] == ' c ' && $ cc > $ cnt ) { $ cc -- ; $ s [ $ i ] = ' a ' ; $ ca ++ ; } $ i ++ ; } $ i = 0 ; while ( $ cb < $ cnt && $ i < $ n ) { if ( $ s [ $ i ] == ' c ' && $ cc > $ cnt ) { $ cc -- ; $ s [ $ i ] = '1' ; $ cb ++ ; } $ i ++ ; } $ i = $ n - 1 ; while ( $ cc < $ cnt && $ i >= 0 ) { if ( $ s [ $ i ] == ' a ' && $ ca > $ cnt ) { $ ca -- ; $ s [ $ i ] = ' c ' ; $ cc ++ ; } $ i -- ; } $ i = $ n - 1 ; while ( $ cb < $ cnt && $ i >= 0 ) { if ( $ s [ $ i ] == ' a ' && $ ca > $ cnt ) { $ ca -- ; $ s [ $ i ] = ' b ' ; $ cb ++ ; } $ i -- ; } $ i = $ n - 1 ; while ( $ cc < $ cnt && $ i >= 0 ) { if ( $ s [ $ i ] == ' b ' && $ cb > $ cnt ) { $ cb -- ; $ s [ $ i ] = ' c ' ; $ cc ++ ; } $ i -- ; } return $ s ; } $ s = " aaaaaa " ; $ n = strlen ( $ s ) ; echo lexoSmallest ( $ s , $ n ) ; ? >
Minimum moves to reach from i to j in a cyclic string | Function to return the count of steps required to move from i to j ; Starting from i + 1 ; Count of steps ; Current character ; If current character is different from previous ; Increment steps ; Update current character ; Return total steps ; Function to return the minimum number of steps required to reach j from i ; Swap the values so that i <= j ; Steps to go from i to j ( left to right ) ; While going from i to j ( right to left ) First go from i to 0 then from ( n - 1 ) to j ; If first and last character is different then it 'll add a step to stepsToLeft  ; Return the minimum of two paths ; Driver code
< ? php function getSteps ( $ str , $ i , $ j , $ n ) { $ k = $ i + 1 ; $ steps = 0 ; $ ch = $ str [ $ i ] ; while ( $ k <= $ j ) { if ( $ str [ $ k ] != $ ch ) { $ steps ++ ; $ ch = $ str [ $ k ] ; } $ k ++ ; } return $ steps ; } function getMinSteps ( $ str , $ i , $ j , $ n ) { if ( $ j < $ i ) { $ temp = $ i ; $ i = $ j ; $ j = $ temp ; } $ stepsToRight = getSteps ( $ str , $ i , $ j , $ n ) ; $ stepsToLeft = getSteps ( $ str , 0 , $ i , $ n ) + getSteps ( $ str , $ j , $ n - 1 , $ n ) ; if ( $ str [ 0 ] != $ str [ $ n - 1 ] ) $ stepsToLeft ++ ; return min ( $ stepsToLeft , $ stepsToRight ) ; } $ str = " SSNSS " ; $ n = strlen ( $ str ) ; $ i = 0 ; $ j = 3 ; echo getMinSteps ( $ str , $ i , $ j , $ n ) ; ? >
Remove consecutive alphabets which are in same case | Function to return the modified string ; Traverse through the remaining characters in the string ; If the current and the previous characters are not in the same case then take the character ; Driver code
< ? php function removeChars ( $ s ) { $ modifiedStr = " " ; $ modifiedStr = $ modifiedStr . $ s [ 0 ] ; for ( $ i = 1 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( ctype_upper ( $ s [ $ i ] ) && ctype_lower ( $ s [ $ i - 1 ] ) || ctype_lower ( $ s [ $ i ] ) && ctype_upper ( $ s [ $ i - 1 ] ) ) $ modifiedStr = $ modifiedStr . $ s [ $ i ] ; } return $ modifiedStr ; } $ s = " GeeksForGeeks " ; echo removeChars ( $ s ) ; ? >
Cost to make a string Panagram | Function to return the total cost required to make the string Pangram ; Mark all the alphabets that occurred in the string ; Calculate the total cost for the missing alphabets ; Driver Code
< ? php function pangramCost ( $ arr , $ str ) { $ cost = 0 ; $ occurred = array ( ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) $ occurred [ $ i ] = false ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { $ idx = ord ( $ str [ $ i ] ) - 97 ; $ occurred [ $ idx ] = true ; } for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ occurred [ $ i ] == false ) $ cost += $ arr [ $ i ] ; } return $ cost ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 ) ; $ str = " abcdefghijklmopqrstuvwz " ; echo pangramCost ( $ arr , $ str ) ; ? >
Recursive program to insert a star between pair of identical characters | Function to insert * at desired position ; Append current character ; If we reached last character ; If next character is same , append ' * ' ; Driver code
< ? php function pairStar ( & $ input , & $ output , $ i = 0 ) { $ output = $ output . $ input [ $ i ] ; if ( $ i == strlen ( $ input ) - 1 ) return ; if ( $ input [ $ i ] == $ input [ $ i + 1 ] ) $ output = $ output . ' * ' ; pairStar ( $ input , $ output , $ i +1 ) ; } $ input = " geeks " ; $ output = " " ; pairStar ( $ input , $ output ) ; echo $ output ; return 0 ; ? >
Maximum number of removals of given subsequence from a string | Function to return max possible operation of the given type that can be performed on str ; Increment count of sub - sequence ' g ' ; Increment count of sub - sequence ' gk ' if ' g ' is available ; Increment count of sub - sequence ' gks ' if sub - sequence ' gk ' appeared previously ; Return the count of sub - sequence ' gks ' ; Driver code
< ? php function maxOperations ( $ str ) { $ i = $ g = $ gk = $ gks = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] == ' g ' ) { $ g ++ ; } else if ( $ str [ $ i ] == ' k ' ) { if ( $ g > 0 ) { $ g -- ; $ gk ++ ; } } else if ( $ str [ $ i ] == ' s ' ) { if ( $ gk > 0 ) { $ gk -- ; $ gks ++ ; } } } return $ gks ; } $ a = " ggkssk " ; echo maxOperations ( $ a ) ; ? >
Final string after performing given operations | Function to return the modified string ; Count number of ' x ' ; Count number of ' y ' ; min ( x , y ) number of ' x ' and ' y ' will be deleted ; Driver Code
< ? php function printFinalString ( $ s ) { $ n = strlen ( $ s ) ; $ x = 0 ; $ y = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == ' x ' ) $ x ++ ; else $ y ++ ; } $ finalString = ( string ) null ; if ( $ x > $ y ) for ( $ i = 0 ; $ i < $ x - $ y ; $ i ++ ) $ finalString . = " x " ; else for ( $ i = 0 ; $ i < $ y - $ x ; $ i ++ ) $ finalString . = " y " ; return $ finalString ; } $ s = " xxyyxyy " ; echo printFinalString ( $ s ) ; ? >
String which when repeated exactly K times gives a permutation of S | Function to return a string which when repeated exactly k times gives a permutation of s ; size of string ; to frequency of each character ; get frequency of each character ; to store final answer ; check if frequency is divisible by k ; add to answer ; if frequency is not divisible by k ; Driver code ; function call
< ? php function K_String ( $ s , $ k ) { $ n = strlen ( $ s ) ; $ fre = $ array = array_fill ( 0 , 26 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ fre [ ord ( $ s [ $ i ] ) - ord ( ' a ' ) ] ++ ; $ str = " " ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ fre [ $ i ] % $ k == 0 ) { $ x = $ fre [ $ i ] / $ k ; while ( $ x -- ) { $ str . = chr ( $ i + ord ( ' a ' ) ) ; } } else { return " - 1" ; } } return $ str ; } $ s = " aabb " ; $ k = 2 ; echo K_String ( $ s , $ k ) ; ? >
Count and Print the alphabets having ASCII value not in the range [ l , r ] | Function to count the number of characters whose ascii value not in range [ l , r ] ; Initializing the count to 0 ; using map to print a character only once ; Increment the count if the value is less ; return the count ; Driver code
< ? php function CountCharacters ( $ str , $ l , $ r ) { $ cnt = 0 ; $ m = array_fill ( 0 , 256 , NULL ) ; $ len = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( ! ( $ l <= ord ( $ str [ $ i ] ) and ord ( $ str [ $ i ] ) <= $ r ) ) { $ cnt ++ ; if ( isset ( $ m [ ord ( $ str [ $ i ] ) ] ) != 1 ) { echo $ str [ $ i ] . " " ; $ m [ ord ( $ str [ $ i ] ) ] ++ ; } } } return $ cnt ; } $ str = " geeksforgeeks " ; $ l = 102 ; $ r = 111 ; echo " Characters ▁ with ▁ ASCII ▁ values ▁ not ▁ in ▁ the ▁ " . " in the given string are : " echo " and their count is " . CountCharacters ( $ str , $ l , $ r ) ; ? >
Count the number of words having sum of ASCII values less than and greater than k | Function to count the words ; Sum of ascii values ; Number of words having sum of ascii less than k ; If character is a space ; Add the ascii value to sum ; Handling the Last word separately ; Driver code
< ? php function CountWords ( $ str , $ k ) { $ sum = 0 ; $ NumberOfWords = 0 ; $ counter = 0 ; $ len = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ len ; ++ $ i ) { if ( $ str [ $ i ] == ' ▁ ' ) { if ( $ sum < $ k ) $ counter ++ ; $ sum = 0 ; $ NumberOfWords ++ ; } else $ sum += ord ( $ str [ $ i ] ) ; } $ NumberOfWords ++ ; if ( $ sum < $ k ) $ counter ++ ; echo " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ ASCII " . " values less than k = " ▁ . ▁ $ counter ▁ . ▁ " " ; STRNEWLINE TABSYMBOL echo ▁ " Number of words having sum of ASCII " ▁ . STRNEWLINE TABSYMBOL TABSYMBOL " values greater than or equal to k = " ( $ NumberOfWords - $ counter ) ; } $ str = " Learn ▁ how ▁ to ▁ code " ; $ k = 400 ; CountWords ( $ str , $ k ) ; ? >
Find the count of palindromic sub | PHP program to find the count of palindromic sub - string of a string in it 's ascending form ; function to return count of palindromic sub - string ; calculate frequency ; calculate count of palindromic sub - string ; return result ; Driver Code
< ? php $ MAX_CHAR = 26 ; function countPalindrome ( $ str ) { global $ MAX_CHAR ; $ n = strlen ( $ str ) ; $ sum = 0 ; $ hashTable = array_fill ( 0 , $ MAX_CHAR , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ hashTable [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ hashTable [ $ i ] ) $ sum += ( int ) ( $ hashTable [ $ i ] * ( $ hashTable [ $ i ] + 1 ) / 2 ) ; } return $ sum ; } $ str = " ananananddd " ; echo countPalindrome ( $ str ) ; ? >