Skip to main content

Repeated string Hackerrank

Repeated String solution in c.


Lilah has a string ,s , of lowercase English letters that she repeated infinitely many times.
Given an integer,, find and print the number of letter a's in the first  letters of Lilah's infinite string.

For example, if the string s='abcac'  and n=10 , the substring we consider is abcacabcac, the first 10 characters of her infinite string. There are 4  occurrences of a in the substring.
Function Description
Complete the repeatedString function in the editor below. It should return an integer representing the number of occurrences of a in the prefix of length  in the infinitely repeating string.
repeatedString has the following parameter(s):
  • s: a string to repeat
  • n: the number of characters to consider
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,≤ 10^6 .
Output Format
Print a single integer denoting the number of letter a's in the first  letters of the infinite string created by repeating  infinitely many times.
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 print on a new line.
Sample Input 1
a
1000000000000
Sample Output 1
1000000000000
Explanation 1
Because all of the first n=100000000000 letters of the infinite string are a, we print 100000000000 on a new line.

Solutions is below:

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.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= count*(n/m);
       
    for( int i = 0; i < n % m; i++ ) {
        if( s[i] == 'a' ) count++;
    }
    
    printf("%lld\n", count);
    
    return 0;
}

Comments

Popular posts from this blog

Day 2: Conditional Statements: Switch

  Objective In this challenge, we learn about  switch statements . Check out the attached tutorial for more details. Task Complete the  getLetter(s)  function in the editor. It has one parameter: a string,  , consisting of lowercase English alphabetic letters (i.e.,  a  through  z ). It must return  A ,  B ,  C , or  D  depending on the following criteria: If the first character in string   is in the set  , then return  A . If the first character in string   is in the set  , then return  B . If the first character in string   is in the set  , then return  C . If the first character in string   is in the set  , then return  D . Hint:  You can get the letter at some index   in   using the syntax  s[i]  or  s.charAt(i) . Input Format Stub code in the editor reads a single string denoting   from ...

Day 2: Conditional Statements: If-Else

Day 2: Conditional Statements: If-Else || Hackerrank Solution Objective In this challenge, we learn about  if-else  statements. Check out the attached tutorial for more details. Task Complete the  getGrade(score)  function in the editor. It has one parameter: an integer,  , denoting the number of points Julia earned on an exam. It must return the letter corresponding to her   according to the following rules: If  , then  . If  , then  . If  , then  . If  , then  . If  , then  . If  , then  .

Jumping on the Clouds Hackerrank

Jumping on the Clouds solutions in c. Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1  or 2  . She must avoid the thunderheads. Determine the minimum number of jumps it will take Emma to jump from her starting position to the last cloud. It is always possible to win the game. For each game, Emma will get an array of clouds numbered 0   if they are safe or 1 if they must be avoided. For example, c=[0,1,0,0,0,1,0] indexed from 0....6. The number on each cloud is its index in the list so she must avoid the clouds at indexes 1 and 5 . She could follow the following two paths:0 ➜ 2 ➜ 4➜6   or 0 ➜ 2 ➜ 3 ➜ 4 ➜ 6 . The first path takes 3   jumps while the second takes 4 . Function Description Complete the  jumpingOnClouds ...