Count strings that end with the given pattern | Java 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
class GfG { static boolean 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 . charAt ( patLen ) != str . charAt ( strLen ) ) return false ; patLen -- ; strLen -- ; } return true ; } static 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 ; } public static void main ( String [ ] args ) { String pat = " ks " ; int n = 4 ; String sArr [ ] = { " geeks " , " geeksforgeeks " , " games " , " unit " } ; System . out . println ( countOfStrings ( pat , n , sArr ) ) ; } }