codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the binary matrix by filpping submatrix once | PHP program to find maximum number of ones after one flipping in Binary Matrix ; Return number of ones in square submatrix of size k x k starting from ( x , y ) ; Return maximum number of 1 s after flipping a submatrix ; Precomputing the number of 1 s ; Finding the maximum number of 1 s after flipping ; Driver code
< ? php $ R = 3 ; $ C = 3 ; function cal ( $ ones , $ x , $ y , $ k ) { return $ ones [ $ x + $ k - 1 ] [ $ y + $ k - 1 ] - $ ones [ $ x - 1 ] [ $ y + $ k - 1 ] - $ ones [ $ x + $ k - 1 ] [ $ y - 1 ] + $ ones [ $ x - 1 ] [ $ y - 1 ] ; } function sol ( $ mat ) { global $ C , $ R ; $ ans = 0 ; $ ones = array_fill ( 0 , $ R + 1 , array_fill ( 0 , $ C + 1 , 0 ) ) ; for ( $ i = 1 ; $ i <= $ R ; $ i ++ ) for ( $ j = 1 ; $ j <= $ C ; $ j ++ ) $ ones [ $ i ] [ $ j ] = $ ones [ $ i - 1 ] [ $ j ] + $ ones [ $ i ] [ $ j - 1 ] - $ ones [ $ i - 1 ] [ $ j - 1 ] + ( int ) ( $ mat [ $ i - 1 ] [ $ j - 1 ] == 1 ) ; for ( $ k = 1 ; $ k <= min ( $ R , $ C ) ; $ k ++ ) for ( $ i = 1 ; $ i + $ k - 1 <= $ R ; $ i ++ ) for ( $ j = 1 ; $ j + $ k - 1 <= $ C ; $ j ++ ) $ ans = max ( $ ans , ( $ ones [ $ R ] [ $ C ] + $ k * $ k - 2 * cal ( $ ones , $ i , $ j , $ k ) ) ) ; return $ ans ; } $ mat = array ( array ( 0 , 0 , 1 ) , array ( 0 , 0 , 1 ) , array ( 1 , 0 , 1 ) ) ; echo sol ( $ mat ) ; ? >
Minimum steps to minimize n as per given condition | A tabulation based solution in PHP ; Driver Code
< ? php function getMinSteps ( $ n ) { $ table = array ( ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ table [ $ i ] = $ n - $ i ; for ( $ i = $ n ; $ i >= 1 ; $ i -- ) { if ( ! ( $ i % 2 ) ) $ table [ $ i / 2 ] = min ( $ table [ $ i ] + 1 , $ table [ $ i / 2 ] ) ; if ( ! ( $ i % 3 ) ) $ table [ $ i / 3 ] = min ( $ table [ $ i ] + 1 , $ table [ $ i / 3 ] ) ; } return $ table [ 1 ] ; } $ n = 10 ; echo getMinSteps ( $ n ) ; ? >
Longest subsequence such that difference between adjacents is one | Function to find the length of longest subsequence ; Initialize the dp [ ] array with 1 as a single element will be of 1 length ; Start traversing the given array ; Compare with all the previous elements ; If the element is consecutive then consider this subsequence and update dp [ i ] if required . ; Longest length will be the maximum value of dp array . ; Longest subsequence with one difference is { 1 , 2 , 3 , 4 , 3 , 2 }
< ? php function longestSubseqWithDiffOne ( $ arr , $ n ) { $ dp [ $ n ] = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ dp [ $ i ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ i ; $ j ++ ) { if ( ( $ arr [ $ i ] == $ arr [ $ j ] + 1 ) || ( $ arr [ $ i ] == $ arr [ $ j ] - 1 ) ) $ dp [ $ i ] = max ( $ dp [ $ i ] , $ dp [ $ j ] + 1 ) ; } } $ result = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ result < $ dp [ $ i ] ) $ result = $ dp [ $ i ] ; return $ result ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 3 , 2 ) ; $ n = sizeof ( $ arr ) ; echo longestSubseqWithDiffOne ( $ arr , $ n ) ; ? >
Non | A dynamic programming based function to find nth Catalan number ; Initialize first two values in table ; Fill entries in catalan [ ] using recursive formula ; Return last entry ; Returns count of ways to connect n points on a circle such that no two connecting lines cross each other and every point is connected with one other point . ; Throw error if n is odd ; Else return n / 2 'th Catalan number ; Driver Code
< ? php function catalanDP ( $ n ) { $ catalan [ 0 ] = $ catalan [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ catalan [ $ i ] = 0 ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ catalan [ $ i ] += $ catalan [ $ j ] * $ catalan [ $ i - $ j - 1 ] ; } return $ catalan [ $ n ] ; } function countWays ( $ n ) { if ( $ n & 1 ) { echo " Invalid " ; return 0 ; } return catalanDP ( $ n / 2 ) ; } echo countWays ( 6 ) , " " ; ? >
Ways to arrange Balls such that adjacent balls are of different types | PHP program to count number of ways to arrange three types of balls such that no two balls of same color are adjacent to each other ; table to store to store results of subproblems ; Returns count of arrangements where last placed ball is ' last ' . ' last ' is 0 for ' p ' , 1 for ' q ' and 2 for ' r ' ; if number of balls of any color becomes less than 0 the number of ways arrangements is 0. ; If last ball required is of type P and the number of balls of P type is 1 while number of balls of other color is 0 the number of ways is 1. ; Same case as above for ' q ' and ' r ' ; If this subproblem is already evaluated ; if last ball required is P and the number of ways is the sum of number of ways to form sequence with ' p - 1' P balls , q Q Balls and r R balls ending with Q and R . ; Same as above case for ' q ' and ' r ' ; else ( last == 2 ) ; Returns count of required arrangements ; Three cases arise : return countWays ( $p , $q , $r , 0 ) + Last required balls is type P countWays ( $p , $q , $r , 1 ) + Last required balls is type Q countWays ( $p , $q , $r , 2 ) ; Last required balls is type R ; Driver code
< ? php $ MAX = 100 ; $ dp = array_fill ( 0 , $ MAX , array_fill ( 0 , $ MAX , array_fill ( 0 , $ MAX , array_fill ( 0 , 3 , -1 ) ) ) ) ; function countWays ( $ p , $ q , $ r , $ last ) { global $ dp ; if ( $ p < 0 $ q < 0 $ r < 0 ) return 0 ; if ( $ p == 1 && $ q == 0 && $ r == 0 && $ last == 0 ) return 1 ; if ( $ p == 0 && $ q == 1 && $ r == 0 && $ last == 1 ) return 1 ; if ( $ p == 0 && $ q == 0 && $ r == 1 && $ last == 2 ) return 1 ; if ( $ dp [ $ p ] [ $ q ] [ $ r ] [ $ last ] != -1 ) return $ dp [ $ p ] [ $ q ] [ $ r ] [ $ last ] ; if ( $ last == 0 ) $ dp [ $ p ] [ $ q ] [ $ r ] [ $ last ] = countWays ( $ p - 1 , $ q , $ r , 1 ) + countWays ( $ p - 1 , $ q , $ r , 2 ) ; else if ( $ last == 1 ) $ dp [ $ p ] [ $ q ] [ $ r ] [ $ last ] = countWays ( $ p , $ q - 1 , $ r , 0 ) + countWays ( $ p , $ q - 1 , $ r , 2 ) ; $ dp [ $ p ] [ $ q ] [ $ r ] [ $ last ] = countWays ( $ p , $ q , $ r - 1 , 0 ) + countWays ( $ p , $ q , $ r - 1 , 1 ) ; return $ dp [ $ p ] [ $ q ] [ $ r ] [ $ last ] ; } function countUtil ( $ p , $ q , $ r ) { } $ p = 1 ; $ q = 1 ; $ r = 1 ; print ( countUtil ( $ p , $ q , $ r ) ) ; ? >
Count Derangements ( Permutation such that no element appears in its original position ) | Function to count derangements ; Base cases ; countDer ( n ) = ( n - 1 ) [ countDer ( n - 1 ) + der ( n - 2 ) ] ; Driver Code
< ? php function countDer ( $ n ) { if ( $ n == 1 ) return 0 ; if ( $ n == 2 ) return 1 ; return ( $ n - 1 ) * ( countDer ( $ n - 1 ) + countDer ( $ n - 2 ) ) ; } $ n = 4 ; echo " Count ▁ of ▁ Derangements ▁ is ▁ " , countDer ( $ n ) ; ? >
Count Derangements ( Permutation such that no element appears in its original position ) | Function to count derangements ; Base cases ; Fill der [ 0. . n ] in bottom up manner using above recursive formula ; Return result for n ; Driver code
< ? php function countDer ( $ n ) { $ der [ 1 ] = 0 ; $ der [ 2 ] = 1 ; for ( $ i = 3 ; $ i <= $ n ; ++ $ i ) $ der [ $ i ] = ( $ i - 1 ) * ( $ der [ $ i - 1 ] + $ der [ $ i - 2 ] ) ; return $ der [ $ n ] ; } $ n = 4 ; echo " Count ▁ of ▁ Derangements ▁ is ▁ " , countDer ( $ n ) ; ? >
Find number of solutions of a linear equation of n variables | Returns count of solutions for given rhs and coefficients coeff [ 0. . n - 1 ] ; Create and initialize a table to store results of subproblems ; Fill table in bottom up manner ; Driver Code
< ? php function countSol ( $ coeff , $ n , $ rhs ) { $ dp = str_repeat ( " \0" , 256 ) ; $ dp [ 0 ] = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ coeff [ $ i ] ; $ j <= $ rhs ; $ j ++ ) $ dp [ $ j ] = $ dp [ $ j ] + ( $ dp [ $ j - $ coeff [ $ i ] ] ) ; return $ dp [ $ rhs ] ; } $ coeff = array ( 2 , 2 , 5 ) ; $ rhs = 4 ; $ n = sizeof ( $ coeff ) / sizeof ( $ coeff [ 0 ] ) ; echo countSol ( $ coeff , $ n , $ rhs ) ; ? >
Maximum Product Cutting | DP | The main function that returns the max possible product ; n equals to 2 or 3 must be handled explicitly ; Keep removing parts of size 3 while n is greater than 4 ; Keep multiplying 3 to res ; The last part multiplied by previous parts ; Driver code
< ? php function maxProd ( $ n ) { if ( $ n == 2 $ n == 3 ) return ( $ n - 1 ) ; $ res = 1 ; while ( $ n > 4 ) { $ n = $ n - 3 ; $ res = $ res * 3 ; } return ( $ n * $ res ) ; } echo ( " Maximum ▁ Product ▁ is ▁ " ) ; echo ( maxProd ( 10 ) ) ; ? >
Dice Throw | DP | The main function that returns number of ways to get sum ' x ' with ' n ' dice and ' m ' with m faces . ; Create a table to store results of subproblems . One extra row and column are used for simpilicity ( Number of dice is directly used as row index and sum is directly used as column index ) . The entries in 0 th row and 0 th column are never used . ; Table entries for only one dice ; Fill rest of the entries in table using recursive relation i : number of dice , j : sum ; Return value ; Driver Code
< ? php function findWays ( $ m , $ n , $ x ) { $ table ; for ( $ i = 1 ; $ i < $ n + 1 ; $ i ++ ) for ( $ j = 1 ; $ j < $ x + 1 ; $ j ++ ) $ table [ $ i ] [ $ j ] = 0 ; for ( $ j = 1 ; $ j <= $ m && $ j <= $ x ; $ j ++ ) $ table [ 1 ] [ $ j ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) for ( $ j = 1 ; $ j <= $ x ; $ j ++ ) for ( $ k = 1 ; $ k <= $ m && $ k < $ j ; $ k ++ ) $ table [ $ i ] [ $ j ] += $ table [ $ i - 1 ] [ $ j - $ k ] ; return $ table [ $ n ] [ $ x ] ; } echo findWays ( 4 , 2 , 1 ) . " STRNEWLINE " ; echo findWays ( 2 , 2 , 3 ) . " STRNEWLINE " ; echo findWays ( 6 , 3 , 8 ) . " STRNEWLINE " ; echo findWays ( 4 , 2 , 5 ) . " STRNEWLINE " ; echo findWays ( 4 , 3 , 5 ) . " STRNEWLINE " ; ? >
Longest Palindromic Subsequence | DP | Returns the length of the longest palindromic subsequence in seq ; Base Case 1 : If there is only 1 character ; Base Case 2 : If there are only 2 characters and both are same ; If the first and last characters match ; If the first and last characters do not match ; Driver Code
< ? php function lps ( $ seq , $ i , $ j ) { if ( $ i == $ j ) return 1 ; if ( $ seq [ $ i ] == $ seq [ $ j ] && $ i + 1 == $ j ) return 2 ; if ( $ seq [ $ i ] == $ seq [ $ j ] ) return lps ( $ seq , $ i + 1 , $ j - 1 ) + 2 ; return max ( lps ( $ seq , $ i , $ j - 1 ) , lps ( $ seq , $ i + 1 , $ j ) ) ; } $ seq = " GEEKSFORGEEKS " ; $ n = strlen ( $ seq ) ; echo " The ▁ length ▁ of ▁ the ▁ LPS ▁ is ▁ " . lps ( $ seq , 0 , $ n - 1 ) ; ? >
Count occurrences of a string that can be constructed from another given string | Function to find the count ; Initialize hash for both strings ; hash the frequency of letters of str1 ; hash the frequency of letters of str2 ; Find the count of str2 constructed from str1 ; Return answer ; Driver code
< ? php function findCount ( $ str1 , $ str2 ) { $ len = strlen ( $ str1 ) ; $ len2 = strlen ( $ str1 ) ; $ ans = PHP_INT_MAX ; $ hash1 = array_fill ( 0 , 26 , 0 ) ; $ hash2 = array_fill ( 0 , 26 , 0 ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) $ hash1 [ ord ( $ str1 [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ len2 ; $ i ++ ) $ hash2 [ ord ( $ str2 [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) if ( $ hash2 [ $ i ] ) $ ans = min ( $ ans , $ hash1 [ $ i ] / $ hash2 [ $ i ] ) ; return $ ans ; } $ str1 = " geeksclassesatnoida " ; $ str2 = " sea " ; echo findCount ( $ str1 , $ str2 ) ; ? >
Check if a string is the typed name of the given name | Check if the character is vowel or not ; Returns true if ' typed ' is a typed name given str ; Traverse through all characters of str . ; If current characters do not match ; If not vowel , simply move ahead in both ; Count occurrences of current vowel in str ; Count occurrences of current vowel in typed . ; Driver code
< ? php function isVowel ( $ c ) { $ vowel = " aeiou " ; for ( $ i = 0 ; $ i < strlen ( $ vowel ) ; ++ $ i ) if ( $ vowel [ $ i ] == $ c ) return true ; return false ; } function printRLE ( $ str , $ typed ) { $ n = strlen ( $ str ) ; $ m = strlen ( $ typed ) ; $ j = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] != $ typed [ $ j ] ) return false ; if ( isVowel ( $ str [ $ i ] ) == false ) { $ j ++ ; continue ; } $ count1 = 1 ; while ( $ i < $ n - 1 && $ str [ $ i ] == $ str [ $ i + 1 ] ) { $ count1 ++ ; $ i ++ ; } $ count2 = 1 ; while ( $ j < $ m - 1 && $ typed [ $ j ] == $ str [ $ i ] ) { $ count2 ++ ; $ j ++ ; } if ( $ count1 > $ count2 ) return false ; } return true ; } $ name = " alex " ; $ typed = " aaalaeex " ; if ( printRLE ( $ name , $ typed ) ) echo " Yes " ; else echo " No " ; ? >
Program to replace a word with asterisks in a sentence | Function takes two parameter ; Break down sentence by ' ▁ ' spaces and store each individual word in a different list ; A new string to store the result ; Creating the censor which is an asterisks " * " text of the length of censor word ; count variable to access our word_list ; Iterating through our list of extracted words ; changing the censored word to created asterisks censor ; join the words ; Driver code
< ? php function censor ( $ text , $ word ) { $ word_list = explode ( " ▁ " , $ text ) ; $ result = ' ' $ stars = " " ; for ( $ i = 0 ; $ i < strlen ( $ word ) ; $ i ++ ) $ stars . = " * " ; $ count = 0 ; $ index = 0 ; for ( $ i = 0 ; $ i < sizeof ( $ word_list ) ; $ i ++ ) { if ( $ word_list [ $ i ] == $ word ) $ word_list [ $ index ] = $ stars ; $ index += 1 ; } return implode ( ' ▁ ' , $ word_list ) ; } $ extract = " GeeksforGeeks ▁ is ▁ a ▁ computer ▁ science ▁ " . " portal for geeks . I am pursuing my " . STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL " major in computer science . " $ cen = " computer " ; echo censor ( $ extract , $ cen ) ; ? >
Find the time which is palindromic and comes after the given time | Function to return the required time ; To store the resultant time ; Hours are stored in h as integer ; Minutes are stored in m as integer ; Reverse of h ; Reverse of h as a string ; If MM < reverse of ( HH ) ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Increment hours ; Reverse of the hour after incrementing 1 ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Driver code
< ? php function getTime ( $ s , $ n ) { $ res = " " ; $ h = intval ( $ s . substr ( 0 , 2 ) ) ; $ m = intval ( $ s . substr ( 3 , 2 ) ) ; $ rev_h = ( $ h % 10 ) * 10 + ( ( $ h % 100 ) - ( $ h % 10 ) ) / 10 ; $ rev_hs = strval ( $ rev_h ) ; if ( $ h == 23 && $ m >= 32 ) { $ res = " - 1" ; } else if ( $ m < $ rev_h ) { $ temp = " " ; if ( $ h < 10 ) $ temp = "0" ; $ temp = $ temp . strval ( $ h ) ; if ( $ rev_h < 10 ) $ res = $ res . $ temp . " : 0" . $ rev_hs ; else $ res = $ res . $ temp . " : " . $ rev_hs ; } else { $ h ++ ; $ rev_h = ( $ h % 10 ) * 10 + ( ( $ h % 100 ) - ( $ h % 10 ) ) / 10 ; $ rev_hs = strval ( $ rev_h ) ; $ temp = " " ; if ( $ h < 10 ) $ temp = "0" ; $ temp = $ temp . strval ( $ h ) ; if ( $ rev_h < 10 ) $ res = $ res . $ temp . " : 0" . $ rev_hs ; else $ res = $ res . $ temp . " : " . $ rev_hs ; } return $ res ; } $ s = "21:12" ; $ n = strlen ( $ s ) ; echo getTime ( $ s , $ n ) ; return 0 ; ? >
Count of sub | Function to return the count of valid sub - strings ; Variable ans to store all the possible substrings Initialize its value as total number of substrings that can be formed from the given string ; Stores recent index of the characters ; If character is a update a 's index  and the variable ans ; If character is b update b 's index  and the variable ans ; If character is c update c 's index  and the variable ans ; Driver code
< ? php function CountSubstring ( $ str , $ n ) { $ ans = ( $ n * ( $ n + 1 ) ) / 2 ; $ a_index = 0 ; $ b_index = 0 ; $ c_index = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == ' a ' ) { $ a_index = $ i + 1 ; $ ans -= min ( $ b_index , $ c_index ) ; } else if ( $ str [ $ i ] == ' b ' ) { $ b_index = $ i + 1 ; $ ans -= min ( $ a_index , $ c_index ) ; } else { $ c_index = $ i + 1 ; $ ans -= min ( $ a_index , $ b_index ) ; } } return $ ans ; } { $ str = str_split ( " babac " ) ; $ n = sizeof ( $ str ) ; echo ( CountSubstring ( $ str , $ n ) ) ; }
Check if a binary string contains consecutive same or not | Function that returns true is str is valid ; Assuming the string is binary If any two consecutive characters are equal then the string is invalid ; If the string is alternating ; Driver code
< ? php function isValid ( $ str , $ len ) { for ( $ i = 1 ; $ i < $ len ; $ i ++ ) { if ( $ str [ $ i ] == $ str [ $ i - 1 ] ) return false ; } return true ; } $ str = "0110" ; $ len = strlen ( $ str ) ; if ( isValid ( $ str , $ len ) ) echo " Valid " ; else echo " Invalid " ; ? >
Minimum K such that every substring of length atleast K contains a character c | This function checks if there exists some character which appears in all K length substrings ; Iterate over all possible characters ; stores the last occurrence ; set answer as true ; ; No occurrence found of current character in first substring of length K ; Check for every last substring of length K where last occurr - ence exists in substring ; If last occ is not present in substring ; current character is K amazing ; This function performs binary search over the answer to minimise it ; Check if answer is found try to minimise it ; Driver Code
< ? php function check ( $ s , $ K ) { for ( $ ch = 0 ; $ ch < 26 ; $ ch ++ ) { $ c = chr ( ord ( ' a ' ) + $ ch ) ; $ last = -1 ; $ found = true ; for ( $ i = 0 ; $ i < $ K ; $ i ++ ) if ( $ s [ $ i ] == $ c ) $ last = $ i ; if ( $ last == -1 ) continue ; for ( $ i = $ K ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ s [ $ i ] == $ c ) $ last = $ i ; if ( $ last <= ( $ i - $ K ) ) { $ found = false ; break ; } } if ( $ found ) return 1 ; } return 0 ; } function binarySearch ( $ s ) { $ low = 1 ; $ high = strlen ( $ s ) ; while ( $ low <= $ high ) { $ mid = ( $ high + $ low ) >> 1 ; if ( check ( $ s , $ mid ) ) { $ ans = $ mid ; $ high = $ mid - 1 ; } else $ low = $ mid + 1 ; } return $ ans ; } $ s = " abcde " ; echo binarySearch ( $ s ) . " STRNEWLINE " ; $ s = " aaaa " ; echo binarySearch ( $ s ) . " STRNEWLINE " ; ? >
Check if number can be displayed using seven segment led | Pre - computed values of segment used by digit 0 to 9. ; Check if it is possible to display the number ; Finding sum of the segments used by each digit of the number ; Driver Code ; Function call to print required answer
< ? php $ seg = array ( 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ) ; function LedRequired ( $ s , $ led ) { $ count = 0 ; global $ seg ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; ++ $ i ) { $ count += $ seg [ ord ( $ s [ $ i ] ) - 48 ] ; } if ( $ count <= $ led ) return " YES " ; else return " NO " ; } $ S = "123456789" ; $ led = 20 ; echo LedRequired ( $ S , $ led ) ; ? >
Count the number of vowels occurring in all the substrings of given string | Returns the total sum of occurrences of all vowels ; No . of occurrences of 0 th character in all the substrings ; No . of occurrences of ith character in all the substrings ; Check if ith character is a vowel ; Return the total sum of occurrences of vowels ; Driver Code
< ? php function vowel_calc ( $ s ) { $ n = strlen ( $ s ) ; $ arr = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i == 0 ) $ arr [ $ i ] = $ n ; else $ arr [ $ i ] = ( $ n - $ i ) + $ arr [ $ i - 1 ] - $ i ; } $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == ' a ' $ s [ $ i ] == ' e ' $ s [ $ i ] == ' i ' $ s [ $ i ] == ' o ' $ s [ $ i ] == ' u ' ) $ sum += $ arr [ $ i ] ; } return $ sum ; } $ s = " daceh " ; echo ( vowel_calc ( $ s ) ) ; ? >
Count all palindrome which is square of a palindrome | check if a number is a palindrome ; Function to return required count of palindromes ; Range [ L , R ] ; Upper limit ; count odd length palindromes ; if s = '1234' ; then , t = '1234321' ; count even length palindromes ; if s = '1234' ; then , t = '12344321' ; Return count of super - palindromes ; Driver Code ; function call to get required answer
< ? php function ispalindrome ( $ x ) { $ ans = 0 ; $ temp = $ x ; while ( $ temp > 0 ) { $ ans = ( 10 * $ ans ) + ( $ temp % 10 ) ; $ temp = ( int ) ( $ temp / 10 ) ; } return $ ans == $ x ; } function SuperPalindromes ( $ L , $ R ) { $ L = ( int ) $ L ; $ R = ( int ) $ R ; $ LIMIT = 100000 ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ LIMIT ; $ i ++ ) { $ s = ( string ) $ i ; $ rs = substr ( $ s , 0 , strlen ( $ s ) - 1 ) ; $ p = $ s . strrev ( $ rs ) ; $ p_sq = ( int ) $ p * * 2 ; if ( $ p_sq > $ R ) { break ; } if ( $ p_sq >= $ L and ispalindrome ( $ p_sq ) ) { $ ans = $ ans + 1 ; } } for ( $ i = 0 ; $ i < $ LIMIT ; $ i ++ ) { $ s = ( string ) $ i ; $ p = $ s . strrev ( $ s ) ; $ p_sq = ( int ) $ p * * 2 ; if ( $ p_sq > $ R ) { break ; } if ( $ p_sq >= $ L and ispalindrome ( $ p_sq ) ) { $ ans = $ ans + 1 ; } } return $ ans ; } $ L = "4" ; $ R = "1000" ; echo SuperPalindromes ( $ L , $ R ) ; ? >
Sub | Function that counts all the sub - strings of length ' k ' which have all identical characters ; count of sub - strings , length , initial position of sliding window ; map to store the frequency of the characters of sub - string ; increase the frequency of the character and length of the sub - string ; if the length of the sub - string is greater than K ; remove the character from the beginning of sub - string ; if the length of the sub string is equal to k and frequency of one of its characters is equal to the length of the sub - string i . e . all the characters are same increase the count ; display the number of valid sub - strings ; Driver code
< ? php function solve ( $ s , $ k ) { $ count = 0 ; $ length = 0 ; $ pos = 0 ; $ m = array ( ) ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { $ m [ $ s [ $ i ] ] ++ ; $ length ++ ; if ( $ length > $ k ) { $ m [ $ s [ $ pos ++ ] ] -- ; $ length -- ; } if ( $ length == $ k && $ m [ $ s [ $ i ] ] == $ length ) $ count ++ ; } echo $ count . " STRNEWLINE " ; } $ s = " aaaabbbccdddd " ; $ k = 4 ; solve ( $ s , $ k ) ; ? >
Program to replace every space in a string with hyphen | Get the String ; Traverse the string character by character . ; Changing the ith character to ' - ' if it 's a space. ; Print the modified string .
< ? php $ str = " A ▁ computer ▁ science ▁ portal ▁ for ▁ geeks " ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; ++ $ i ) { if ( $ str [ $ i ] == ' ▁ ' ) { $ str [ $ i ] = ' - ' ; } } echo $ str . " STRNEWLINE " ;
Check if any permutation of a large number is divisible by 8 | PHP program to check if any permutation of a large number is divisible by 8 or not ; Function to check if any permutation of a large number is divisible by 8 ; Less than three digit number can be checked directly . ; check for the reverse of a number ; Stores the Frequency of characters in the n . ; Iterates for all three digit numbers divisible by 8 ; stores the frequency of all single digit in three - digit number ; check if the original number has the digit ; when all are checked its not possible ; Driver Code
< ? php error_reporting ( 0 ) ; function solve ( $ n , $ l ) { if ( $ l < 3 ) { if ( intval ( $ n ) % 8 == 0 ) return true ; strrev ( $ n ) ; if ( intval ( $ n ) % 8 == 0 ) return true ; return false ; } $ hash [ 10 ] = array ( 0 ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) $ hash [ $ n [ $ i ] - '0' ] ++ ; for ( $ i = 104 ; $ i < 1000 ; $ i += 8 ) { $ dup = $ i ; $ freq [ 10 ] = array ( 0 ) ; $ freq [ $ dup % 10 ] ++ ; $ dup = $ dup / 10 ; $ freq [ $ dup % 10 ] ++ ; $ dup = $ dup / 10 ; $ freq [ $ dup % 10 ] ++ ; $ dup = $ i ; if ( $ freq [ $ dup % 10 ] > $ hash [ $ dup % 10 ] ) continue ; $ dup = $ dup / 10 ; if ( $ freq [ $ dup % 10 ] > $ hash [ $ dup % 10 ] ) continue ; $ dup = $ dup / 10 ; if ( $ freq [ $ dup % 10 ] > $ hash [ $ dup % 10 ] ) continue ; return true ; } return false ; } $ number = "31462708" ; $ l = strlen ( $ number ) ; if ( solve ( $ number , $ l ) ) echo " Yes " ; else echo " No " ;