Given a pattern pat and a string array sArr[], the task is to count the number of strings from the array that ends with the given pattern.
Examples:
Input: pat = “ks”, sArr[] = {“geeks”, “geeksforgeeks”, “games”, “unit”}
Output: 2
Only string “geeks” and “geeksforgeeks” end with the pattern “ks”.Input: pat = “abc”, sArr[] = {“abcd”, “abcc”, “aaa”, “bbb”}
Output: 0
Approach:
- Initialize count = 0 and start traversing the given string array.
- For every string str, initialize strLen = len(str) and patLen = len(pattern).
- If patLen > strLen then skip to the next string as the current string cannot end with the given pattern.
- Else match the string with the pattern starting from the end. If the string matches the pattern then update count = count + 1.
- Print the count in the end.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that return true if str // ends with pat bool endsWith(string str, string pat) { int patLen = pat.length(); int strLen = str.length(); // Pattern is larger in length than // the string if (patLen > strLen) return false; // We match starting from the end while // patLen is greater than or equal to 0. patLen--; strLen--; while (patLen >= 0) { // If at any index str doesn't match // with pattern if (pat[patLen] != str[strLen]) return false; patLen--; strLen--; } // If str ends with the given pattern return true; } // Function to return the count of required // strings int countOfStrings(string pat, int n, string sArr[]) { int count = 0; for (int i = 0; i < n; i++) // If current string ends with // the given pattern if (endsWith(sArr[i], pat)) count++; return count; } // Driver code int main() { string pat = "ks"; int n = 4; string sArr[] = { "geeks", "geeksforgeeks", "games", "unit" }; cout << countOfStrings(pat, n, sArr); return 0; } |
Java
// Java implementation of the approach class GfG { // Function that return true // if str ends with pat static boolean endsWith(String str, String pat) { int patLen = pat.length(); int strLen = str.length(); // Pattern is larger in length // than the string if (patLen > strLen) return false; // We match starting from the end while // patLen is greater than or equal to 0. patLen--; strLen--; while (patLen >= 0) { // If at any index str doesn't match // with pattern if (pat.charAt(patLen) != str.charAt(strLen)) return false; patLen--; strLen--; } // If str ends with the given pattern return true; } // Function to return the // count of required strings static int countOfStrings(String pat, int n, String sArr[]) { int count = 0; for (int i = 0; i < n; i++) { // If current string ends with // the given pattern if (endsWith(sArr[i], pat)) count++; } return count; } // Driver code 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)); } } // This code is contributed by Rituraj Jain |
Python3
# Python3 implementation of the approach # Function that return true if str1 # ends with pat def endsWith(str1, pat): patLen = len(pat) str1Len = len(str1) # Pattern is larger in length # than the str1ing if (patLen > str1Len): return False # We match starting from the end while # patLen is greater than or equal to 0. patLen -= 1 str1Len -= 1 while (patLen >= 0): # If at any index str1 doesn't match # with pattern if (pat[patLen] != str1[str1Len]): return False patLen -= 1 str1Len -= 1 # If str1 ends with the given pattern return True # Function to return the count of # required str1ings def countOfstr1ings(pat, n, sArr): count = 0 for i in range(n): # If current str1ing ends with # the given pattern if (endsWith(sArr[i], pat) == True): count += 1 return count # Driver code pat = "ks"n = 4sArr= [ "geeks", "geeksforgeeks", "games", "unit"] print(countOfstr1ings(pat, n, sArr)) # This code is contributed by # Mohit kumar 29 |
C#
// C# implementation of the approach using System; class GFG { // Function that return true if str // ends with pat static bool endsWith(string str, string pat) { int patLen = pat.Length; int strLen = str.Length; // Pattern is larger in length than // the string if (patLen > strLen) return false; // We match starting from the end while // patLen is greater than or equal to 0. patLen--; strLen--; while (patLen >= 0) { // If at any index str doesn't match // with pattern if (pat[patLen] != str[strLen]) return false; patLen--; strLen--; } // If str ends with the given pattern return true; } // Function to return the count of required // strings static int countOfStrings(string pat, int n, string[] sArr) { int count = 0; for (int i = 0; i < n; i++) // If current string ends with // the given pattern if (endsWith(sArr[i], pat)) count++; return count; } // Driver code public static void Main() { string pat = "ks"; int n = 4; string[] sArr = { "geeks", "geeksforgeeks", "games", "unit" }; Console.WriteLine(countOfStrings(pat, n, sArr)); } } // This code is contributed by Akanksha Rai |
PHP
<?php // PHP implementation of the approach // Function that return true if str // ends with pat function endsWith($str, $pat) { $patLen = strlen($pat); $strLen = strlen($str); // Pattern is larger in length than // the string if ($patLen > $strLen) return false; // We match starting from the end while // patLen is greater than or equal to 0. $patLen--; $strLen--; while ($patLen >= 0) { // If at any index str doesn't match // with pattern if ($pat[$patLen] != $str[$strLen]) return false; $patLen--; $strLen--; } // If str ends with the given pattern return true; } // Function to return the count of required // strings function countOfStrings($pat, $n, $sArr) { $count = 0; for ($i = 0; $i < $n; $i++) // If current string ends with // the given pattern if (endsWith($sArr[$i], $pat)) $count++; return $count; } // Driver code $pat = "ks"; $n = 4; $sArr = array("geeks", "geeksforgeeks", "games", "unit"); echo countOfStrings($pat, $n, $sArr); // This code is contributed by mits ?> |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Generate all binary strings from given pattern
- Count of same length Strings that exists lexicographically in between two given Strings
- Find all strings that match specific pattern in a dictionary
- Count of strings that become equal to one of the two strings after one removal
- Count of binary strings of length N having equal count of 0's and 1's
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of occurrences of a "1(0+)1" pattern in a string
- Queries to find the count of integers in a range that contain the given pattern
- Count of all sub-strings with sum of weights at most K
- Count of sub-strings that contain character X at least once
- Count of sub-strings that are divisible by K
- Count common subsequence in two strings
- Count of sub-strings with equal consecutive 0's and 1's
- Count All Palindrome Sub-Strings in a String | Set 2
- Count of sub-strings that do not contain all the characters from the set {'a', 'b', 'c'} at the same time
- Count All Palindrome Sub-Strings in a String | Set 1
- Count common characters in two strings
- Count of sub-strings of length n possible from the given string
- Count of sub-strings that do not consist of the given character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

