Count strings that end with the given pattern | Function that return true if str ends with pat ; Pattern is larger in length than the string ; We match starting from the end while patLen is greater than or equal to 0. ; If at any index str doesn 't match  with pattern ; If str ends with the given pattern ; Function to return the count of required strings ; If current string ends with the given pattern ; Driver code
< ? php function endsWith ( $ str , $ pat ) { $ patLen = strlen ( $ pat ) ; $ strLen = strlen ( $ str ) ; if ( $ patLen > $ strLen ) return false ; $ patLen -- ; $ strLen -- ; while ( $ patLen >= 0 ) { if ( $ pat [ $ patLen ] != $ str [ $ strLen ] ) return false ; $ patLen -- ; $ strLen -- ; } return true ; } function countOfStrings ( $ pat , $ n , $ sArr ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( endsWith ( $ sArr [ $ i ] , $ pat ) ) $ count ++ ; return $ count ; } $ pat = " ks " ; $ n = 4 ; $ sArr = array ( " geeks " , " geeksforgeeks " , " games " , " unit " ) ; echo countOfStrings ( $ pat , $ n , $ sArr ) ; ? >