Count strings that end with the given pattern
Last Updated :
29 Nov, 2022
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 skips 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 string
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 strings
def countOfstrings(pat, n, sArr):
count = 0
for i in range(n):
# If current string ends with
# the given pattern
if (endsWith(sArr[i], pat) == True):
count += 1
return count
# Driver code
pat = "ks"
n = 4
sArr= [ "geeks", "geeksforgeeks",
"games", "unit"]
print(countOfstrings(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
?>
JavaScript
<script>
// JavaScript implementation of the approach
// Function that return true
// if str ends with pat
function endsWith(str,pat)
{
let patLen = pat.length;
let 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
function countOfStrings(pat,n,sArr)
{
let count = 0;
for (let i = 0; i < n; i++)
{
// If current string ends with
// the given pattern
if (endsWith(sArr[i], pat))
count++;
}
return count;
}
// Driver code
let pat = "ks";
let n = 4;
let sArr=[ "geeks", "geeksforgeeks", "games", "unit"];
document.write(countOfStrings(pat, n, sArr));
// This code is contributed by unknown2108
</script>
Time Complexity: O(m * n), where m is the length of pattern string and n is the size of the string array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Similar Reads
Count of substrings that start and end with 1 in given Binary String Given a binary string s, the task is to count all substrings that start and end with the character '1'. A valid substring must have both its first and last characters as '1', and can include one or more number of characters in between.Examples:Input: s = "00100101"Output: 3Explanation: Valid substri
7 min read
Count the strings that are subsequence of the given string Given a string S and an array arr[] of words, the task is to return the number of words from the array which is a subsequence of S. Examples: Input: S = âprogrammingâ, arr[] = {"prom", "amin", "proj"}Output: 2Explanation: "prom" and "amin" are subsequence of S while "proj" is not) Input: S = âgeeksf
11 min read
Different substrings in a string that start and end with given strings Given a string s and two other strings begin and end, find the number of different substrings in the string which begin and end with the given begin and end strings. Examples: Input : s = "geeksforgeeks" begin = "geeks" end = "for" Output : 1 Input : s = "vishakha" begin = "h" end = "a" Output : 2 T
9 min read
Number of substrings that start with "geeks" and end with "for" Given a string str consisting of lowercase English alphabets, the task is to find the count of substrings that start with "geeks" and end with "for". Examples: Input: str = "geeksforgeeksisforgeeks" Output: 3 "geeksfor", "geeksforgeeksisfor" and "geeksisfor" are the only valid substrings. Input: str
4 min read
Count of Substrings that can be formed without using the given list of Characters Given a string str and a list of characters L, the task is to count the total numbers of substrings of the string str without using characters given in the list L. Examples: Input: str = "abcd", L[] = {'a', 'b', 't', 'q'} Output: 3 Explanation: On ignoring the characters 'a' and 'b' from the given s
7 min read
Count Substrings with Frequencies Less than Maximum Digit Given a string S of length N. Then your task is to find the count of substrings such that, the frequency of each digit in that substring must be less than the maximum digit of substring. Examples:Input: S = 122321Output: 13Explanation: Below are some of those substrings: S[1, 2]: 12. Max element = 2
6 min read