codeparrot/xlcost-text-to-code huggingface dataset:
Count number of ways to partition a set into k subsets | Returns count of different partitions of n elements in k subsets ; Base cases ; S ( n + 1 , k ) = k * S ( n , k ) + S ( n , k - 1 ) ; Driver Code
< ? php function countP ( $ n , $ k ) { if ( $ n == 0 $ k == 0 $ k > $ n ) return 0 ; if ( $ k == 1 $ k == $ n ) return 1 ; return $ k * countP ( $ n - 1 , $ k ) + countP ( $ n - 1 , $ k - 1 ) ; } echo countP ( 3 , 2 ) ; ? >
Count number of ways to partition a set into k subsets | Returns count of different partitions of n elements in k subsets ; Table to store results of subproblems ; Base cases ; Fill rest of the entries in dp [ ] [ ] in bottom up manner ; Driver Code
< ? php function countP ( $ n , $ k ) { $ dp [ $ n + 1 ] [ $ k + 1 ] = array ( array ( ) ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] [ 0 ] = 0 ; for ( $ i = 0 ; $ i <= $ k ; $ i ++ ) $ dp [ 0 ] [ $ k ] = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) if ( $ j == 1 $ i == $ j ) $ dp [ $ i ] [ $ j ] = 1 ; else $ dp [ $ i ] [ $ j ] = $ j * $ dp [ $ i - 1 ] [ $ j ] + $ dp [ $ i - 1 ] [ $ j - 1 ] ; return $ dp [ $ n ] [ $ k ] ; } echo countP ( 5 , 2 ) ; ? >
Count number of ways to cover a distance | Returns count of ways to cover ' dist ' ; Base cases ; Recur for all previous 3 and add the results ; Driver Code
< ? php function printCountRec ( $ dist ) { if ( $ dist < 0 ) return 0 ; if ( $ dist == 0 ) return 1 ; return printCountRec ( $ dist - 1 ) + printCountRec ( $ dist - 2 ) + printCountRec ( $ dist - 3 ) ; } $ dist = 4 ; echo printCountRec ( $ dist ) ; ? >
Count numbers from 1 to n that have 4 as a digit | Returns sum of all digits in numbers from 1 to n ; One by one compute sum of digits in every number from 1 to n ; A utility function to compute sum of digits in a given number x ; Driver Code
< ? php function countNumbersWith4 ( $ n ) { for ( $ x = 1 ; $ x <= $ n ; $ x ++ ) $ result += has4 ( $ x ) ? 1 : 0 ; return $ result ; } function has4 ( $ x ) { while ( $ x != 0 ) { if ( $ x % 10 == 4 ) return true ; $ x = intval ( $ x / 10 ) ; } return false ; } $ n = 328 ; echo " Count ▁ of ▁ numbers ▁ from ▁ 1 ▁ to ▁ " . $ n . " ▁ that ▁ have ▁ 4 ▁ as ▁ a ▁ a ▁ digit ▁ is ▁ " . countNumbersWith4 ( $ n ) ; ? >
Count numbers from 1 to n that have 4 as a digit | Function to count numbers from 1 to n that have 4 as a digit ; Base case ; d = number of digits minus one in n . For 328 , d is 2 ; computing count of numbers from 1 to 10 ^ d - 1 , d = 0 a [ 0 ] = 0 ; d = 1 a [ 1 ] = count of numbers from 0 to 9 is 1 d = 2 a [ 2 ] = count of numbers from 0 to 99 is a [ 1 ] * 9 + 10 = 19 d = 3 a [ 3 ] = count of numbers from 0 to 999 is a [ 2 ] * 19 + 100 = 171 ; Computing 10 ^ d ; Most significant digit ( msd ) of n , For 328 , msd is 3 which can be obtained using 328 / 100 ; If MSD is 4. For example if n = 428 , then count of numbers is sum of following . 1 ) Count of numbers from 1 to 399 2 ) Count of numbers from 400 to 428 which is 29. ; IF MSD > 4. For example if n is 728 , then count of numbers is sum of following . 1 ) Count of numbers from 1 to 399 and count of numbers from 500 to 699 , i . e . , " a [ 2 ] ▁ * ▁ 6" 2 ) Count of numbers from 400 to 499 , i . e . 100 3 ) Count of numbers from 700 to 728 , recur for 28 ; IF MSD < 4. For example if n is 328 , then count of numbers is sum of following . 1 ) Count of numbers from 1 to 299 a 2 ) Count of numbers from 300 to 328 , recur for 28 ; Driver Code
< ? php function countNumbersWith4 ( $ n ) { if ( $ n < 4 ) return 0 ; $ d = ( int ) log10 ( $ n ) ; $ a = array_fill ( 0 , $ d + 1 , NULL ) ; $ a [ 0 ] = 0 ; $ a [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ d ; $ i ++ ) $ a [ $ i ] = $ a [ $ i - 1 ] * 9 + ceil ( pow ( 10 , $ i - 1 ) ) ; $ p = ceil ( pow ( 10 , $ d ) ) ; $ msd = intval ( $ n / $ p ) ; if ( $ msd == 4 ) return ( $ msd ) * $ a [ $ d ] + ( $ n % $ p ) + 1 ; if ( $ msd > 4 ) return ( $ msd - 1 ) * $ a [ $ d ] + $ p + countNumbersWith4 ( $ n % $ p ) ; return ( $ msd ) * $ a [ $ d ] + countNumbersWith4 ( $ n % $ p ) ; } $ n = 328 ; echo " Count ▁ of ▁ numbers ▁ from ▁ 1 ▁ to ▁ " . $ n . " ▁ that ▁ have ▁ 4 ▁ as ▁ a ▁ digit ▁ is ▁ " . countNumbersWith4 ( $ n ) . " STRNEWLINE " ; ? >
Remove minimum elements from either side such that 2 * min becomes more than max | Returns the minimum number of removals from either end in arr [ l . . h ] so that 2 * min becomes greater than max . ; Initialize starting and ending indexes of the maximum sized subarray with property 2 * min > max ; Choose different elements as starting point ; Initialize min and max for the current start ; Choose different ending points for current start ; Update min and max if necessary ; If the property is violated , then no point to continue for a bigger array ; Update longest_start and longest_end if needed ; If not even a single element follow the property , then return n ; Return the number of elements to be removed ; Driver Code
< ? php function minRemovalsDP ( $ arr , $ n ) { $ longest_start = -1 ; $ longest_end = 0 ; for ( $ start = 0 ; $ start < $ n ; $ start ++ ) { $ min = PHP_INT_MAX ; $ max = PHP_INT_MIN ; for ( $ end = $ start ; $ end < $ n ; $ end ++ ) { $ val = $ arr [ $ end ] ; if ( $ val < $ min ) $ min = $ val ; if ( $ val > $ max ) $ max = $ val ; if ( 2 * $ min <= $ max ) break ; if ( $ end - $ start > $ longest_end - $ longest_start $ longest_start == -1 ) { $ longest_start = $ start ; $ longest_end = $ end ; } } } if ( $ longest_start == -1 ) return $ n ; return ( $ n - ( $ longest_end - $ longest_start + 1 ) ) ; } $ arr = array ( 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 ) ; $ n = sizeof ( $ arr ) ; echo minRemovalsDP ( $ arr , $ n ) ; ? >
Optimal Strategy for a Game | DP | Returns optimal value possible that a player can collect from an array of coins of size n . Note than n must be even ; Create a table to store solutions of subproblems ; Fill table using above recursive formula . Note that the table is filled in diagonal fashion ( similar to http : goo . gl / PQqoS ) , from diagonal elements to table [ 0 ] [ n - 1 ] which is the result . ; Here x is value of F ( i + 2 , j ) , y is F ( i + 1 , j - 1 ) and z is F ( i , j - 2 ) in above recursive formula ; Driver Code
< ? php function optimalStrategyOfGame ( $ arr , $ n ) { $ table = array_fill ( 0 , $ n , array_fill ( 0 , $ n , 0 ) ) ; for ( $ gap = 0 ; $ gap < $ n ; ++ $ gap ) { for ( $ i = 0 , $ j = $ gap ; $ j < $ n ; ++ $ i , ++ $ j ) { $ x = ( ( $ i + 2 ) <= $ j ) ? $ table [ $ i + 2 ] [ $ j ] : 0 ; $ y = ( ( $ i + 1 ) <= ( $ j - 1 ) ) ? $ table [ $ i + 1 ] [ $ j - 1 ] : 0 ; $ z = ( $ i <= ( $ j - 2 ) ) ? $ table [ $ i ] [ $ j - 2 ] : 0 ; $ table [ $ i ] [ $ j ] = max ( $ arr [ $ i ] + min ( $ x , $ y ) , $ arr [ $ j ] + min ( $ y , $ z ) ) ; } } return $ table [ 0 ] [ $ n - 1 ] ; } $ arr1 = array ( 8 , 15 , 3 , 7 ) ; $ n = count ( $ arr1 ) ; print ( optimalStrategyOfGame ( $ arr1 , $ n ) . " " ) ; $ arr2 = array ( 2 , 2 , 2 , 2 ) ; $ n = count ( $ arr2 ) ; print ( optimalStrategyOfGame ( $ arr2 , $ n ) . " " ) ; $ arr3 = array ( 20 , 30 , 2 , 2 , 2 , 10 ) ; $ n = count ( $ arr3 ) ; print ( optimalStrategyOfGame ( $ arr3 , $ n ) . " " ) ; ? >
Maximum Sum Increasing Subsequence | DP | maxSumIS ( ) returns the maximum sum of increasing subsequence in arr [ ] of size n ; Initialize msis values for all indexes ; Compute maximum sum values in bottom up manner ; Pick maximum of all msis values ; Driver Code
< ? php function maxSumIS ( $ arr , $ n ) { $ max = 0 ; $ msis = array ( $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ msis [ $ i ] = $ arr [ $ i ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] > $ arr [ $ j ] && $ msis [ $ i ] < $ msis [ $ j ] + $ arr [ $ i ] ) $ msis [ $ i ] = $ msis [ $ j ] + $ arr [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ max < $ msis [ $ i ] ) $ max = $ msis [ $ i ] ; return $ max ; } $ arr = array ( 1 , 101 , 2 , 3 , 100 , 4 , 5 ) ; $ n = count ( $ arr ) ; echo " Sum ▁ of ▁ maximum ▁ sum ▁ increasing ▁ subsequence ▁ is ▁ " . maxSumIS ( $ arr , $ n ) ; ? >
Count strings that end with the given pattern | Function that return true if str ends with pat ; Pattern is larger in length than the string ; We match starting from the end while patLen is greater than or equal to 0. ; If at any index str doesn 't match  with pattern ; If str ends with the given pattern ; Function to return the count of required strings ; If current string ends with the given pattern ; Driver code
< ? php function endsWith ( $ str , $ pat ) { $ patLen = strlen ( $ pat ) ; $ strLen = strlen ( $ str ) ; if ( $ patLen > $ strLen ) return false ; $ patLen -- ; $ strLen -- ; while ( $ patLen >= 0 ) { if ( $ pat [ $ patLen ] != $ str [ $ strLen ] ) return false ; $ patLen -- ; $ strLen -- ; } return true ; } function countOfStrings ( $ pat , $ n , $ sArr ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( endsWith ( $ sArr [ $ i ] , $ pat ) ) $ count ++ ; return $ count ; } $ pat = " ks " ; $ n = 4 ; $ sArr = array ( " geeks " , " geeksforgeeks " , " games " , " unit " ) ; echo countOfStrings ( $ pat , $ n , $ sArr ) ; ? >
Length of the longest substring with consecutive characters | Function to return the ending index for the largest valid sub - string / starting from index i ; If the current character appears after the previous character according to the given circular alphabetical order ; Function to return the length of the longest sub - string of consecutive characters from str ; Valid sub - string exists from index i to end ; Update the length ; Driver code
< ? php function getEndingIndex ( $ str , $ n , $ i ) { $ i ++ ; while ( $ i < $ n ) { $ curr = $ str [ $ i ] ; $ prev = $ str [ $ i - 1 ] ; if ( ( $ curr == ' a ' && $ prev == ' z ' ) || ( ord ( $ curr ) - ord ( $ prev ) == 1 ) ) $ i ++ ; else break ; } return $ i - 1 ; } function largestSubStr ( $ str , $ n ) { $ len = 0 ; $ i = 0 ; while ( $ i < $ n ) { $ end = getEndingIndex ( $ str , $ n , $ i ) ; $ len = max ( $ end - $ i + 1 , $ len ) ; $ i = $ end + 1 ; } return $ len ; } $ str = " abcabcdefabc " ; $ n = strlen ( $ str ) ; echo largestSubStr ( $ str , $ n ) ; ? >
Sum of integers upto N with given unit digit ( Set 2 ) | Function to return the required sum ; Decrement N ; Driver code
< ? php function getSum ( $ n , $ d ) { if ( $ n < $ d ) return 0 ; while ( $ n % 10 != $ d ) $ n -- ; $ k = ( int ) ( $ n / 10 ) ; return ( $ k + 1 ) * $ d + ( $ k * 10 + 10 * $ k * $ k ) / 2 ; } $ n = 30 ; $ d = 3 ; echo getSum ( $ n , $ d ) ; ? >
Minimum characters to be replaced to remove the given substring | function to calculate minimum characters to replace ; mismatch occurs ; if all characters matched , i . e , there is a substring of ' a ' which is same as string ' b ' ; increment i to index m - 1 such that minimum characters are replaced in ' a ' ; Driver Code
< ? php function replace ( $ A , $ B ) { $ n = strlen ( $ A ) ; $ m = strlen ( $ B ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ m ; $ j ++ ) { if ( $ i + $ j >= $ n ) { break ; } else if ( $ A [ $ i + $ j ] != $ B [ $ j ] ) { break ; } } if ( $ j == $ m ) { $ count ++ ; $ i = $ i + $ m - 1 ; } } return $ count ; } $ str1 = " aaaaaaaa " ; $ str2 = " aaa " ; echo ( replace ( $ str1 , $ str2 ) ) ; ? >
Check if a string is substring of another | Returns true if s1 is substring of s2 ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; Driver Code
< ? php function isSubstring ( $ s1 , $ s2 ) { $ M = strlen ( $ s1 ) ; $ N = strlen ( $ s2 ) ; for ( $ i = 0 ; $ i <= $ N - $ M ; $ i ++ ) { $ j = 0 ; for ( ; $ j < $ M ; $ j ++ ) if ( $ s2 [ $ i + $ j ] != $ s1 [ $ j ] ) break ; if ( $ j == $ M ) return $ i ; } return -1 ; } $ s1 = " for " ; $ s2 = " geeksforgeeks " ; $ res = isSubstring ( $ s1 , $ s2 ) ; if ( $ res == -1 ) echo " Not ▁ present " ; else echo " Present ▁ at ▁ index ▁ " . $ res ; ? >
Find all the patterns of "1(0 + ) 1" in a given string | SET 1 ( General Approach ) | Function to count patterns ; Variable to store the last character ; We found 0 and last character was '1' , state change ; After the stream of 0 ' s , ▁ ▁ we ▁ got ▁ a ▁ ' 1 ', counter  incremented ; Last character stored ; Driver Code
< ? php function patternCount ( $ str ) { $ last = $ str [ 0 ] ; $ i = 1 ; $ counter = 0 ; while ( $ i < strlen ( $ str ) ) { if ( $ str [ $ i ] == '0' && $ last == '1' ) { while ( $ str [ $ i ] == '0' ) $ i ++ ; if ( $ str [ $ i ] == '1' ) $ counter ++ ; } $ last = $ str [ $ i ] ; $ i ++ ; } return $ counter ; } $ str = "1001ab010abc01001" ; echo patternCount ( $ str ) ; ? >
Maximum length prefix of one string that occurs as subsequence in another | Return the maximum length prefix which is subsequence . ; Iterating string T . ; If end of string S . ; If character match , increment counter . ; Driver Code
< ? php function maxPrefix ( $ s , $ t ) { $ count = 0 ; for ( $ i = 0 ; $ i < strlen ( $ t ) ; $ i ++ ) { if ( $ count == strlen ( $ s ) ) break ; if ( $ t [ $ i ] == $ s [ $ count ] ) $ count ++ ; } return $ count ; } { $ S = " digger " ; $ T = " biggerdiagram " ; echo maxPrefix ( $ S , $ T ) ; return 0 ; } ? >
Replace all occurrences of string AB with C without using extra space | PHP program to replace all occurrences of " AB " with " C " ; Start traversing from second character ; If previous character is ' A ' and current character is 'B" ; Replace previous character with ' C ' and move all subsequent characters one position back ; Driver code
< ? php function translate ( & $ str ) { if ( $ str [ 0 ] == ' ' ) return ; for ( $ i = 1 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i - 1 ] == ' A ' && $ str [ $ i ] == ' B ' ) { $ str [ $ i - 1 ] = ' C ' ; for ( $ j = $ i ; $ j < strlen ( $ str ) ; $ j ++ ) $ str [ $ j ] = $ str [ $ j + 1 ] ; } } return ; } $ str = " helloABworldABGfG " ; translate ( $ str ) ; echo " The ▁ modified ▁ string ▁ is ▁ : STRNEWLINE " ; echo $ str ; ? >
Find letter 's position in Alphabet using Bit operation | Function to calculate the position of characters ; Performing AND operation with number 31 $ ; Driver code
< ? php function positions ( $ str , $ n ) { $ a = 31 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { print ( ( ord ( $ str [ $ i ] ) & ( $ a ) ) . " " ) ; } } $ str = " Geeks " ; $ n = strlen ( $ str ) ; positions ( $ str , $ n ) ; ? >
Length of the longest substring that do not contain any palindrome | Function to find the length of the longest substring ; initializing the variables ; checking palindrome of size 2 example : aa ; checking palindrome of size 3 example : aba ; else incrementing length of substring ; $max1 = max ( $max1 , $len + 1 ) ; finding maximum ; if there exits single character then it is always palindrome ; Driver Code
< ? php function lenoflongestnonpalindrome ( $ s ) { $ max1 = 1 ; $ len = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) - 1 ; $ i ++ ) { if ( $ s [ $ i ] == $ s [ $ i + 1 ] ) $ len = 0 ; else if ( $ s [ $ i + 1 ] == $ s [ $ i - 1 ] && $ i > 0 ) $ len = 1 ; $ len ++ ; } if ( $ max1 == 1 ) return 0 ; else return $ max1 ; } $ s = " synapse " ; echo lenoflongestnonpalindrome ( $ s ) , " " ; ? >
Make lexicographically smallest palindrome by substituting missing characters | Function to return the lexicographically smallest palindrome that can be made from the given string after replacing the required characters ; If characters are missing at both the positions then substitute it with ' a ' ; If only str [ j ] = ' * ' then update it with the value at str [ i ] ; If only str [ i ] = ' * ' then update it with the value at str [ j ] ; If characters at both positions are not equal and != ' * ' then the string cannot be made palindrome ; Return the required palindrome ; Driver code
< ? php function makePalindrome ( $ str ) { $ i = 0 ; $ j = strlen ( $ str ) - 1 ; while ( $ i <= $ j ) { if ( $ str [ $ i ] == ' * ' && $ str [ $ j ] == ' * ' ) { $ str [ $ i ] = ' a ' ; $ str [ $ j ] = ' a ' ; } else if ( $ str [ $ j ] == ' * ' ) $ str [ $ j ] = $ str [ $ i ] ; else if ( $ str [ $ i ] == ' * ' ) $ str [ $ i ] = $ str [ $ j ] ; else if ( $ str [ $ i ] != $ str [ $ j ] ) return " - 1" ; $ i ++ ; $ j -- ; } return $ str ; } $ str = " na * an " ; echo makePalindrome ( $ str ) ; ? >
Calculate score for the given binary string | Function to return the score for the given binary string ; Traverse through string character ; Initialize current chunk 's size ; Get current character ; Calculate total chunk size of same characters ; Add / subtract pow ( chunkSize , 2 ) depending upon character ; Return the score ; Driver code
< ? php function calcScore ( $ str ) { $ score = 0 ; $ len = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ len { $ chunkSize = 1 ; $ currentChar = $ str [ $ i ++ ] ; while ( $ i < $ len && $ str [ $ i ] == $ currentChar ) { $ chunkSize ++ ; $ i ++ ; } if ( $ currentChar == '1' ) $ score += pow ( $ chunkSize , 2 ) ; else $ score -= pow ( $ chunkSize , 2 ) ; } return $ score ; } $ str = "11011" ; echo calcScore ( $ str ) ; ? >
Number of sub | Function to return the count of required sub - strings ; Left and right counters for characters on both sides of sub - string window ; Left and right pointer on both sides of sub - string window ; Initialize the frequency ; Result and length of string ; Initialize the left pointer ; Initialize the right pointer ; Traverse all the window sub - strings ; Counting the characters on left side of the sub - string window ; Counting the characters on right side of the sub - string window ; Add the possible sub - strings on both sides to result ; Setting the frequency for next sub - string window ; Reset the left and right counters ; Driver code
< ? php function countSubString ( $ s , $ c , $ k ) { $ leftCount = 0 ; $ rightCount = 0 ; $ left = 0 ; $ right = 0 ; $ freq = 0 ; $ result = 0 ; $ len = strlen ( $ s ) ; while ( $ s [ $ left ] != $ c && $ left < $ len ) { $ left ++ ; $ leftCount ++ ; } $ right = $ left + 1 ; while ( $ freq != ( $ k - 1 ) && ( $ right - 1 ) < $ len ) { if ( $ s [ $ right ] == $ c ) $ freq ++ ; $ right ++ ; } while ( $ left < $ len && ( $ right - 1 ) < $ len ) { while ( $ s [ $ left ] != $ c && $ left < $ len ) { $ left ++ ; $ leftCount ++ ; } while ( $ right < $ len && $ s [ $ right ] != $ c ) { if ( $ s [ $ right ] == $ c ) $ freq ++ ; $ right ++ ; $ rightCount ++ ; } $ result = $ result + ( $ leftCount + 1 ) * ( $ rightCount + 1 ) ; $ freq = $ k - 1 ; $ leftCount = 0 ; $ rightCount = 0 ; $ left ++ ; $ right ++ ; } return $ result ; } $ s = " abada " ; $ c = ' a ' ; $ k = 2 ; echo countSubString ( $ s , $ c , $ k ) , " STRNEWLINE " ; ? >
Check whether the given string is a valid identifier | Function that returns true if str is a valid identifier ; If first character is invalid ; Traverse the string for the rest of the characters ; String is a valid identifier ; Driver code
< ? php function isValid ( $ str , $ n ) { if ( ! ( ( $ str [ 0 ] >= ' a ' && $ str [ 0 ] <= ' z ' ) || ( $ str [ 0 ] >= ' A ' && $ str [ 0 ] <= ' Z ' ) $ str [ 0 ] == ' _ ' ) ) return false ; for ( $ i = 1 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( ! ( ( $ str [ $ i ] >= ' a ' && $ str [ $ i ] <= ' z ' ) || ( $ str [ $ i ] >= ' A ' && $ str [ $ i ] <= ' Z ' ) || ( $ str [ $ i ] >= '0' && $ str [ $ i ] <= '9' ) $ str [ $ i ] == ' _ ' ) ) return false ; } return true ; } $ str = " _ geeks123" ; $ n = strlen ( $ str ) ; if ( isValid ( $ str , $ n ) ) print ( " Valid " ) ; else print ( " Invalid " ) ; ? >
Capitalize the first and last character of each word in a string | PHP program to capitalise the first and last character of each word in a string . ; Create an equivalent string of the given string ; $k stores index of first character and $i is going to store index of last character . ; Check if the character is a small letter If yes , then Capitalise ; Driver code
< ? php function FirstAndLast ( $ str ) { $ ch = $ str ; for ( $ i = 0 ; $ i < strlen ( $ ch ) ; $ i ++ ) { $ k = $ i ; while ( $ i < strlen ( $ ch ) && $ ch [ $ i ] != ' ▁ ' ) $ i ++ ; $ ch [ $ k ] = chr ( ( $ ch [ $ k ] >= ' a ' && $ ch [ $ k ] <= ' z ' ) ? ( ord ( $ ch [ $ k ] ) - 32 ) : ( ord ( $ ch [ $ k ] ) ) ) ; $ ch [ $ i - 1 ] = chr ( ( $ ch [ $ i - 1 ] >= ' a ' && $ ch [ $ i - 1 ] <= ' z ' ) ? ( ord ( $ ch [ $ i - 1 ] ) - 32 ) : ( ord ( $ ch [ $ i - 1 ] ) ) ) ; } return $ ch ; } $ str = " Geeks ▁ for ▁ Geeks " ; echo $ str , " STRNEWLINE " ; echo FirstAndLast ( $ str ) ; ? >
Find the number of players who roll the dice when the dice output sequence is given | Function to return the number of players ; Initialize cnt as 0 ; Iterate in the string ; Check for numbers other than x ; Driver code
< ? php function findM ( $ s , $ x ) { $ cnt = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( ord ( $ s [ $ i ] ) - ord ( '0' ) != $ x ) $ cnt ++ ; } return $ cnt ; } $ s = "3662123" ; $ x = 6 ; echo findM ( $ s , $ x ) ; ? >
Find the longest sub | Function to find longest prefix suffix ; To store longest prefix suffix ; Length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; Loop calculates lps [ i ] for i = 1 to n - 1 ; ( pat [ i ] != pat [ len ] ) ; If len = 0 ; Function to find the longest substring which is prefix as well as a sub - string of s [ 1. . . n - 2 ] ; Find longest prefix suffix ; If lps of n - 1 is zero ; At any position lps [ i ] equals to lps [ n - 1 ] ; If answer is not possible ; Driver code ; function call
< ? php function compute_lps ( $ s ) { $ n = strlen ( $ s ) ; $ lps = array ( ) ; $ len = 0 ; $ lps [ 0 ] = 0 ; $ i = 1 ; while ( $ i < $ n ) { if ( $ s [ $ i ] == $ s [ $ len ] ) { $ len ++ ; $ lps [ $ i ] = $ len ; $ i ++ ; } else { if ( $ len != 0 ) $ len = $ lps [ $ len - 1 ] ; else { $ lps [ $ i ] = 0 ; $ i ++ ; } } } return $ lps ; } function Longestsubstring ( $ s ) { $ lps = compute_lps ( $ s ) ; $ n = strlen ( $ s ) ; if ( $ lps [ $ n - 1 ] == 0 ) { echo - 1 ; return ; } for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ lps [ $ i ] == $ lps [ $ n - 1 ] ) { echo substr ( $ s , 0 , $ lps [ $ i ] ) ; return ; } } if ( $ lps [ $ lps [ $ n - 1 ] - 1 ] == 0 ) echo - 1 ; else echo substr ( $ s , 0 , $ lps [ $ lps [ $ n - 1 ] - 1 ] ) ; } $ s = " fixprefixsuffix " ; Longestsubstring ( $ s ) ; ? >
Pairs of strings which on concatenating contains each character of " string " | PHP implementation of the approach ; Function to return the bitmask for the string ; Function to return the count of pairs ; bitMask [ i ] will store the count of strings from the array whose bitmask is i ; To store the count of pairs ; MAX - 1 = 63 i . e . 111111 in binary ; arr [ i ] cannot make s pair with itself i . e . ( arr [ i ] , arr [ i ] ) ; Driver code
< ? php $ MAX = 64 ; function getBitmask ( $ s ) { $ temp = 0 ; for ( $ j = 0 ; $ j < strlen ( $ s ) ; $ j ++ ) { if ( $ s [ $ j ] == ' s ' ) { $ temp = $ temp | ( 1 ) ; } else if ( $ s [ $ j ] == ' t ' ) { $ temp = $ temp | ( 2 ) ; } else if ( $ s [ $ j ] == ' r ' ) { $ temp = $ temp | ( 4 ) ; } else if ( $ s [ $ j ] == ' i ' ) { $ temp = $ temp | ( 8 ) ; } else if ( $ s [ $ j ] == ' n ' ) { $ temp = $ temp | ( 16 ) ; } else if ( $ s [ $ j ] == ' g ' ) { $ temp = $ temp | ( 32 ) ; } } return $ temp ; } function countPairs ( $ arr , $ n ) { $ bitMask = array_fill ( 0 , $ GLOBALS [ ' MAX ' ] , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ bitMask [ getBitmask ( $ arr [ $ i ] ) ] ++ ; $ cnt = 0 ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' MAX ' ] ; $ i ++ ) { for ( $ j = $ i ; $ j < $ GLOBALS [ ' MAX ' ] ; $ j ++ ) { if ( ( $ i $ j ) == ( $ GLOBALS [ ' MAX ' ] - 1 ) ) { if ( $ i == $ j ) $ cnt += floor ( ( $ bitMask [ $ i ] * $ bitMask [ $ i ] - 1 ) / 2 ) ; else $ cnt += ( $ bitMask [ $ i ] * $ bitMask [ $ j ] ) ; } } } return $ cnt ; } $ arr = array ( " strrr " , " string " , " gstrin " ) ; $ n = count ( $ arr ) ; echo countPairs ( $ arr , $ n ) ; ? >