codeparrot/xlcost-text-to-code huggingface dataset:
Find time taken for signal to reach all positions in a string | Returns time needed for signal to traverse through complete string . ; for the calculation of last index ; for strings like oxoooo , xoxxoooo . . ; if coun is greater than max_length ; if ' x ' is present at the right side of max_length ; if ' x ' is present at left side of max_length ; We use ceiling function to handle odd number ' o ' s ; Driver code
< ? php function maxLength ( $ s , $ n ) { $ right = 0 ; $ left = 0 ; $ coun = 0 ; $ max_length = PHP_INT_MIN ; $ s = $ s . '1' ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { if ( $ s [ $ i ] == ' o ' ) $ coun ++ ; else { if ( $ coun > $ max_length ) { $ right = 0 ; $ left = 0 ; if ( $ s [ $ i ] == ' x ' ) $ right = 1 ; if ( ( ( $ i - $ coun ) > 0 ) && ( $ s [ $ i - $ coun - 1 ] == ' x ' ) ) $ left = 1 ; $ coun = ( int ) ceil ( ( double ) $ coun / ( $ right + $ left ) ) ; $ max_length = max ( $ max_length , $ coun ) ; } $ coun = 0 ; } } return $ max_length ; } $ s = " oooxoooooooooxooo " ; $ n = strlen ( $ s ) ; echo ( maxLength ( $ s , $ n ) ) ;
Check whether the vowels in a string are in alphabetical order or not | Function that checks whether the vowel characters in a string are in alphabetical order or not ; ASCII Value 64 is less than all the alphabets so using it as a default value ; check if the vowels in the string are sorted or not ; if the vowel is smaller than the previous vowel ; store the vowel ; Driver code ; check whether the vowel characters in a string are in alphabetical order or not
< ? php function areVowelsInOrder ( $ s ) { $ n = strlen ( $ s ) ; $ c = chr ( 64 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == ' a ' $ s [ $ i ] == ' e ' $ s [ $ i ] == ' $ i ' $ s [ $ i ] == ' o ' $ s [ $ i ] == ' u ' ) { if ( $ s [ $ i ] < $ c ) return false ; else { $ c = $ s [ $ i ] ; } } } return true ; } $ s = " aabbbddeecc " ; if ( areVowelsInOrder ( $ s ) ) echo " Yes " ; else echo " No " ; ? >
Program to find remainder when large number is divided by r | Function to Return Remainder ; len is variable to store the length of Number string . ; loop that find Remainder ; Return the remainder ; Get the large number as string ; Get the divisor R ; Find and print the remainder
< ? php function Remainder ( $ str , $ R ) { $ len = strlen ( $ str ) ; $ Num = 0 ; $ Rem = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ Num = $ Rem * 10 + ( $ str [ $ i ] - '0' ) ; $ Rem = $ Num % $ R ; } return $ Rem ; } $ str = "13589234356546756" ; $ R = 13 ; echo Remainder ( $ str , $ R ) ;
Count the number of carry operations required to add two numbers | Function to count the number of carry operations ; Initialize the value of carry to 0 ; Counts the number of carry operations ; Initialize len_a and len_b with the sizes of strings ; Assigning the ascii value of the character ; Add both numbers / digits ; If sum > 0 , increment count and set carry to 1 ; Else , set carry to 0 ; Driver code
< ? php function count_carry ( $ a , $ b ) { $ carry = 0 ; $ count = 0 ; $ len_a = strlen ( $ a ) ; $ len_b = strlen ( $ b ) ; while ( $ len_a != 0 $ len_b != 0 ) { $ x = 0 ; $ y = 0 ; if ( $ len_a > 0 ) { $ x = $ a [ $ len_a - 1 ] - '0' ; $ len_a -- ; } if ( $ len_b > 0 ) { $ y = $ b [ $ len_b - 1 ] - '0' ; $ len_b -- ; } $ sum = $ x + $ y + $ carry ; if ( $ sum >= 10 ) { $ carry = 1 ; $ count ++ ; } else $ carry = 0 ; } return $ count ; } $ a = "9555" ; $ b = "555" ; $ count = count_carry ( $ a , $ b ) ; if ( $ count == 0 ) echo "0 STRNEWLINE " ; else if ( $ count == 1 ) echo "1 STRNEWLINE " ; else echo $ count , " STRNEWLINE " ; ? >
Check if a number is in given base or not | PHP program to check if given number is in given base or not . ; Allowed bases are till 16 ( Hexadecimal ) ; If base is below or equal to 10 , then all digits should be from 0 to 9. ; If base is below or equal to 16 , then all digits should be from 0 to 9 or from ' A ' ; Driver code
< ? php function isInGivenBase ( $ str , $ base ) { if ( $ base > 16 ) return false ; else if ( $ base <= 10 ) { for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( ! ( $ str [ $ i ] >= '0' and $ str [ $ i ] < ( '0' + $ base ) ) ) return false ; } else { for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( ! ( ( $ str [ $ i ] >= '0' && $ str [ $ i ] < ( '0' + $ base ) ) || ( $ str [ $ i ] >= ' A ' && $ str [ $ i ] < ( ' A ' + $ base - 10 ) ) ) ) return false ; } return true ; } $ str = " AF87" ; if ( isInGivenBase ( $ str , 16 ) ) echo " Yes " ; else echo " No " ; ? >
Find indices of all occurrence of one string in other | PHP program to find indices of all occurrences of one string in other . ; Driver Code
< ? php function printIndex ( $ str , $ s ) { $ flag = false ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( substr ( $ str , $ i , strlen ( $ s ) ) == $ s ) { echo $ i . " " ; $ flag = true ; } } if ( $ flag == false ) echo " NONE " ; } $ str1 = " GeeksforGeeks " ; $ str2 = " Geeks " ; printIndex ( $ str1 , $ str2 ) ; ? >
Sorting array with conditional swapping | Function to check if it is possible to sort the array ; Calculating max_element at each iteration . ; if we can not swap the i - th element . ; if it is impossible to swap the max_element then we can not sort the array . ; Otherwise , we can sort the array . ; Driver Code
< ? php function possibleToSort ( $ arr , $ n , $ str ) { $ max_element = -1 ; for ( $ i = 0 ; $ i < sizeof ( $ str ) ; $ i ++ ) { $ max_element = max ( $ max_element , $ arr [ $ i ] ) ; if ( $ str [ $ i ] == '0' ) { if ( $ max_element > $ i + 1 ) return " No " ; } } return " Yes " ; } $ arr = array ( 1 , 2 , 5 , 3 , 4 , 6 ) ; $ n = sizeof ( $ arr ) ; $ str = "01110" ; echo possibleToSort ( $ arr , $ n , $ str ) ; ? >
Prime String | Function that checks if sum is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver code
< ? php function isPrimeString ( $ str ) { $ len = strlen ( $ str ) ; $ n = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) $ n += ( int ) $ str [ $ i ] ; if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } $ str = " geekRam " ; if ( isPrimeString ( $ str ) ) echo " Yes " , " STRNEWLINE " ; else echo " No " , " STRNEWLINE " ; ? >
Number of palindromic permutations | Set 1 | PHP program to find number of palindromic permutations of a given string ; Returns factorial of n ; Returns count of palindromic permutations of str . ; Count frequencies of all characters ; Since half of the characters decide count of palindromic permutations , we take ( n / 2 ) ! ; To make sure that there is at most one odd occurring char ; Traverse through all counts ; To make sure that the string can permute to form a palindrome ; If there are more than one odd occurring chars ; Divide all permutations with repeated characters ; Driver code
< ? php $ MAX = 256 ; function fact ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } function countPalinPermutations ( & $ str ) { global $ MAX ; $ n = strlen ( $ str ) ; $ freq = ( 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res = fact ( $ n / 2 ) ; $ oddFreq = false ; for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) { $ half = $ freq [ $ i ] / 2 ; if ( $ freq [ $ i ] % 2 != 0 ) { if ( $ oddFreq == true ) return 0 ; $ oddFreq = true ; } $ res = $ res / fact ( $ half ) ; } return $ res ; } $ str = " gffg " ; echo countPalinPermutations ( $ str ) ; ? >
Convert the string into palindrome string by changing only one character | Function to check if it is possible to convert the string into palindrome ; Counting number of characters that should be changed . ; If count of changes is less than or equal to 1 ; Driver Code
< ? php function checkPalindrome ( $ str ) { $ n = strlen ( $ str ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n / 2 ; ++ $ i ) if ( $ str [ $ i ] != $ str [ $ n - $ i - 1 ] ) ++ $ count ; return ( $ count <= 1 ) ; } { $ str = " abccaa " ; if ( checkPalindrome ( $ str ) ) echo " Yes " ; else echo " No " ; return 0 ; } ? >
Counting even decimal value substrings in a binary string | function return count of even decimal value substring ; store the count of even decimal value substring ; substring started with '0' ; increment result by ( n - i ) because all substring which are generated by this character produce even decimal value . ; Driver Code
< ? php function evenDecimalValue ( $ str , $ n ) { $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == '0' ) { $ result += ( $ n - $ i ) ; } } return $ result ; } $ str = "10010" ; $ n = 5 ; echo evenDecimalValue ( $ str , $ n ) ; return 0 ; ? >
Number of substrings with odd decimal value in a binary string | function to count number of substrings with odd decimal representation ; auxiliary array to store count of 1 's  before ith index ; store count of 1 's  before i-th index ; variable to store answer ; traverse the string reversely to calculate number of odd substrings before i - th index ; Driver code
< ? php function countSubstr ( $ s ) { $ n = strlen ( $ s ) ; $ auxArr = array ( ) ; if ( $ s [ 0 ] == '1' ) $ auxArr [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == '1' ) $ auxArr [ $ i ] = $ auxArr [ $ i - 1 ] + 1 ; else $ auxArr [ $ i ] = $ auxArr [ $ i - 1 ] ; } $ count = 0 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) if ( $ s [ $ i ] == '1' ) $ count += $ auxArr [ $ i ] ; return $ count ; } $ s = "1101" ; echo countSubstr ( $ s ) ; ? >
Check whether second string can be formed from characters of first string | PHP program to check whether second string can be formed from first string ; Create a count array and count frequencies characters in str1 . ; Now traverse through str2 to check if every character has enough counts ; Driver Code
< ? php $ MAX = 256 ; function canMakeStr2 ( $ str1 , $ str2 ) { $ count = ( 0 ) ; for ( $ i = 0 ; $ i < strlen ( $ str1 ) ; $ i ++ ) for ( $ i = 0 ; $ i < strlen ( $ str2 ) ; $ i ++ ) { if ( $ count [ $ str2 [ $ i ] ] == 0 ) return -1 ; } return true ; } $ str1 = " geekforgeeks " ; $ str2 = " for " ; if ( canMakeStr2 ( $ str1 , $ str2 ) ) echo " Yes " ; else echo " No " ; ? >
Position of robot after given movements | function to find final position of robot after the complete movement ; traverse the instruction string ' move ' ; for each movement increment its respective counter ; required final position of robot ; Driver Code
< ? php function finalPosition ( $ move ) { $ l = strlen ( $ move ) ; $ countUp = 0 ; $ countDown = 0 ; $ countLeft = 0 ; $ countRight = 0 ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { if ( $ move [ $ i ] == ' U ' ) $ countUp ++ ; else if ( $ move [ $ i ] == ' D ' ) $ countDown ++ ; else if ( $ move [ $ i ] == ' L ' ) $ countLeft ++ ; else if ( $ move [ $ i ] == ' R ' ) $ countRight ++ ; } echo " Final ▁ Position : ▁ ( " . ( $ countRight - $ countLeft ) . " , " ▁ , ▁ ( $ countUp ▁ - ▁ $ countDown ) ▁ . ▁ " ) " ▁ . " " } $ move = " UDDLLRUUUDUURUDDUULLDRRRR " ; finalPosition ( $ move ) ; ? >
Length of longest balanced parentheses prefix | Return the length of longest balanced parentheses prefix . ; Traversing the string . ; If open bracket add 1 to sum . ; If closed bracket subtract 1 from sum ; if first bracket is closing bracket then this condition would help ; If sum is 0 , store the index value . ; Driver Code
< ? php function maxbalancedprefix ( $ str , $ n ) { $ sum = 0 ; $ maxi = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == ' ( ' ) $ sum += 1 ; else $ sum -= 1 ; if ( $ sum < 0 ) break ; if ( $ sum == 0 ) $ maxi = $ i + 1 ; } return $ maxi ; } $ str = array ( ' ( ' , ' ( ' , ' ( ' , ' ) ' , ' ( ' , ' ) ' , ' ) ' , ' ( ' , ' ) ' , ' ) ' , ' ( ' , ' ( ' ) ; $ n = count ( $ str ) ; echo maxbalancedprefix ( $ str , $ n ) ; ? >
Minimum cost to convert string into palindrome | Function to return cost ; length of string ; Iterate from both sides of string . If not equal , a cost will be there ; Driver code
< ? php function cost ( $ str ) { $ len = strlen ( $ str ) ; $ res = 0 ; for ( $ i = 0 , $ j = $ len - 1 ; $ i < $ j ; $ i ++ , $ j -- ) if ( $ str [ $ i ] != $ str [ $ j ] ) $ res += ( min ( ord ( $ str [ $ i ] ) , ord ( $ str [ $ j ] ) ) - ord ( ' a ' ) + 1 ) ; return $ res ; } $ str = " abcdef " ; echo cost ( $ str ) ; ? >
Possibility of a word from a given set of characters | PHP program to check if a query string is present is given set . ; Count occurrences of all characters in s . ; Check if number of occurrences of every character in q is less than or equal to that in s . ; Driver Code
< ? php function isPresent ( $ s , $ q ) { $ freq = array ( 256 ) ; for ( $ i = 0 ; $ i < 256 ; $ i ++ ) $ freq [ $ i ] = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) $ freq [ ord ( $ s [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < strlen ( $ q ) ; $ i ++ ) { $ freq [ ord ( $ q [ $ i ] ) - ord ( ' a ' ) ] -- ; if ( $ freq [ ord ( $ q [ $ i ] ) - ord ( ' a ' ) ] < 0 ) return false ; } return true ; } $ s = " abctd " ; $ q = " cat " ; if ( isPresent ( $ s , $ q ) ) echo " Yes " ; else echo " No " ; ? >
Minimum reduce operations to convert a given string into a palindrome | Returns count of minimum character reduce operations to make palindrome . ; Compare every character of first half with the corresponding character of second half and add difference to result . ; Driver code
< ? php function countReduce ( $ str ) { $ n = strlen ( $ str ) ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) $ res += abs ( ord ( $ str [ $ i ] ) - ord ( $ str [ ( $ n - $ i - 1 ) ] ) ) ; return $ res ; } $ str = " abcd " ; echo countReduce ( $ str ) ; ? >
Minimal operations to make a number magical | function to calculate the minimal changes ; maximum digits that can be changed ; nested loops to generate all 6 digit numbers ; counter to count the number of change required ; if first digit is equal ; if 2 nd digit is equal ; if 3 rd digit is equal ; if 4 th digit is equal ; if 5 th digit is equal ; if 6 th digit is equal ; checks if less then the previous calculate changes ; returns the answer ; number stored in string ; prints the minimum operations
< ? php function calculate ( $ s ) { $ ans = 6 ; for ( $ i = 0 ; $ i < 10 ; ++ $ i ) { for ( $ j = 0 ; $ j < 10 ; ++ $ j ) { for ( $ k = 0 ; $ k < 10 ; ++ $ k ) { for ( $ l = 0 ; $ l < 10 ; ++ $ l ) { for ( $ m = 0 ; $ m < 10 ; ++ $ m ) { for ( $ n = 0 ; $ n < 10 ; ++ $ n ) { if ( $ i + $ j + $ k == $ l + $ m + $ n ) { $ c = 0 ; if ( $ i != $ s [ 0 ] - '0' ) $ c ++ ; if ( $ j != $ s [ 1 ] - '0' ) $ c ++ ; if ( $ k != $ s [ 2 ] - '0' ) $ c ++ ; if ( $ l != $ s [ 3 ] - '0' ) $ c ++ ; if ( $ m != $ s [ 4 ] - '0' ) $ c ++ ; if ( $ n != $ s [ 5 ] - '0' ) $ c ++ ; if ( $ c < $ ans ) $ ans = $ c ; } } } } } } } return $ ans ; } $ s = "123456" ; echo calculate ( $ s ) ; ? >
Check if a two character string can be made using given words | Function to check if str can be made using given words ; If str itself is present ; Match first character of str with second of word and vice versa ; If both characters found . ; Driver Code
< ? php function makeAndCheckString ( $ words , $ str ) { $ n = sizeof ( $ words ) ; $ first = false ; $ second = false ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ words [ $ i ] == $ str ) return true ; if ( $ str [ 0 ] == $ words [ $ i ] [ 1 ] ) $ first = true ; if ( $ str [ 1 ] == $ words [ $ i ] [ 0 ] ) $ second = true ; if ( $ first && $ second ) return true ; } return false ; } $ str = " ya " ; $ words = array ( " ah " , " oy " , " to " , " ha " ) ; if ( makeAndCheckString ( $ words , $ str ) ) echo " Yes " ; else echo " No " ; ? >
Sentence Palindrome ( Palindrome after removing spaces , dots , . . etc ) | To check sentence is palindrome or not ; Lowercase string ; Compares character until they are equal ; If there is another symbol in left of sentence ; If there is another symbol in right of sentence ; If characters are equal ; If characters are not equal then sentence is not palindrome ; Returns true if sentence is palindrome ; Driver program to test sentencePalindrome ( )
< ? php function sentencePalindrome ( $ str ) { $ l = 0 ; $ h = strlen ( $ str ) - 1 ; for ( $ i = 0 ; $ i < $ h ; $ i ++ ) $ str [ $ i ] = strtolower ( $ str [ $ i ] ) ; while ( $ l <= $ h ) { if ( ! ( $ str [ $ l ] >= ' a ' && $ str [ $ l ] <= ' z ' ) ) $ l ++ ; else if ( ! ( $ str [ $ h ] >= ' a ' && $ str [ $ h ] <= ' z ' ) ) $ h -- ; else if ( $ str [ $ l ] == $ str [ $ h ] ) { $ l ++ ; $ h -- ; } else return false ; } return true ; } $ str = " Too ▁ hot ▁ to ▁ hoot . " ; if ( sentencePalindrome ( $ str ) ) echo " Sentence ▁ is ▁ palindrome . " ; else echo " Sentence ▁ is ▁ not ▁ palindrome . " ; return 0 ; ? >
Ways to remove one element from a binary string so that XOR becomes zero | Return number of ways in which XOR become ZERO by remove 1 element ; Counting number of 0 and 1 ; If count of ones is even then return count of zero else count of one ; Driver Code
< ? php function xorZero ( $ str ) { $ one_count = 0 ; $ zero_count = 0 ; $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ str [ $ i ] == '1' ) $ one_count ++ ; else $ zero_count ++ ; if ( $ one_count % 2 == 0 ) return $ zero_count ; return $ one_count ; } $ str = "11111" ; echo xorZero ( $ str ) , " STRNEWLINE " ; ? >
Check if both halves of the string have same set of characters | PHP program to check if it is possible to split string or not ; function to check if we can split string or not ; Counter array initialized with 0 ; Length of the string ; traverse till the middle element is reached ; First half ; Second half ; Checking if values are different set flag to 1 ; String to be checked
< ? php $ MAX_CHAR = 26 ; function checkCorrectOrNot ( $ s ) { global $ MAX_CHAR ; $ count = array_fill ( 0 , $ MAX_CHAR , NULL ) ; $ n = strlen ( $ s ) ; if ( $ n == 1 ) return true ; for ( $ i = 0 , $ j = $ n - 1 ; $ i < $ j ; $ i ++ , $ j -- ) { $ count [ $ s [ $ i ] - ' a ' ] ++ ; $ count [ $ s [ $ j ] - ' a ' ] -- ; } for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) if ( $ count [ $ i ] != 0 ) return false ; return true ; } $ s = " abab " ; if ( checkCorrectOrNot ( $ s ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
Determine if a string has all Unique Characters | PHP program to illustrate string with unique characters using brute force technique ; Assuming string can have characters a - z , this has 32 bits set to 0 ; if that bit is already set in checker , return false ; otherwise update and continue by setting that bit in the checker ; no duplicates encountered , return true ; Driver Code
< ? php function uniqueCharacters ( $ str ) { $ checker = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { $ bitAtIndex = $ str [ $ i ] - ' a ' ; if ( ( $ checker & ( 1 << $ bitAtIndex ) ) > 0 ) { return false ; } $ checker = $ checker | ( 1 << $ bitAtIndex ) ; } return true ; } $ str = " geeksforgeeks " ; if ( uniqueCharacters ( $ str ) ) { echo " The ▁ String ▁ " , $ str , " ▁ has ▁ all ▁ unique ▁ characters STRNEWLINE " ; } else { echo " The ▁ String ▁ " , $ str , " ▁ has ▁ duplicate ▁ characters STRNEWLINE " ; } ? >
Check whether K | PHP program to check if k - th bit of a given number is set or not ; Driver code
< ? php function isKthBitSet ( $ n , $ k ) { if ( $ n & ( 1 << ( $ k - 1 ) ) ) echo " SET " ; else echo " NOT ▁ SET " ; } $ n = 5 ; $ k = 1 ; isKthBitSet ( $ n , $ k ) ; ? >
Recursive function to check if a string is palindrome | A recursive function that check a str [ s . . e ] is palindrome or not . ; If there is only one character ; If first and last characters do not match ; If there are more than two characters , check if middle substring is also palindrome or not . ; An empty string is considered as palindrome ; Driver Code
< ? php function isPalRec ( $ str , $ s , $ e ) { if ( $ s == $ e ) return true ; if ( $ str [ $ s ] != $ str [ $ e ] ) return false ; if ( $ s < $ e + 1 ) return isPalRec ( $ str , $ s + 1 , $ e - 1 ) ; return true ; } function isPalindrome ( $ str ) { $ n = strlen ( $ str ) ; if ( $ n == 0 ) return true ; return isPalRec ( $ str , 0 , $ n - 1 ) ; } { $ str = " geeg " ; if ( isPalindrome ( $ str ) ) echo ( " Yes " ) ; else echo ( " No " ) ; return 0 ; } ? >
Count substrings with same first and last characters | assuming lower case only ; Calculating frequency of each character in the string . ; Computing result using counts ; Driver Code
< ? php $ MAX_CHAR = 26 ; function countSubstringWithEqualEnds ( $ s ) { global $ MAX_CHAR ; $ result = 0 ; $ n = strlen ( $ s ) ; $ count = array_fill ( 0 , $ MAX_CHAR , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ count [ ord ( $ s [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) $ result += ( $ count [ $ i ] * ( $ count [ $ i ] + 1 ) / 2 ) ; return $ result ; } $ s = " abcab " ; echo countSubstringWithEqualEnds ( $ s ) ; ? >
Maximum consecutive repeating character in string | Returns the maximum repeating character in a given string ; Traverse string except last character ; If current character matches with next ; If doesn 't match, update result  (if required) and reset count ; Driver code
< ? php function maxRepeating ( $ str ) { $ n = strlen ( $ str ) ; $ count = 0 ; $ res = $ str [ 0 ] ; $ cur_count = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i < $ n - 1 && $ str [ $ i ] == $ str [ $ i + 1 ] ) $ cur_count ++ ; else { if ( $ cur_count > $ count ) { $ count = $ cur_count ; $ res = $ str [ $ i ] ; } $ cur_count = 1 ; } } return $ res ; } $ str = " aaaabbaaccde " ; echo maxRepeating ( $ str ) ; ? >
Queries for characters in a repeated string | Print whether index i and j have same element or not . ; Finding relative position of index i , j . ; Checking is element are same at index i , j . ; Driver Code
< ? php function query ( $ s , $ i , $ j ) { $ n = strlen ( $ s ) ; $ i %= $ n ; $ j %= $ n ; if ( ( $ s [ $ i ] == $ s [ $ j ] ) ) echo " Yes STRNEWLINE " ; else echo " No " ; } $ X = " geeksforgeeks " ; query ( $ X , 0 , 8 ) ; query ( $ X , 8 , 13 ) ; query ( $ X , 6 , 15 ) ; ? >
Count of character pairs at same distance as in English alphabets | Function to count pairs ; Increment count if characters are at same distance ; Driver code
< ? php function countPairs ( $ str ) { $ result = 0 ; $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( abs ( ord ( $ str [ $ i ] ) - ord ( $ str [ $ j ] ) ) == abs ( $ i - $ j ) ) $ result ++ ; return $ result ; } $ str = " geeksforgeeks " ; echo countPairs ( $ str ) ; ? >
Longest common subsequence with permutations allowed | Function to calculate longest string str1 -- > first string str2 -- > second string count1 [ ] -- > hash array to calculate frequency of characters in str1 count [ 2 ] -- > hash array to calculate frequency of characters in str2 result -- > resultant longest string whose permutations are sub - sequence of given two strings ; calculate frequency of characters ; Now traverse hash array ; append character ( ' a ' + i ) in resultant string ' result ' by min ( count1 [ $i ] , count2 [ $i ] ) times ; Driver Code
< ? php function longestString ( $ str1 , $ str2 ) { $ count1 = array_fill ( 0 , 26 , NULL ) ; $ count2 = array_fill ( 0 , 26 , NULL ) ; for ( $ i = 0 ; $ i < strlen ( $ str1 ) ; $ i ++ ) $ count1 [ ord ( $ str1 [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < strlen ( $ str2 ) ; $ i ++ ) $ count2 [ ord ( $ str2 [ $ i ] ) - ord ( ' a ' ) ] ++ ; $ result = " " ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) for ( $ j = 1 ; $ j <= min ( $ count1 [ $ i ] , $ count2 [ $ i ] ) ; $ j ++ ) $ result = $ result . chr ( ord ( ' a ' ) + $ i ) ; echo $ result ; } $ str1 = " geeks " ; $ str2 = " cake " ; longestString ( $ str1 , $ str2 ) ; ? >