Arya has a string, S, of uppercase English letters. She writes down the string S on a paper K times.
She wants to calculate the occurrence of a specific letter in the first N characters of the final string.
The first input statement contains a string. The second input statement contains 2 space-separated integers K and N, and an uppercase English letter C whose occurrence needs to be counted.
Examples :
Input : ABA Input : 3 7 B Output : 2 Input : BHD Input : 4 6 E Output : 0
Explanation : Case 1 : Final string – ABAABAABA
Case 2 : Final string – BHDBHDBHDBHD
Implementation Approach: Count the number of characters c in given string s as char_count. Find the complete number of strings that can be formed within ‘n’ length. Multiply it by char_count. Then, find the remainder of ‘n’ and length of s, this is the unchecked part of concatenated string. Now, check for character c in remaining of concatenated string and add to char_count. Finally, print it.
Implementation :
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
string s;
cin>>s;
ll count = 0, charcount = 0;
ll k, n;
cin>>k>>n;
char c;
cin>>c;
for(int i = 0; s[i]; i++)
{
if(s[i] == c)
charcount++; //number of chars in given string
}
count = n/s.length(); //total number of complete given string in first n chars
charcount *= count; //total number of chars in concatenated string
ll rem = n%s.length(); //remaining length of concatenated string that is not checked for char 'c'
for(int i = 0; i < rem; i++)
{
if(s[i] == c)
charcount++; //final number of char 'c' in first 'N' chars of concatenated string
}
cout<<charcount<<endl;
return 0;
}