Repeated String HackerRank Solution in C, C++, Java, Python

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<=10^12
  • For 25% of the test cases,n<=10^6 .

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

 

Repeated String HackerRank Solution in C

#include 
#include 
#include 
#include 
#include 
#include 
#include 

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

 

Repeated String HackerRank Solution in C++

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;


int main(){
    string s;
    cin >> s;
    long long n;
    cin >> n;
    long long count=0;
    for (int i=0;i

 

Repeated String HackerRank 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 HackerRank Solution in Python

#!/bin/python

import sys


s = raw_input().strip()
n = long(raw_input().strip())

k = s.count("a")*(n/len(s))
k += s[:n%len(s)].count("a")
print k

 

Repeated String HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RepeatedString
{
    class Program
    {
        static void Main(String[] args)
        {
            string s = Console.ReadLine();
            long n = Convert.ToInt64(Console.ReadLine());

            var inSingle = s.Count(x => x == 'a');

            var full = n / s.Length;

            var rest = n % s.Length;

            var inRest = s.Substring(0, (int)rest).Count(x => x == 'a');

            var result = (inSingle * full) + inRest;

            Console.WriteLine(result);

        }
    }
}

 

Attempt Repeated String HackerRank Challenge 

Link – https://www.hackerrank.com/challenges/repeated-string/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/jumping-on-the-clouds-hackerrank-solution/

Leave a Comment