Contar strings que terminan con el patrón dado

Dado un patrón pat y una array de strings sArr[] , la tarea es contar el número de strings de la array que termina con el patrón dado.
Ejemplos: 
 

Entrada: pat = “ks”, sArr[] = {“geeks”, “geeksforgeeks”, “games”, “unit”} 
Salida:
Solo las strings “geeks” y “geeksforgeeks” terminan con el patrón “ks”.
Entrada: pat = “abc”, sArr[] = {“abcd”, “abcc”, “aaa”, “bbb”} 
Salida:
 

Acercarse: 
 

  • Inicialice count = 0 y comience a recorrer la array de strings dada.
  • Para cada string str , inicialice strLen = len(str) y patLen = len(pattern)
    • Si patLen > strLen salta a la siguiente string ya que la string actual no puede terminar con el patrón dado.
    • De lo contrario, haga coincidir la string con el patrón comenzando desde el final. Si la string coincide con el patrón, actualice count = count + 1 .
  • Imprime el conteo al final.

A continuación se muestra la implementación del enfoque anterior: 
 

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>
Producción: 

2

 

Publicación traducida automáticamente

Artículo escrito por Vivek.Pandit y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *