HackerRank.com2021. 10. 4. 22:01

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

Example

The substring we consider is , the first  characters of the infinite string. There are  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 substringInput Format
  • Sample Input
  • Sample Input 0aba 10Sample Output 07Explanation 0
    The first  letters of the infinite string are abaabaabaa. Because there are  a's, we return .a 1000000000000Sample Output 11000000000000Explanation 1
    Because all of the first  letters of the infinite string are a, we return .
  • Sample Input 1
  • The first line contains a single string, .
    The second line contains an integer, 

 

-- Answer

    public static long repeatedString(string s, long n)

    {

        if(s == "a"return n;

        long result = 0;

        long length = s.Length;

        result = s.Count(f => (f == 'a'));        

        long repeat = n / length;

        result = result * repeat;

        long remain = n % length;

        for(int i=0; i <  remain; i++){

            if('a' == s[i]){

                result++;

            }

        }

        

       return result;  

    }

'HackerRank.com' 카테고리의 다른 글

Jumping on the Clouds  (0) 2021.10.04
Counting Valleys  (0) 2021.10.04
Sales by Match  (0) 2021.10.04
Posted by yuwol