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
function endsWith ( str , pat ) { let patLen = pat . length ; let strLen = str . length ; 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 ) { let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( endsWith ( sArr [ i ] , pat ) ) count ++ ; } return count ; } let pat = " " ; let n = 4 ; let sArr = [ " " , " " , " " , " " ] ; document . write ( countOfStrings ( pat , n , sArr ) ) ;