codeparrot/xlcost-text-to-code huggingface dataset:
Number of positions where a letter can be inserted such that a string becomes palindrome | Function to check if the string is palindrome ; to know the length of string ; if the given string is a palindrome ( Case - I ) ; Sub - case - III ) ; if the length is even ; sub - case - I ; sub - case - II ; insertion point ; Case - I ; Case - II ; Driver code
< ? php function isPalindrome ( $ s , $ i , $ j ) { $ p = $ j ; for ( $ k = $ i ; $ k <= $ p ; $ k ++ ) { if ( $ s [ $ k ] != $ s [ $ p ] ) return false ; $ p -- ; } return true ; } function countWays ( $ s ) { $ n = strlen ( $ s ) ; $ count = 0 ; if ( isPalindrome ( $ s , 0 , $ n - 1 ) ) { for ( $ i = $ n / 2 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == $ s [ $ i + 1 ] ) $ count ++ ; else break ; } if ( $ n % 2 == 0 ) { $ count ++ ; $ count = 2 * $ count + 1 ; } else $ count = 2 * $ count + 2 ; } else { for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) { if ( $ s [ $ i ] != $ s [ $ n - 1 - $ i ] ) { $ j = $ n - 1 - $ i ; if ( isPalindrome ( $ s , $ i , $ n - 2 - $ i ) ) { for ( $ k = $ i - 1 ; $ k >= 0 ; $ k -- ) { if ( $ s [ $ k ] != $ s [ $ j ] ) break ; $ count ++ ; } $ count ++ ; } if ( isPalindrome ( $ s , $ i + 1 , $ n - 1 - $ i ) ) { for ( $ k = $ n - $ i ; $ k < $ n ; $ k ++ ) { if ( $ s [ $ k ] != $ s [ $ i ] ) break ; $ count ++ ; } $ count ++ ; } break ; } } } return $ count ; } $ s = " abca " ; echo countWays ( $ s ) ; ? >
Count of substrings of a binary string containing K ones | method returns total number of substring having K ones ; initialize index having zero sum as 1 ; loop over binary characters of string ; update countOfOne variable with value of ith character ; if value reaches more than K , then update result ; add frequency of indices , having sum ( current sum - K ) , to the result ; update frequency of one 's count ; Driver code
< ? php function countOfSubstringWithKOnes ( $ s , $ K ) { $ N = strlen ( $ s ) ; $ res = 0 ; $ countOfOne = 0 ; $ freq = array ( ) ; for ( $ i = 0 ; $ i <= $ N ; $ i ++ ) $ freq [ $ i ] = 0 ; $ freq [ 0 ] = 1 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ countOfOne += ( $ s [ $ i ] - '0' ) ; if ( $ countOfOne >= $ K ) { $ res = $ res + $ freq [ $ countOfOne - $ K ] ; } $ freq [ $ countOfOne ] ++ ; } return $ res ; } $ s = "10010" ; $ K = 1 ; echo countOfSubstringWithKOnes ( $ s , $ K ) , " " ; ? >
Next higher palindromic number using the same set of digits | function to reverse the digits in the range i to j in ' num ' ; function to find next higher palindromic number using the same set of digits ; if length of number is less than '3' then no higher palindromic number can be formed ; find the index of last digit in the 1 st half of ' num ' ; Start from the ( mid - 1 ) th digit and find the first digit that is smaller than the digit next to it . ; If no such digit is found , then all digits are in descending order which means there cannot be a greater palindromic number with same set of digits ; Find the smallest digit on right side of ith digit which is greater than num [ i ] up to index ' mid ' ; swap num [ i ] with num [ smallest ] ; as the number is a palindrome , the same swap of digits should be performed in the 2 nd half of ' num ' ; reverse digits in the range ( i + 1 ) to mid ; if n is even , then reverse digits in the range mid + 1 to n - i - 2 ; else if n is odd , then reverse digits in the range mid + 2 to n - i - 2 ; required next higher palindromic number ; Driver Code
< ? php function reverse ( & $ num , $ i , $ j ) { while ( $ i < $ j ) { $ t = $ num [ $ i ] ; $ num [ $ i ] = $ num [ $ j ] ; $ num [ $ j ] = $ t ; $ i ++ ; $ j -- ; } } function nextPalin ( $ num , $ n ) { if ( $ n <= 3 ) { echo " Not ▁ Possible " ; return ; } $ mid = ( $ n / 2 ) - 1 ; $ i = $ mid - 1 ; $ j ; for ( ; $ i >= 0 ; $ i -- ) if ( $ num [ $ i ] < $ num [ $ i + 1 ] ) break ; if ( $ i < 0 ) { echo " Not ▁ Possible " ; return ; } $ smallest = $ i + 1 ; $ j = 0 ; for ( $ j = $ i + 2 ; $ j <= $ mid ; $ j ++ ) if ( $ num [ $ j ] > $ num [ $ i ] && $ num [ $ j ] < $ num [ $ smallest ] ) $ smallest = $ j ; $ t = $ num [ $ i ] ; $ num [ $ i ] = $ num [ $ smallest ] ; $ num [ $ smallest ] = $ t ; $ t = $ num [ $ n - $ i - 1 ] ; $ num [ $ n - $ i - 1 ] = $ num [ $ n - $ smallest - 1 ] ; $ num [ $ n - $ smallest - 1 ] = $ t ; reverse ( $ num , $ i + 1 , $ mid ) ; if ( $ n % 2 == 0 ) reverse ( $ num , $ mid + 1 , $ n - $ i - 2 ) ; else reverse ( $ num , $ mid + 2 , $ n - $ i - 2 ) ; echo " Next ▁ Palindrome : ▁ " . $ num ; } $ num = "4697557964" ; $ n = strlen ( $ num ) ; nextPalin ( $ num , $ n ) ; ? >
Print N | function to generate n digit numbers ; if number generated ; Append 1 at the current number and reduce the remaining places by one ; If more ones than zeros , append 0 to the current number and reduce the remaining places by one ; Driver Code ; Function call
< ? php function printRec ( $ number , $ extraOnes , $ remainingPlaces ) { if ( 0 == $ remainingPlaces ) { echo ( $ number . " " ) ; return ; } printRec ( $ number . "1" , $ extraOnes + 1 , $ remainingPlaces - 1 ) ; if ( 0 < $ extraOnes ) printRec ( $ number . "0" , $ extraOnes - 1 , $ remainingPlaces - 1 ) ; } function printNums ( $ n ) { $ str = " " ; printRec ( $ str , 0 , $ n ) ; } $ n = 4 ; printNums ( $ n ) ;
Longest Common Substring in an Array of Strings | function to find the stem ( longest common substring ) from the string array ; Determine size of the array ; Take first word from array as reference ; generating all possible substrings of our reference string arr [ 0 ] i . e s ; Check if the generated stem is common to all words ; If current substring is present in all strings and its length is greater than current result ; Driver Code ; Function call
< ? php function findstem ( $ arr ) { $ n = count ( $ arr ) ; $ s = $ arr [ 0 ] ; $ len = strlen ( $ s ) ; $ res = " " ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j <= $ len ; $ j ++ ) { $ stem = substr ( $ s , $ i , $ j - $ i ) ; $ k = 1 ; for ( $ k = 1 ; $ k < $ n ; $ k ++ ) if ( ! strpos ( $ arr [ $ k ] , $ stem ) ) break ; if ( $ k <= $ n && strlen ( $ res ) < strlen ( $ stem ) ) $ res = $ stem ; } } return $ res ; } $ arr = array ( " grace " , " graceful " , " disgraceful " , " gracefully " ) ; $ stems = findstem ( $ arr ) ; print ( $ stems ) ; ? >
Next higher number using atmost one swap operation | function to find the next higher number using atmost one swap operation ; to store the index of the largest digit encountered so far from the right ; to store the index of rightmost digit which has a digit greater to it on its right side ; finding the ' index ' of rightmost digit which has a digit greater to it on its right side ; required digit found , store its ' index ' and break ; if no such digit is found which has a larger digit on its right side ; to store the index of the smallest digit greater than the digit at ' index ' and right to it ; finding the index of the smallest digit greater than the digit at ' index ' and right to it ; swapping the digits ; required number ; Driver Code
< ? php function nxtHighUsingAtMostOneSwap ( $ num ) { $ l = strlen ( $ num ) ; $ posRMax = $ l - 1 ; $ index = -1 ; for ( $ i = $ l - 2 ; $ i >= 0 ; $ i -- ) { if ( $ num [ $ i ] >= $ num [ $ posRMax ] ) $ posRMax = $ i ; else { $ index = $ i ; break ; } } if ( $ index == -1 ) return " Not ▁ Possible " ; $ greatSmallDgt = -1 ; for ( $ i = $ l - 1 ; $ i > $ index ; $ i -- ) { if ( $ num [ $ i ] > $ num [ $ index ] ) { if ( $ greatSmallDgt == -1 ) $ greatSmallDgt = $ i ; else if ( $ num [ $ i ] <= $ num [ $ greatSmallDgt ] ) $ greatSmallDgt = $ i ; } } $ temp = $ num [ $ index ] ; $ num [ $ index ] = $ num [ $ greatSmallDgt ] ; $ num [ $ greatSmallDgt ] = $ temp ; return $ num ; } $ num = "218765" ; echo " Original ▁ number : ▁ " . $ num . " STRNEWLINE " ; echo " Next ▁ higher ▁ number : ▁ " . nxtHighUsingAtMostOneSwap ( $ num ) ; ? >
Longest substring of vowels | PHP program to find the longest substring of vowels . ; Increment current count if s [ i ] is vowel ; check previous value is greater then or not ; Driver code
< ? php function isVowel ( $ c ) { return ( $ c == ' a ' $ c == ' e ' $ c == ' i ' $ c == ' o ' $ c == ' u ' ) ; } function longestVowel ( $ s ) { $ count = 0 ; $ res = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( isVowel ( $ s [ $ i ] ) ) $ count ++ ; else { $ res = max ( $ res , $ count ) ; $ count = 0 ; } } return max ( $ res , $ count ) ; } $ s = " theeare " ; echo longestVowel ( $ s ) ; ? >
Number of substrings with count of each character as k | PHP program to count number of substrings with counts of distinct characters as k . ; Returns true if all values in freq [ ] are either 0 or k . ; Returns count of substrings where frequency of every present character is k ; Pick a starting point ; Initialize all frequencies as 0 for this starting point ; One by one pick ending points ; Increment frequency of current char ; If frequency becomes more than k , we can 't have more substrings  starting with i ; If frequency becomes k , then check other frequencies as well . ; Driver code
< ? php $ MAX_CHAR = 26 ; function check ( & $ freq , $ k ) { global $ MAX_CHAR ; for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) if ( $ freq [ $ i ] && $ freq [ $ i ] != $ k ) return false ; return true ; } function substrings ( $ s , $ k ) { global $ MAX_CHAR ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { $ freq = array_fill ( 0 , $ MAX_CHAR , NULL ) ; for ( $ j = $ i ; $ j < strlen ( $ s ) ; $ j ++ ) { $ index = ord ( $ s [ $ j ] ) - ord ( ' a ' ) ; $ freq [ $ index ] ++ ; if ( $ freq [ $ index ] > $ k ) break ; else if ( $ freq [ $ index ] == $ k && check ( $ freq , $ k ) == true ) $ res ++ ; } } return $ res ; } $ s = " aabbcc " ; $ k = 2 ; echo substrings ( $ s , $ k ) . " STRNEWLINE " ; $ s = " aabbc " ; $ k = 2 ; echo substrings ( $ s , $ k ) . " STRNEWLINE " ; ? >
Recursive solution to count substrings with same first and last characters | Function to count substrings with same first and last characters ; base cases ; Driver Code
< ? php function countSubstrs ( $ str , $ i , $ j , $ n ) { if ( $ n == 1 ) return 1 ; if ( $ n <= 0 ) return 0 ; $ res = countSubstrs ( $ str , $ i + 1 , $ j , $ n - 1 ) + countSubstrs ( $ str , $ i , $ j - 1 , $ n - 1 ) - countSubstrs ( $ str , $ i + 1 , $ j - 1 , $ n - 2 ) ; if ( $ str [ $ i ] == $ str [ $ j ] ) $ res ++ ; return $ res ; } $ str = " abcab " ; $ n = strlen ( $ str ) ; echo ( countSubstrs ( $ str , 0 , $ n - 1 , $ n ) ) ; ? >
Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character | Counts the no of manipulations required ; store the count of character ; iterate though the first String and update count ; iterate through the second string update char_count . if character is not found in char_count then increase count ; Driver code
< ? php function countManipulations ( $ s1 , $ s2 ) { $ count = 0 ; $ char_count = array_fill ( 0 , 26 , 0 ) ; for ( $ i = 0 ; $ i < strlen ( $ s1 ) ; $ i ++ ) $ char_count [ ord ( $ s1 [ $ i ] ) - ord ( ' a ' ) ] += 1 ; for ( $ i = 0 ; $ i < strlen ( $ s2 ) ; $ i ++ ) { $ char_count [ ord ( $ s2 [ $ i ] ) - ord ( ' a ' ) ] -= 1 ; } for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ char_count [ i ] != 0 ) { $ count += abs ( $ char_count [ i ] ) ; } } return $ count ; } $ s1 = " ddcf " ; $ s2 = " cedk " ; echo countManipulations ( $ s1 , $ s2 ) ; ? >
Given two strings check which string makes a palindrome first | Given two strings , check which string makes palindrome first . ; returns winner of two strings ; Count frequencies of characters in both given strings ; Check if there is a character that appears more than once in A and does not appear in B ; Driver Code
< ? php $ MAX_CHAR = 26 ; function stringPalindrome ( $ A , $ B ) { global $ MAX_CHAR ; $ countA = array_fill ( 0 , $ MAX_CHAR , 0 ) ; $ countB = array_fill ( 0 , $ MAX_CHAR , 0 ) ; $ l1 = strlen ( $ A ) ; $ l2 = strlen ( $ B ) ; for ( $ i = 0 ; $ i < $ l1 ; $ i ++ ) $ countA [ ord ( $ A [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ l2 ; $ i ++ ) $ countB [ ord ( $ B [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) if ( ( $ countA [ $ i ] > 1 && $ countB [ $ i ] == 0 ) ) return ' A ' ; return ' B ' ; } $ a = " abcdea " ; $ b = " bcdesg " ; echo stringPalindrome ( $ a , $ b ) ; ? >
Find numbers of balancing positions in string | PHP program to find number of balancing points in string ; function to return number of balancing points ; hash array for storing hash of string initialized by 0 being global ; process string initially for rightVisited ; check for balancing points ; for every position inc left hash & dec rightVisited ; check whether both hash have same character or not ; Either both leftVisited [ j ] and rightVisited [ j ] should have none zero value or both should have zero value ; if both have same character increment count ; Driver Code
< ? php $ MAX_CHAR = 256 ; function countBalance ( $ str ) { global $ MAX_CHAR ; $ leftVisited = array_fill ( 0 , $ MAX_CHAR , NULL ) ; $ rightVisited = array_fill ( 0 , $ MAX_CHAR , NULL ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ rightVisited [ ord ( $ str [ $ i ] ) ] ++ ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ leftVisited [ ord ( $ str [ $ i ] ) ] ++ ; $ rightVisited [ ord ( $ str [ $ i ] ) ] -- ; for ( $ j = 0 ; $ j < $ MAX_CHAR ; $ j ++ ) { if ( ( $ leftVisited [ $ j ] == 0 && $ rightVisited [ $ j ] != 0 ) || ( $ leftVisited [ $ j ] != 0 && $ rightVisited [ $ j ] == 0 ) ) break ; } if ( $ j == $ MAX_CHAR ) $ res ++ ; } return $ res ; } $ str = " abaababa " ; echo countBalance ( $ str ) ; ? >
Min flips of continuous characters to make all characters same in a string | To find min number of flips in binary string ; If last character is not equal to str [ i ] increase res ; To return min flips ; Driver Code
< ? php function findFlips ( $ str , $ n ) { $ last = ' ▁ ' ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ last != $ str [ $ i ] ) $ res ++ ; $ last = $ str [ $ i ] ; } return intval ( $ res / 2 ) ; } $ str = "00011110001110" ; $ n = strlen ( $ str ) ; echo findFlips ( $ str , $ n ) ; ? >
Maximum length substring having all same characters after k changes | function to find the maximum length of substring having character ch ; traverse the whole string ; if character is not same as ch increase count ; While count > k traverse the string again until count becomes less than k and decrease the count when characters are not same ; length of substring will be rightIndex - leftIndex + 1. Compare this with the maximum length and return maximum length ; function which returns maximum length of substring ; Driver code
< ? php function findLen ( $ A , $ n , $ k , $ ch ) { $ maxlen = 1 ; $ cnt = 0 ; $ l = 0 ; $ r = 0 ; while ( $ r < $ n ) { if ( $ A [ $ r ] != $ ch ) ++ $ cnt ; while ( $ cnt > $ k ) { if ( $ A [ $ l ] != $ ch ) -- $ cnt ; ++ $ l ; } $ maxlen = max ( $ maxlen , $ r - $ l + 1 ) ; ++ $ r ; } return $ maxlen ; } function answer ( $ A , $ n , $ k ) { $ maxlen = 1 ; for ( $ i = 0 ; $ i < 26 ; ++ $ i ) { $ maxlen = max ( $ maxlen , findLen ( $ A , $ n , $ k , $ i + ' A ' ) ) ; $ maxlen = max ( $ maxlen , findLen ( $ A , $ n , $ k , $ i + ' a ' ) ) ; } return $ maxlen ; } $ n = 5 ; $ k = 2 ; $ A = " ABABA " ; echo " Maximum length = " ▁ . ▁ answer ( $ A , ▁ $ n , ▁ $ k ) ▁ . ▁ " " $ n = 6 ; $ k = 4 ; $ B = " HHHHHH " ; echo " Maximum length = " ▁ . ▁ answer ( $ B , ▁ $ n , ▁ $ k ) ▁ . ▁ " " ? >
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 ; $ count1 = array_fill ( 0 , $ MAX_CHAR , NULL ) ; $ count2 = array_fill ( 0 , $ MAX_CHAR , NULL ) ; $ n = strlen ( $ s ) ; if ( $ n == 1 ) return true ; for ( $ i = 0 , $ j = $ n - 1 ; $ i < $ j ; $ i ++ , $ j -- ) { $ count1 [ $ s [ $ i ] - ' a ' ] ++ ; $ count2 [ $ s [ $ j ] - ' a ' ] ++ ; } for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) if ( $ count1 [ $ i ] != $ count2 [ $ i ] ) return false ; return true ; } $ s = " abab " ; if ( checkCorrectOrNot ( $ s ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
Extract maximum numeric value from a given string | Set 1 ( General approach ) | Utility function to find maximum string ; If both having equal lengths ; Reach first unmatched character / value ; Return string with maximum value ; If different lengths return string with maximum length ; Function to extract the maximum value ; Start traversing the string ; Ignore leading zeroes ; Store numeric value into a string ; Update maximum string ; To handle the case if there is only 0 numeric value ; Return maximum string ; Drivers program
< ? php function maximumNum ( $ curr_num , $ res ) { $ len1 = strlen ( $ curr_num ) ; $ len2 = strlen ( $ res ) ; if ( $ len1 == $ len2 ) { $ i = 0 ; while ( $ curr_num [ $ i ] == $ res [ $ i ] ) $ i ++ ; if ( $ curr_num [ $ i ] < $ res [ $ i ] ) return $ res ; else return $ curr_num ; } return $ len1 < $ len2 ? $ res : $ curr_num ; } function extractMaximum ( $ str ) { $ n = strlen ( $ str ) ; $ curr_num = " " ; $ res = " " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { while ( $ i < $ n && $ str [ $ i ] == '0' ) $ i ++ ; while ( $ i < $ n && $ str [ $ i ] >= '0' && $ str [ $ i ] <= '9' ) { $ curr_num . = $ str [ $ i ] ; $ i ++ ; } if ( $ i == $ n ) break ; if ( strlen ( $ curr_num ) > 0 ) $ i -- ; $ res = maximumNum ( $ curr_num , $ res ) ; $ curr_num = " " ; } if ( strlen ( $ curr_num ) == 0 && strlen ( $ res ) == 0 ) $ res . = '0' ; return maximumNum ( $ curr_num , $ res ) ; } $ str = "100klh564abc365bg " ; echo extractMaximum ( $ str ) ; ? >
To check divisibility of any large number by 999 | function to check divisibility ; Append required 0 s at the beginning . ; add digits in group of three in gSum ; group saves 3 - digit group ; calculate result till 3 digit sum ; Driver Code
< ? php function isDivisible999 ( $ num ) { $ n = strlen ( $ num ) ; if ( $ n == 0 && $ num [ 0 ] == '0' ) return true ; if ( $ n % 3 == 1 ) $ num = "00" . $ num ; if ( $ n % 3 == 2 ) $ num = "0" . $ num ; $ gSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i += 3 ) { $ group = 0 ; $ group += ( ord ( $ num [ $ i ] ) - 48 ) * 100 ; $ group += ( ord ( $ num [ $ i + 1 ] ) - 48 ) * 10 ; $ group += ( ord ( $ num [ $ i + 2 ] ) - 48 ) ; $ gSum += $ group ; } if ( $ gSum > 1000 ) { $ num = strval ( $ gSum ) ; $ n = strlen ( $ num ) ; $ gSum = isDivisible999 ( $ num ) ; } return ( $ gSum == 999 ) ; } $ num = "1998" ; if ( isDivisible999 ( $ num ) ) echo " Divisible " ; else echo " Not ▁ divisible " ; ? >
Rearrange a string in sorted order followed by the integer sum | PHP program for above implementation ; Function to return string in lexicographic order followed by integers sum ; Traverse the string ; Count occurrence of uppercase alphabets ; Store sum of integers ; Traverse for all characters A to Z ; Append the current character in the string no . of times it occurs in the given string ; Append the sum of integers ; return resultant string ; Driver Code
< ? php $ MAX_CHAR = 26 ; function arrangeString ( $ str ) { global $ MAX_CHAR ; $ char_count = array_fill ( 0 , $ MAX_CHAR , NULL ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] >= ' A ' && $ str [ $ i ] <= ' Z ' ) $ char_count [ ord ( $ str [ $ i ] ) - ord ( ' A ' ) ] ++ ; else $ sum = $ sum + ( ord ( $ str [ $ i ] ) - ord ( '0' ) ) ; } $ res = " " ; for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) { $ ch = chr ( ord ( ' A ' ) + $ i ) ; while ( $ char_count [ $ i ] -- ) $ res = $ res . $ ch ; } if ( $ sum > 0 ) $ res = $ res . strval ( $ sum ) ; return $ res ; } $ str = " ACCBA10D2EW30" ; echo arrangeString ( $ str ) ; ? >
Program to print all substrings of a given string | Function to print all sub strings ; Pick starting point ; Pick ending point ; Print characters from current starting point to current ending point . ; Driver Code
< ? php function subString ( $ str , $ n ) { for ( $ len = 1 ; $ len <= $ n ; $ len ++ ) { for ( $ i = 0 ; $ i <= $ n - $ len ; $ i ++ ) { $ j = $ i + $ len - 1 ; for ( $ k = $ i ; $ k <= $ j ; $ k ++ ) echo $ str [ $ k ] ; echo " STRNEWLINE " ; } } } $ str = " abc " ; subString ( $ str , strlen ( $ str ) ) ; ? >
Program to count vowels in a string ( Iterative and Recursive ) | Function to check the Vowel ; Returns count of vowels in str ; if ( isVowel ( $str [ $i ] ) ) Check for vowel ; 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 countVowels ( $ str ) { $ count = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) ++ $ count ; return $ count ; } $ str = " abc ▁ de " ; echo countVowels ( $ str ) . " STRNEWLINE " ; ? >
Generate all rotations of a given string | Print all the rotated string . ; Generate all rotations one by one and print ; Current index in str ; Current index in temp ; Copying the second part from the point of rotation . ; Copying the first part from the point of rotation . ; Driver Code
< ? php function printRotatedString ( $ str ) { $ len = strlen ( $ str ) ; $ temp = " ▁ " ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ j = $ i ; $ k = 0 ; while ( $ j < $ len ) { $ temp [ $ k ] = $ str [ $ j ] ; $ k ++ ; $ j ++ ; } $ j = 0 ; while ( $ j < $ i ) { $ temp [ $ k ] = $ str [ $ j ] ; $ j ++ ; $ k ++ ; } echo $ temp . " STRNEWLINE " ; } } $ str = " geeks " ; printRotatedString ( $ str ) ; ? >
Longest Non | utility function to check whether a string is palindrome or not ; Check for palindrome . ; palindrome string ; function to find maximum length substring which is not palindrome ; to check whether all characters of the string are same or not ; All characters are same , we can 't  make a non-palindromic string. ; If string is palindrome , we can make it non - palindrome by removing any corner character ; Complete string is not a palindrome . ; Driver code
< ? php function isPalindrome ( $ str ) { $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) if ( $ str [ $ i ] != $ str [ $ n - $ i - 1 ] ) return false ; return true ; } function maxLengthNonPalinSubstring ( $ str ) { $ n = strlen ( $ str ) ; $ ch = $ str [ 0 ] ; $ i = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ str [ $ i ] != $ ch ) break ; if ( $ i == $ n ) return 0 ; if ( isPalindrome ( $ str ) ) return ( $ n - 1 ) ; return $ n ; } $ str = " abba " ; echo " Maximum ▁ Length ▁ = ▁ " , maxLengthNonPalinSubstring ( $ str ) ; ? >
Check whether K | PHP program to check if k - th bit of a given number is set or not using right shift operator . ; Driver code
< ? php function isKthBitSet ( $ n , $ k ) { if ( ( $ n >> ( $ k - 1 ) ) & 1 ) echo " SET " ; else echo " NOT ▁ SET " ; } $ n = 5 ; $ k = 1 ; isKthBitSet ( $ n , $ k ) ; ? >