Count strings that end with the given pattern | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool endsWith ( string str , string pat ) { int patLen = pat . length ( ) ; int strLen = str . length ( ) ; if ( patLen > strLen ) return false ; patLen -- ; strLen -- ; while ( patLen >= 0 ) { if ( pat [ patLen ] != str [ strLen ] ) return false ; patLen -- ; strLen -- ; } return true ; } int countOfStrings ( string pat , int n , string sArr [ ] ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( endsWith ( sArr [ i ] , pat ) ) count ++ ; return count ; } int main ( ) { string pat = " ks " ; int n = 4 ; string sArr [ ] = { " geeks " , " geeksforgeeks " , " games " , " unit " } ; cout << countOfStrings ( pat , n , sArr ) ; return 0 ; }