Contar las apariciones de un carácter en una string repetida

Dado un número entero N y una string en minúsculas. La string se repite infinitamente. La tarea es encontrar el número de ocurrencias de un carácter dado x en las primeras N letras.
Ejemplos: 
 

Input : N = 10 str = "abcac"
Output : 4
Explanation: "abcacabcac" is the substring from the infinitely repeated string. In first 10 letters 'a' occurs 4  times.
          
Input: N = 10, str = "aba"
Output : 7

Enfoque: 
1. Encuentre las ocurrencias del carácter ‘a’ en la string dada. 
2. Encuentre el número de repeticiones que se requieren para encontrar las ocurrencias ‘a’. 
3. Multiplique las ocurrencias de una sola string por el número de repeticiones. 
4. Si n dado no es el múltiplo del tamaño de string dado, encontraremos las ocurrencias ‘a’ en la substring restante.
A continuación se muestra la implementación del enfoque anterior:
 

C++

// CPP program to find the occurrences of
// character x in the infinite repeated string
// upto length n
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the character 'a'
int countChar(string str, char x)
{
    int count = 0, n = 10;
    for (int i = 0; i < str.size(); i++)
        if (str[i] == x)
            count++;
 
    // atleast k repetition are required
    int repetitions = n / str.size();
    count = count * repetitions;
 
    // if n is not the multiple of the string size
    // check for the remaining repeating character.
    for (int i = 0; i < n % str.size(); i++) {
        if (str[i] == x)
            count++;
    }
 
    return count;
}
 
// Driver code
int main()
{
    string str = "abcac";
    cout << countChar(str, 'a');
    return 0;
}
 
// This code is contributed by Surendra_Gangwar

Java

// Java program to find the occurrences
// of character x in the infinite
// repeated string upto length n
import java.util.*;
import java.lang.*;
 
class GFG
{
// Function to count the character 'a'
static int countChar(String str, char x)
{
    int count = 0;
    int n = 10;
    for (int i = 0; i < str.length(); i++)
        if (str.charAt(i) == x)
            count++;
 
    // atleast k repetition are required
    int repetitions = n / str.length();
    count = count * repetitions;
 
    // if n is not the multiple of the
    // string size check for the remaining
    // repeating character.
    for (int i = 0;
            i < n % str.length(); i++)
    {
        if (str.charAt(i) == x)
            count++;
    }
 
    return count;
}
 
// Driver code
public static void main(String args[])
{
    String str = "abcac";
    System.out.println(countChar(str, 'a'));
}
}
 
// This code is contributed
// by Akanksha Rai

Python3

# Python3 program to find the occurrences of
# character x in the infinite repeated string
# upto length n
 
# Function to count the character 'a'
def countChar(str, x):
    count = 0
    for i in range(len(str)):
        if (str[i] == x) :
            count += 1
    n = 10
     
    # atleast k repetition are required
    repetitions = n // len(str)
    count = count * repetitions
 
    # if n is not the multiple of the
    # string size check for the remaining
    # repeating character.
    l = n % len(str)
    for i in range(l):
        if (str[i] == x):
            count += 1
    return count
 
# Driver code
str = "abcac"
print(countChar(str, 'a'))
 
# This code is contributed
# by sahishelangia

C#

// C# program to find the occurrences
// of character x in the infinite
// repeated string upto length n
using System;
 
class GFG
{
// Function to count the character 'a'
static int countChar(string str, char x)
{
    int count = 0;
    int n = 10;
    for (int i = 0; i < str.Length; i++)
        if (str[i] == x)
            count++;
 
    // atleast k repetition are required
    int repetitions = n / str.Length;
    count = count * repetitions;
 
    // if n is not the multiple of the
    // string size check for the remaining
    // repeating character.
    for (int i = 0;
             i < n % str.Length; i++)
    {
        if (str[i] == x)
            count++;
    }
 
    return count;
}
 
// Driver code
public static void Main()
{
    string str = "abcac";
    Console.WriteLine(countChar(str, 'a'));
}
}
 
// This code is contributed
// by Akanksha Rai

PHP

<?php
// PHP program to find the occurrences
// of character x in the infinite
// repeated string upto length n
 
// Function to count the character 'a'
function countChar($str, $x)
{
    $count = 0;
    $n = 10;
    for ($i = 0; $i < strlen($str); $i++)
        if ($str[$i] == $x)
            $count++;
 
    // atleast k repetition are required
    $repetitions = (int)($n / strlen($str));
    $count = $count * $repetitions;
 
    // if n is not the multiple of
    // the string size check for the
    // remaining repeating character.
    for ($i = 0; $i < $n % strlen($str); $i++)
    {
        if ($str[$i] == $x)
            $count++;
    }
 
    return $count;
}
 
// Driver code
$str = "abcac";
echo countChar($str, 'a');
 
// This code is contributed by Sachin
?>

Javascript

<script>
 
    // JavaScript program to find the occurrences
    // of character x in the infinite
    // repeated string upto length n
     
    // Function to count the character 'a'
    function countChar(str, x)
    {
        let count = 0;
        let n = 10;
        for (let i = 0; i < str.length; i++)
            if (str[i] == x)
                count++;
 
        // atleast k repetition are required
        let repetitions = n / str.length;
        count = count * repetitions;
 
        // if n is not the multiple of the
        // string size check for the remaining
        // repeating character.
        for (let i = 0; i < n % str.length; i++)
        {
            if (str[i] == x)
                count++;
        }
 
        return count;
    }
     
    let str = "abcac";
    document.write(countChar(str, 'a'));
     
</script>

Producción:  

4

Publicación traducida automáticamente

Artículo escrito por SURENDRA_GANGWAR 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 *