HackerRank Repeated String Problem Solution
In this post, we will solve HackerRank Repeated String Problem 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.
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.
Repeated String C Solution
#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;
scanf("%ld",&n);
unsigned long long int cnt = 0;
for( unsigned long long int i = 0; s[i] != '\0'; i++) {
if( s[i] == 'a') cnt++;
}
unsigned long long int cnt1 = 0;
for( unsigned long long int i = 0; i < n%strlen(s); i++) {
if( s[i] == 'a') cnt1++;
}
printf("%llu", cnt* (n/strlen(s)) + cnt1);
return 0;
}
Repeated String C++ Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
string s;
cin>>s;
long long n;
cin>>n;
int count=0;
for(int i=0;i<s.length();i++){
if(s[i]=='a'){
count++;
}
}
if(count==0){
cout<<'0'<<endl;
}
else if(n%s.length()==0){
cout<<(n/s.length())*count<<endl;
}
else{
long long temp=n/s.length()*count;
for(int i=0;i<n%s.length();i++){
if(s[i]=='a'){
temp++;
}
}
cout<<temp;
}
return 0;
}
Repeated String C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
string s = Console.ReadLine();
long n = Convert.ToInt64(Console.ReadLine());
long count = 0;
for (int i = 0; i < s.Length; i++)
if (s[i] == 'a') count++;
long amount = (n / s.Length) * count;
int rem = Convert.ToInt32(n % s.Length);
for (int i = 0; i < rem; i++)
if (s[i] == 'a') amount++;
Console.WriteLine(amount);
}
}
Repeated String Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'repeatedString' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts following parameters:
* 1. STRING s
* 2. LONG_INTEGER n
*/
public static long repeatedString(String s, long n) {
int[] moduloFrequency = new int[s.length()];
int count = 0;
for( int i = 0; i < s.length(); i++) {
if(s.charAt(i) == 'a') count++;
moduloFrequency[i] = count;
}
if(n <= s.length()) return moduloFrequency[(int)(n) - 1];
int modulo = (int) (n % s.length()) == 0 ? 0 : moduloFrequency[(int) (n % s.length()) - 1];
long howManyFullTimes = (n / s.length());
return howManyFullTimes * count + modulo;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String s = bufferedReader.readLine();
long n = Long.parseLong(bufferedReader.readLine().trim());
long result = Result.repeatedString(s, n);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Repeated String JavaScript Solution
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();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var s = readLine();
var n = parseInt(readLine());
var subStrLength = s.length;
fullStrCount = (s.match(/a/g) || []).length;
subStrCount = (s.substring(0, n%subStrLength).match(/a/g) || []).length;
//n%subStrLength
console.log((Math.floor(n/subStrLength) * fullStrCount) + subStrCount);
}
Repeated String Python Solution
#!/bin/python3
import sys
s = input().strip()
n = int(input().strip())
num_of_as = s.count('a')
total = 0
if len(s) <= n:
complete = n//len(s)
rest_of_s = s[:(n%len(s))]
total += ((num_of_as * complete) + rest_of_s.count('a'))
else:
total += s[:n].count('a')
print(total)
Other Solutions