Repeated String (Easy)

9oya·2022년 5월 29일
0

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

s=abcacs = 'abcac'

n=10n = 10

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 substring

Input Format

The first line contains a single string, .The second line contains an integer, .

Constraints

  • 1<=s<=1001<=|s|<=100
  • 1<=n<=10121<=n<=10^{12}
  • For 25%  of the test cases, n<=106n<=10^6

Sample Input

Sample Input 0

aba
10

Sample Output 0

7

Explanation 0The first  letters of the infinite string are abaabaabaa. Because there are  a's, we return .

Sample Input 1

a
1000000000000

Sample Output 1

1000000000000

Explanation 1Because all of the first  letters of the infinite string are a, we return .


func repeatedString(s: String, n: Int) -> Int {
    let a = s.filter { $0 == Character("a") }.count
    var cnt = (n/s.count)*a
    for i in 0..<n%s.count {
        if s[s.index(s.startIndex, offsetBy: i)] == Character("a") {
            cnt += 1
        }
    }
    return cnt
}

0개의 댓글