Find the longest common prefix between two strings after performing swaps on second string | PHP program to find the longest common prefix between two strings after performing swaps on the second string ; $a = strlen ( $x ) ; length of x $b = strlen ( $y ) ; length of y ; creating frequency array of characters of y ; storing the length of longest common prefix ; checking if the frequency of the character at position i in x in b is greater than zero or not if zero we increase the prefix count by 1 ; Driver Code
< ? php function LengthLCP ( $ x , $ y ) { $ fr = array_fill ( 0 , 26 , NULL ) ; for ( $ i = 0 ; $ i < $ b ; $ i ++ ) { $ fr [ ord ( $ y [ $ i ] ) - 97 ] += 1 ; } $ c = 0 ; for ( $ i = 0 ; $ i < $ a ; $ i ++ ) { if ( $ fr [ ord ( $ x [ $ i ] ) - 97 ] > 0 ) { $ c += 1 ; $ fr [ ord ( $ x [ $ i ] ) - 97 ] -= 1 ; } else break ; } echo $ c ; } $ x = " here " ; $ y = " there " ; LengthLCP ( $ x , $ y ) ; return 0 ; ? >