HackerRank Repeated String Solution

ByBrokenprogrammers

Dec 13, 2022

Hello Programmers, In this post, you will know how to solve the HackerRank Repeated String Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Repeated String Solution

There is a string, s, of lowercase English letters that is repeated infinitely many times. Given an integer, n, find and print the number of letter `a`‘s in the first n letters of the infinite string.

Example
s = ‘abcac’
n = 10

The substring we consider is abcacabcac, the first 10 characters of the infinite string. There are 4 occurrences of `a` in the substring.

Function Description

Complete the repeatedString function in the editor below.

repeatedString has the following parameter(s):

• s: a string to repeat
• n: the number of characters to consider

Returns

• int: the frequency of `a` in the substring

Input Format

The first line contains a single string, s.
The second line contains an integer, n.

Constraints

• 1 <= |s| <= 100
• 1 <= n <= 1012
• For 25% of the test cases, n <= 106.

Sample Input

Sample Input 0

aba
10

Sample Output 0

7

Explanation 0

The first n = 10 letters of the infinite string are `abaabaabaa`. Because there are 7 `a`s, we return 7.

Sample Input 1

a
1000000000000

Sample Output 1

1000000000000

Explanation 1

Because all of the first n = 1000000000000 letters of the infinite string are `a`, we return 1000000000000.

HackerRank Repeated String Solutions

Repeated String Solution in C

```#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
char* s = (char *)malloc(512000 * sizeof(char));
scanf("%s",s);
long n,o,p,i;
scanf("%ld",&n);
o=0;
for(i=0;s[i]!='\0';i++)
{
if(s[i]=='a')
o++;
}
p=n%i;
n=n/i;
o=o*n;
n=0;
for(i=0;i<p;i++)
if(s[i]=='a')
n++;
printf("%ld",o+n);
return 0;
}```

Repeated String Solution in Cpp

```#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector< vector<int> > vvi;
typedef vector<ll> vl;
typedef vector< vector<ll> > vvl;
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define forv(i, v) forn(i, v.size())
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
int main() {
#ifdef NEREVAR_PROJECT
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
string s;
ll n;
cin >> s >> n;
ll ca = 0;
forv(i, s) if (s[i] == 'a') ca++;
int m = (int)s.size();
ll k = n / m;
int r = n % m;
ll ans = k * ca;
forn(i, r) if (s[i] == 'a') ans++;
cout << ans << endl;
return 0;
}```

Repeated String Solution in Java

```import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
long n = in.nextLong();
long num = n/s.length();
long rem = n%s.length();
long ans = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i)=='a') {
ans += num;
if (i < rem)
ans++;
}
}
System.out.println(ans);
}
}```

Repeated String Solution in Python

```#!/bin/python
import sys
s = raw_input().strip()
n = long(raw_input().strip())
cnt=0
cnt1=0
L=len(s)
k=n%L
for i in range(L):
if s[i]=='a':
cnt+=1
if i<k: cnt1+=1
print cnt*(n/L)+cnt1```

Repeated String Solution using JavaScript

```process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {

var stringSize = s.length;
var a = s.filter((a) => a == 'a').length;

var repeat = Math.floor(n/stringSize);

var left = n-(repeat*stringSize);

console.log((repeat*a) + s.filter((a,i) => a == 'a' && i < left).length);```

Repeated String Solution in Scala

```object Solution {
def main(args: Array[String]) {
val sc = new java.util.Scanner (System.in);
val s = sc.next();
val n = sc.nextLong();

if (n <= s.length) {
println(s.take(n.toInt).count(c => c == 'a'))
}
else {
val noOfA = s.count(c => c == 'a')
val k = n % s.length
val x = n/s.length
val ans = noOfA * x + s.take(k.toInt).count(c => c == 'a')
println(ans)
}
}
}```

Repeated String Solution in Pascal

```(* Enter your code here. Read input from STDIN. Print output to STDOUT *)
uses math;
var  n,m,ans:int64;
i:longint;
s:ansistring;
begin
m:=length(s);
for i:=1 to length(s) do
if (s[i]='a') then inc(ans);
ans:=(n div m)*ans;
for i:=1 to n mod m do
if s[i]='a' then inc(ans);
writeln(ans);
end.
```

Disclaimer: This problem (Repeated String) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.