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++
#include <bits/stdc++.h>
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;
}
|
Java
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));
}
}
|
Python3
def endsWith(str1, pat):
patLen = len (pat)
str1Len = len (str1)
if (patLen > str1Len):
return False
patLen - = 1
str1Len - = 1
while (patLen > = 0 ):
if (pat[patLen] ! = str1[str1Len]):
return False
patLen - = 1
str1Len - = 1
return True
def countOfstrings(pat, n, sArr):
count = 0
for i in range (n):
if (endsWith(sArr[i], pat) = = True ):
count + = 1
return count
pat = "ks"
n = 4
sArr = [ "geeks" , "geeksforgeeks" ,
"games" , "unit" ]
print (countOfstrings(pat, n, sArr))
|
C#
using System;
class GFG
{
static 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 ;
}
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 pat = "ks" ;
int n = 4;
string [] sArr = { "geeks" , "geeksforgeeks" ,
"games" , "unit" };
Console.WriteLine(countOfStrings(pat, n, sArr));
}
}
|
PHP
<?php
function endsWith( $str , $pat )
{
$patLen = strlen ( $pat );
$strLen = strlen ( $str );
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 )
{
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
if (endsWith( $sArr [ $i ], $pat ))
$count ++;
return $count ;
}
$pat = "ks" ;
$n = 4;
$sArr = array ( "geeks" , "geeksforgeeks" ,
"games" , "unit" );
echo countOfStrings( $pat , $n , $sArr );
?>
|
Javascript
<script>
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 = "ks" ;
let n = 4;
let sArr=[ "geeks" , "geeksforgeeks" , "games" , "unit" ];
document.write(countOfStrings(pat, n, sArr));
</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.