codeparrot/xlcost-text-to-code huggingface dataset:
Counting numbers of n digits that are monotone | PHP program to count numbers of n digits that are monotone . ; Considering all possible digits as { 1 , 2 , 3 , . .9 } ; DP [ i ] [ j ] is going to store monotone numbers of length i + 1 considering j + 1 digits . ; Unit length numbers ; Single digit numbers ; Filling rest of the entries in bottom up manner . ; Driver code
< ? php function getNumMonotone ( $ len ) { $ DP_s = 9 ; $ DP = array ( array_fill ( 0 , $ len , 0 ) , array_fill ( 0 , $ len , 0 ) ) ; for ( $ i = 0 ; $ i < $ DP_s ; ++ $ i ) $ DP [ 0 ] [ $ i ] = $ i + 1 ; for ( $ i = 0 ; $ i < $ len ; ++ $ i ) $ DP [ $ i ] [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ len ; ++ $ i ) for ( $ j = 1 ; $ j < $ DP_s ; ++ $ j ) $ DP [ $ i ] [ $ j ] = $ DP [ $ i - 1 ] [ $ j ] + $ DP [ $ i ] [ $ j - 1 ] ; return $ DP [ $ len - 1 ] [ $ DP_s - 1 ] ; } echo getNumMonotone ( 10 ) ; ? >
Newman | Function to find the n - th element ; Declare array to store sequence ; Driver Code
< ? php function sequence ( $ n ) { $ i ; $ f [ 0 ] = 0 ; $ f [ 1 ] = 1 ; $ f [ 2 ] = 1 ; for ( $ i = 3 ; $ i <= $ n ; $ i ++ ) $ f [ $ i ] = $ f [ $ f [ $ i - 1 ] ] + $ f [ $ i - $ f [ $ i - 1 ] ] ; return $ f [ $ n ] ; } $ n = 10 ; echo ( sequence ( $ n ) ) ; ? >
Maximum product of an increasing subsequence | Returns product of maximum product increasing subsequence . ; Initialize MPIS values ; Compute optimized MPIS values considering every element as ending element of sequence ; Pick maximum of all product values ; Driver program to test above function
< ? php function lis ( & $ arr , $ n ) { $ mpis = array_fill ( 0 , $ n , NULL ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ mpis [ $ i ] = $ arr [ $ i ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] > $ arr [ $ j ] && $ mpis [ $ i ] < ( $ mpis [ $ j ] * $ arr [ $ i ] ) ) $ mpis [ $ i ] = $ mpis [ $ j ] * $ arr [ $ i ] ; return max ( $ mpis ) ; } $ arr = array ( 3 , 100 , 4 , 5 , 150 , 6 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo lis ( $ arr , $ n ) ; return 0 ; ? >
Hosoya 's Triangle | PHP Program to print Hosoya 's triangle of height n. ; Base case ; Recursive step ; Print the Hosoya triangle of height n . ; Driven Code
< ? php function Hosoya ( int $ n , int $ m ) { if ( ( $ n == 0 && $ m == 0 ) || ( $ n == 1 && $ m == 0 ) || ( $ n == 1 && $ m == 1 ) || ( $ n == 2 && $ m == 1 ) ) return 1 ; if ( $ n > $ m ) return Hosoya ( $ n - 1 , $ m ) + Hosoya ( $ n - 2 , $ m ) ; else if ( $ m == $ n ) return Hosoya ( $ n - 1 , $ m - 1 ) + Hosoya ( $ n - 2 , $ m - 2 ) ; else return 0 ; } function printHosoya ( $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) echo Hosoya ( $ i , $ j ) , " ▁ " ; echo " STRNEWLINE " ; } } $ n = 5 ; printHosoya ( $ n ) ; ? >
Eulerian Number | Return euleriannumber A ( n , m ) ; Driven Code
< ? php function eulerian ( $ n , $ m ) { if ( $ m >= $ n $ n == 0 ) return 0 ; if ( $ m == 0 ) return 1 ; return ( $ n - $ m ) * eulerian ( $ n - 1 , $ m - 1 ) + ( $ m + 1 ) * eulerian ( $ n - 1 , $ m ) ; } $ n = 3 ; $ m = 1 ; echo eulerian ( $ n , $ m ) ; ? >
Largest divisible pairs subset | function to find the longest Subsequence ; dp [ i ] is going to store size of largest divisible subset beginning with a [ i ] . ; Since last element is largest , d [ n - 1 ] is 1 ; Fill values for smaller elements . ; Find all multiples of a [ i ] and consider the multiple that has largest subset beginning with it . ; Return maximum value from dp [ ] ; Driver Code
< ? php function largestSubset ( $ a , $ n ) { $ dp = array ( ) ; $ dp [ $ n - 1 ] = 1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { $ mxm = 0 ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( $ a [ $ j ] % $ a [ $ i ] == 0 or $ a [ $ i ] % $ a [ $ j ] == 0 ) $ mxm = max ( $ mxm , $ dp [ $ j ] ) ; $ dp [ $ i ] = 1 + $ mxm ; } return max ( $ dp ) ; } $ a = array ( 1 , 3 , 6 , 13 , 17 , 18 ) ; $ n = count ( $ a ) ; echo largestSubset ( $ a , $ n ) ; ? >
Friends Pairing Problem | Returns count of ways n people can remain single or paired up . ; Filling dp [ ] in bottom - up manner using recursive formula explained above . ; Driver code
< ? php function countFriendsPairings ( $ n ) { $ dp [ $ n + 1 ] = 0 ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { if ( $ i <= 2 ) $ dp [ $ i ] = $ i ; else $ dp [ $ i ] = $ dp [ $ i - 1 ] + ( $ i - 1 ) * $ dp [ $ i - 2 ] ; } return $ dp [ $ n ] ; } $ n = 4 ; echo countFriendsPairings ( $ n ) ; ? >
Friends Pairing Problem | Returns count of ways n people can remain single or paired up . ; Driver code
< ? php function countFriendsPairings ( $ n ) { $ a = 1 ; $ b = 2 ; $ c = 0 ; if ( $ n <= 2 ) { return $ n ; } for ( $ i = 3 ; $ i <= $ n ; $ i ++ ) { $ c = $ b + ( $ i - 1 ) * $ a ; $ a = $ b ; $ b = $ c ; } return $ c ; } $ n = 4 ; print ( countFriendsPairings ( $ n ) ) ; ? >
LCS ( Longest Common Subsequence ) of three strings | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] and Z [ 0. . o - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] [ o + 1 ] in bottom up fashion . Note that L [ i ] [ j ] [ k ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] and Z [ 0. . ... k - 1 ] ; L [ m ] [ n ] [ o ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ] and Z [ 0. . o - 1 ] ; Driver code
< ? php function lcsOf3 ( $ X , $ Y , $ Z , $ m , $ n , $ o ) { $ L [ $ m + 1 ] [ $ n + 1 ] [ $ o + 1 ] = array ( array ( array ( ) ) ) ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) { for ( $ k = 0 ; $ k <= $ o ; $ k ++ ) { if ( $ i == 0 $ j == 0 $ k == 0 ) $ L [ $ i ] [ $ j ] [ $ k ] = 0 ; else if ( $ X [ $ i - 1 ] == $ Y [ $ j - 1 ] && $ X [ $ i - 1 ] == $ Z [ $ k - 1 ] ) $ L [ $ i ] [ $ j ] [ $ k ] = $ L [ $ i - 1 ] [ $ j - 1 ] [ $ k - 1 ] + 1 ; else $ L [ $ i ] [ $ j ] [ $ k ] = max ( max ( $ L [ $ i - 1 ] [ $ j ] [ $ k ] , $ L [ $ i ] [ $ j - 1 ] [ $ k ] ) , $ L [ $ i ] [ $ j ] [ $ k - 1 ] ) ; } } } return $ L [ $ m ] [ $ n ] [ $ o ] ; } $ X = " AGGT12" ; $ Y = "12TXAYB " ; $ Z = "12XBA " ; $ m = strlen ( $ X ) ; $ n = strlen ( $ Y ) ; $ o = strlen ( $ Z ) ; echo " Length ▁ of ▁ LCS ▁ is ▁ " . lcsOf3 ( $ X , $ Y , $ Z , $ m , $ n , $ o ) ; ? >
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 Code
< ? php function findLongestRepeatingSubSeq ( $ str ) { $ n = strlen ( $ str ) ; $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) $ dp [ $ i ] [ $ j ] = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) { if ( $ str [ $ i - 1 ] == $ str [ $ j - 1 ] && $ i != $ j ) $ dp [ $ i ] [ $ j ] = 1 + $ dp [ $ i - 1 ] [ $ j - 1 ] ; else $ dp [ $ i ] [ $ j ] = max ( $ dp [ $ i ] [ $ j - 1 ] , $ dp [ $ i - 1 ] [ $ j ] ) ; } } return $ dp [ $ n ] [ $ n ] ; } $ str = " aabb " ; echo " The ▁ length ▁ of ▁ the ▁ largest ▁ " . " subsequence ▁ that ▁ repeats ▁ itself ▁ is ▁ : ▁ " , findLongestRepeatingSubSeq ( $ str ) ; ? >
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
< ? php function numberOfPaths ( $ m , $ n ) { $ dp = array ( ) ; $ dp [ 0 ] = 1 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { for ( $ j = 1 ; $ j < $ n ; $ j ++ ) { $ dp [ $ j ] += $ dp [ $ j - 1 ] ; } } return $ dp [ $ n - 1 ] ; } echo numberOfPaths ( 3 , 3 ) ; ? >
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
< ? php function factorial ( $ N ) { if ( $ N == 0 or $ N == 1 ) return 1 ; return $ N * factorial ( $ N - 1 ) ; } function nthTerm ( $ N ) { return ( factorial ( $ N ) * ( $ N + 2 ) / 2 ) ; } $ N = 6 ; echo nthTerm ( $ N ) ; ? >
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
< ? php function isFrequencyEqual ( $ str , $ len ) { if ( $ len % 2 == 1 ) return false ; $ freq = array ( ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) $ freq [ $ i ] = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) $ freq [ ord ( $ str [ $ i ] ) - 97 ] ++ ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) if ( $ freq [ $ i ] == $ len / 2 ) return true ; return false ; } $ str = " geeksforgeeks " ; $ len = strlen ( $ str ) ; if ( isFrequencyEqual ( $ str , $ len ) ) echo " Yes " ; else echo " No " ; ? >
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 ; else Last pair ; Check for character which is not same in i - 1 index ; Driver Code
< ? php function countMinimalReplacements ( $ s ) { $ n = strlen ( $ s ) ; $ cnt = 0 ; $ str = "012" ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == $ s [ $ i - 1 ] ) { $ cnt += 1 ; if ( $ i != ( $ n - 1 ) ) { for ( $ it = 0 ; $ it < strlen ( $ str ) ; $ it ++ ) { if ( $ str [ $ it ] != $ s [ $ i + 1 ] && $ str [ $ it ] != $ s [ $ i - 1 ] ) { $ s [ $ i ] = $ str [ $ it ] ; break ; } } } { for ( $ it = 0 ; $ it < strlen ( $ str ) ; $ it ++ ) { if ( $ str [ $ it ] != $ s [ $ i - 1 ] ) { $ s [ $ i ] = $ str [ $ it ] ; break ; } } } } } return $ cnt ; } $ s = "201220211" ; echo countMinimalReplacements ( $ s ) ; ? >
Sub | Function that returns the index of next occurrence of the character c in string str starting from index start ; Starting from start ; If current character = c ; Not found ; Function to return the count of required sub - strings ; Stores running count of ' x ' starting from the end ; Next index of ' x ' starting from index 0 ; Next index of ' y ' starting from index 0 ; To store the count of required sub - strings ; If ' y ' appears before ' x ' it won 't contribute to a valid sub-string ; Find next occurrence of ' y ' ; If ' y ' appears after ' x ' every sub - string ending at an ' x ' appearing after this ' y ' and starting with the current ' x ' is a valid sub - string ; Find next occurrence of ' x ' ; Return the count ; Driver code
< ? php function nextIndex ( $ str , $ start , $ c ) { for ( $ i = $ start ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] == $ c ) return $ i ; } return -1 ; } function countSubStrings ( $ str ) { $ n = strlen ( $ str ) ; $ countX = array ( 0 , $ n , NULL ) ; $ count = 0 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( $ str [ $ i ] == ' x ' ) $ count ++ ; $ countX [ $ i ] = $ count ; } $ nextIndexX = nextIndex ( $ str , 0 , ' x ' ) ; $ nextIndexY = nextIndex ( $ str , 0 , ' y ' ) ; $ count = 0 ; while ( $ nextIndexX != -1 && $ nextIndexY != -1 ) { if ( $ nextIndexX > $ nextIndexY ) { $ nextIndexY = nextIndex ( $ str , $ nextIndexY + 1 , ' y ' ) ; continue ; } else { $ count += $ countX [ $ nextIndexY ] ; $ nextIndexX = nextIndex ( $ str , $ nextIndexX + 1 , ' x ' ) ; } } return $ count ; } $ s = " xyyxx " ; echo countSubStrings ( $ s ) ; ? >
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 ' ; return $mstr ; minimized string ; Utility function to print the minimized , replaced string ; Creating final string by replacing character ; index calculation ; echo " Final ▁ string : ▁ " . $finalStr ; final string ; Driver code
< ? php function minimize ( $ str ) { $ mstr = " ▁ " ; $ flagchar = array_fill ( 0 , 26 , 0 ) ; $ l = strlen ( $ str ) ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { $ ch = $ str [ $ i ] ; if ( $ flagchar [ ord ( $ ch ) - 97 ] == 0 ) { $ mstr . = $ ch ; $ flagchar [ ord ( $ ch ) - 97 ] = 1 ; } } } function replaceMinimizeUtil ( $ str ) { $ finalStr = " " ; $ l = strlen ( $ str ) ; for ( $ i = 0 ; $ i < strlen ( $ minimizedStr ) ; $ i ++ ) { $ ch = $ minimizedStr [ $ i ] ; $ index = ( ord ( $ ch ) * ord ( $ ch ) ) % $ l ; $ finalStr = $ finalStr . $ str [ $ index ] ; } } $ str = " geeks " ; replaceMinimizeUtil ( $ str ) ; ? >
Latin alphabet cipher | function for calculating the encryption ; Driver Code
< ? php function cipher ( $ str ) { if ( ! ctype_alpha ( $ str ) ) { printf ( " Enter ▁ only ▁ " + " alphabets ▁ and ▁ space STRNEWLINE " ) ; return ; } printf ( " Encrypted ▁ Code ▁ using ▁ " ) ; printf ( " Latin ▁ Alphabet STRNEWLINE " ) ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] >= ' A ' && $ str [ $ i ] <= ' Z ' ) echo ( ord ( $ str [ $ i ] ) - 65 + 1 ) . " ▁ " ; else if ( $ str [ $ i ] >= ' a ' && $ str [ $ i ] <= ' z ' ) echo ( ord ( $ str [ $ i ] ) - 97 + 1 ) . " ▁ " ; } echo " STRNEWLINE " ; } $ str = " geeksforgeeks " ; cipher ( $ str ) ; ? >
Round the given number to nearest multiple of 10 | function to round the number ; Smaller multiple ; Larger multiple ; Return of closest of two ; Driver Code
< ? php function roundFunation ( $ n ) { $ a = ( int ) ( $ n / 10 ) * 10 ; $ b = ( $ a + 10 ) ; return ( $ n - $ a > $ b - $ n ) ? $ b : $ a ; } $ n = 4722 ; echo roundFunation ( $ n ) , " STRNEWLINE " ; ? >
Number of subsequences as " ab " in a string repeated K times | PHP code to find number of subsequences of " ab " in the string S which is repeated K times . ; Count of ' a ' s ; Count of ' b ' s ; occurrence of " ab " s in string S ; Add following two : 1 ) K * ( Occurrences of " ab " in single string ) 2 ) a is from one string and b is from other . ; Driver code
< ? php function countOccurrences ( $ s , $ K ) { $ n = strlen ( $ s ) ; $ C = 0 ; $ c1 = 0 ; $ c2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == ' a ' ) $ c1 ++ ; if ( $ s [ $ i ] == ' b ' ) { $ c2 ++ ; $ C = $ C + $ c1 ; } } return $ C * $ K + ( $ K * ( $ K - 1 ) / 2 ) * $ c1 * $ c2 ; } $ S = " abcb " ; $ k = 2 ; echo countOccurrences ( $ S , $ k ) , " STRNEWLINE " ; ? >
String with k distinct characters and no same characters adjacent | Function to find a string of length n with k distinct characters . ; Initialize result with first k Latin letters ; Fill remaining n - k letters by repeating k letters again and again . ; Driver code
< ? php function findString ( $ n , $ k ) { $ res = " " ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) $ res = $ res . chr ( ord ( ' a ' ) + $ i ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n - $ k ; $ i ++ ) { $ res = $ res . chr ( ord ( ' a ' ) + $ count ) ; $ count ++ ; if ( $ count == $ k ) $ count = 0 ; } return $ res ; } $ n = 5 ; $ k = 2 ; echo findString ( $ n , $ k ) ; ? >
Program to count vowels in a string ( Iterative and Recursive ) | Function to check the Vowel ; to count total number of vowel from 0 to n ; string object ; Total numbers of Vowel
< ? php function isVowel ( $ ch ) { $ ch = strtoupper ( $ ch ) ; return ( $ ch == ' A ' $ ch == ' E ' $ ch == ' I ' $ ch == ' O ' $ ch == ' U ' ) ; } function countVovels ( $ str , $ n ) { if ( $ n == 1 ) return isVowel ( $ str [ $ n - 1 ] ) ; return countVovels ( $ str , $ n - 1 ) + isVowel ( $ str [ $ n - 1 ] ) ; } $ str = " abc ▁ de " ; echo countVovels ( $ str , strlen ( $ str ) ) . " " ; ? >
Generate all rotations of a given string | Print all the rotated string . ; Concatenate str with itself ; Print all substrings of size n . Note that size of temp is 2 n ; Driver code
< ? php function printRotatedString ( $ str ) { $ n = strlen ( $ str ) ; $ temp = $ str . $ str ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j != $ n ; $ j ++ ) print ( $ temp [ $ i + $ j ] ) ; print ( " STRNEWLINE " ) ; } } $ str = " geeks " ; printRotatedString ( $ str ) ; ? >
Check if frequency of all characters can become same by one removal | PHP program to get same frequency character string by removal of at most one char ; Utility method to get index of character ch in lower alphabet characters ; Returns true if all non - zero elements values are same ; get first non - zero element ; check equality of each element with variable same ; Returns true if we can make all character frequencies same ; fill frequency array ; if all frequencies are same , then return true ; Try decreasing frequency of all character by one and then check all equality of all non - zero frequencies ; Check character only if it occurs in str ; Driver code
< ? php $ M = 26 ; function getIdx ( $ ch ) { return ( $ ch - ' a ' ) ; } function allSame ( & $ freq , $ N ) { for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( $ freq [ $ i ] > 0 ) { $ same = $ freq [ $ i ] ; break ; } } for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) if ( $ freq [ $ j ] > 0 && $ freq [ $ j ] != $ same ) return false ; return true ; } function possibleSameCharFreqByOneRemoval ( $ str ) { global $ M ; $ l = strlen ( $ str ) ; $ freq = array_fill ( 0 , $ M , NULL ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) $ freq [ getIdx ( $ str [ $ i ] ) ] ++ ; if ( allSame ( $ freq , $ M ) ) return true ; for ( $ c = ' a ' ; $ c <= ' z ' ; $ c ++ ) { $ i = getIdx ( $ c ) ; if ( $ freq [ $ i ] > 0 ) { $ freq [ $ i ] -- ; if ( allSame ( $ freq , $ M ) ) return true ; $ freq [ $ i ] ++ ; } } return false ; } $ str = " xyyzz " ; if ( possibleSameCharFreqByOneRemoval ( $ str ) ) echo " Yes " ; else echo " No " ; ? >
Check if a large number is divisible by 11 or not | Function to find that number divisible by 11 or not ; Compute sum of even and odd digit sums ; When i is even , position of digit is odd ; Check its difference is divisible by 11 or not ; Driver code
< ? php function check ( $ str ) { $ n = strlen ( $ str ) ; $ oddDigSum = 0 ; $ evenDigSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) $ oddDigSum += ( $ str [ $ i ] - '0' ) ; else $ evenDigSum += ( $ str [ $ i ] - '0' ) ; } return ( ( $ oddDigSum - $ evenDigSum ) % 11 == 0 ) ; } $ str = "76945" ; $ x = check ( $ str ) ? " Yes " : " No ▁ " ; echo ( $ x ) ; ? >
Hamming Distance between two strings | function to calculate Hamming distance ; Driver Code ; function call
< ? php function hammingDist ( $ str1 , $ str2 ) { $ i = 0 ; $ count = 0 ; while ( isset ( $ str1 [ $ i ] ) != ' ' ) { if ( $ str1 [ $ i ] != $ str2 [ $ i ] ) $ count ++ ; $ i ++ ; } return $ count ; } $ str1 = " geekspractice " ; $ str2 = " nerdspractise " ; echo hammingDist ( $ str1 , $ str2 ) ; ? >
Check if two strings are k | PHP program to check if two strings are k anagram or not . ; Function to check that string is k - anagram or not ; If both strings are not of equal length then return false ; Store the occurrence of all characters in a hash_array ; Count number of characters that are different in both strings ; Return true if count is less than or equal to k ; Driver Code
< ? php $ MAX_CHAR = 26 ; function arekAnagrams ( $ str1 , $ str2 , $ k ) { global $ MAX_CHAR ; $ n = strlen ( $ str1 ) ; if ( strlen ( $ str2 ) != $ n ) return false ; $ count1 = ( 0 ) ; $ count2 = ( 0 ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) if ( $ count1 [ $ i ] > $ count2 [ $ i ] ) $ count = $ count + abs ( $ count1 [ $ i ] - $ count2 [ $ i ] ) ; return ( $ count <= $ k ) ; } $ str1 = " anagram " ; $ str2 = " grammar " ; $ k = 2 ; if ( arekAnagrams ( $ str1 , $ str2 , $ k ) ) echo " Yes " ; else echo " No " ; ? >
Given a binary string , count number of substrings that start and end with 1. | A simple PHP program to count number of substrings starting and ending with 1 ; Pick a starting point ; Return count of possible pairs among m 1 's ; Driver Code
< ? php function countSubStr ( $ str ) { for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] == '1' ) { $ m ++ ; } } return $ m * ( $ m - 1 ) / 2 ; } $ str = "00100101" ; echo countSubStr ( $ str ) ; ? >
Given a number as a string , find the number of contiguous subsequences which recursively add up to 9 | PHP program to count substrings with recursive sum equal to 9 ; To store result ; Consider every character as beginning of substring ; sum of digits in current substring ; One by one choose every character as an ending character ; Add current digit to sum , if sum becomes multiple of 5 then increment count . Let us do modular arithmetic to avoid overflow for big strings ; Driver Code
< ? php function count9s ( $ number ) { $ count = 0 ; $ n = strlen ( $ number ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum = $ number [ $ i ] - '0' ; if ( $ number [ $ i ] == '9' ) $ count ++ ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ sum = ( $ sum + $ number [ $ j ] - '0' ) % 9 ; if ( $ sum == 0 ) $ count ++ ; } } return $ count ; } echo count9s ( " 4189 " ) , " STRNEWLINE " ; echo count9s ( " 1809 " ) ; ? >
Collect all coins in minimum number of steps | recursive method to collect coins from height array l to r , with height h already collected ; if l is more than r , no steps needed ; loop over heights to get minimum height index ; choose minimum from , 1 ) collecting coins using all vertical lines ( total r - l ) 2 ) collecting coins using lower horizontal lines and recursively on left and right segments ; method returns minimum number of step to collect coin from stack , with height in height [ ] array ; Driver Code
< ? php function minStepsRecur ( $ height , $ l , $ r , $ h ) { if ( $ l >= $ r ) return 0 ; $ m = $ l ; for ( $ i = $ l ; $ i < $ r ; $ i ++ ) if ( $ height [ $ i ] < $ height [ $ m ] ) $ m = $ i ; return min ( $ r - $ l , minStepsRecur ( $ height , $ l , $ m , $ height [ $ m ] ) + minStepsRecur ( $ height , $ m + 1 , $ r , $ height [ $ m ] ) + $ height [ $ m ] - $ h ) ; } function minSteps ( $ height , $ N ) { return minStepsRecur ( $ height , 0 , $ N , 0 ) ; } $ height = array ( 2 , 1 , 2 , 5 , 1 ) ; $ N = sizeof ( $ height ) ; echo minSteps ( $ height , $ N ) ; ? >
Biggest Reuleaux Triangle within A Square | Function to find the Area of the Reuleaux triangle ; Side cannot be negative ; Area of the Reuleaux triangle ; Driver code
< ? php function ReuleauxArea ( $ a ) { if ( $ a < 0 ) return -1 ; $ A = 0.70477 * pow ( $ a , 2 ) ; return $ A ; } $ a = 6 ; echo ReuleauxArea ( $ a ) . " STRNEWLINE " ; ? >
Largest hexagon that can be inscribed within a square | Function to return the side of the hexagon ; Side cannot be negative ; Side of the hexagon ; Driver code
< ? php function hexagonside ( $ a ) { if ( $ a < 0 ) return -1 ; $ x = 0.5176 * $ a ; return $ x ; } $ a = 6 ; echo hexagonside ( $ a ) ; ? >
Largest hexagon that can be inscribed within an equilateral triangle | Function to find the side of the hexagon ; Side cannot be negative ; Side of the hexagon ; Driver code
< ? php function hexagonside ( $ a ) { if ( $ a < 0 ) return -1 ; $ x = $ a / 3 ; return $ x ; } $ a = 6 ; echo hexagonside ( $ a ) ; ? >
Find middle point segment from given segment lengths | Function that returns the segment for the middle point ; the middle point ; stores the segment index ; increment sum by length of the segment ; if the middle is in between two segments ; if sum is greater than middle point ; Driver code
< ? php function findSegment ( $ n , $ m , $ segment_length ) { $ meet_point = ( 1.0 * $ n ) / 2.0 ; $ sum = 0 ; $ segment_number = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ sum += $ segment_length [ $ i ] ; if ( ( double ) $ sum == $ meet_point ) { $ segment_number = -1 ; break ; } if ( $ sum > $ meet_point ) { $ segment_number = $ i + 1 ; break ; } } return $ segment_number ; } $ n = 13 ; $ m = 3 ; $ segment_length = array ( 3 , 2 , 8 ) ; $ ans = findSegment ( $ n , $ m , $ segment_length ) ; echo ( $ ans ) ; ? >
Maximum points of intersection n lines | nC2 = ( n ) * ( n - 1 ) / 2 ; ; n is number of line
< ? php function countMaxIntersect ( $ n ) { return ( $ n ) * ( $ n - 1 ) / 2 ; } $ n = 8 ; echo countMaxIntersect ( $ n ) . " STRNEWLINE " ; ? >