HackerRank Repeated String problem solution

In this HackerRank Repeated String interview preparation kit problem you have Given an integer, n, find and print the number of letter a's in the first n letters of the infinite string.

Problem solution in Python programming.

```#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the repeatedString function below.
def repeatedString(s, n):
x,y = divmod(n,len(s))
return s[:y].count("a")*(x+1) + s[y:].count("a")*x

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

s = input()

n = int(input())

result = repeatedString(s, n)

fptr.write(str(result) + '\n')

fptr.close()```

Problem solution in Java Programming.

```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 numberOfReps = n/s.length();
long remainder = n%s.length();
long total = 0;
for(int a = 0; a < s.length(); a++){
if(s.charAt(a) == 'a'){
total++;
}
}
total = total * numberOfReps;
for(int a = 0; a < remainder; a++){
if(s.charAt(a) == 'a'){
total++;
}
}
System.out.println(total);
}
}```

Problem solution in C++ programming.

```#include <iostream>
#include <vector>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cstdio>
#include <queue>
#include <set>
#include <map>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <numeric>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout << fixed;
#ifdef LOCAL_DEFINE
freopen("input.txt", "rt", stdin);
#endif

string s;
i64 n;
cin >> s >> n;
i64 ans = 0;
forn(i, s.size()) if (s[i] == 'a') ans += max(0LL, n - i - 1 + (int)s.size()) / s.size();
cout << ans << '\n';

#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}```

Problem solution in C programming.

```#include <stdio.h>
#include <string.h>

int main( void ) {
char s[1000];
long long n;

scanf("%s %lld", s, &n);

int m = strlen(s);

long long count = 0;
for( int i = 0; i < m; i++ ) {
if( s[i] == 'a' ) count++;
}

count *= n / m;

for( int i = 0; i < n % m; i++ ) {
if( s[i] == 'a' ) count++;
}

printf("%lld\n", count);

return 0;
}```

Problem solution in JavaScript programming.

```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);

}```