[해커랭크] Repeated String
https://www.hackerrank.com/challenges/repeated-string/problem?isFullScreen=true
Repeated String | HackerRank
Find and print the number of letter a's in the first n letters of an infinitely large periodic string.
www.hackerrank.com
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 substring
Input Format
The first line contains a single string, .
The second line contains an integer, .
Constraints
- For of the test cases, .
Sample Input
Sample Input 0
aba
10
Sample Output 0
7
Explanation 0
The 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 1
Because all of the first letters of the infinite string are a, we return .
그냥 전체 문자열에서 'a' 개수 × 반복 횟수 + 나머지만 계산하면 됨
단순히 반복 구조 + 나머지 처리로 해결 가능해서 DP가 필요 없음

'알고리즘 단련장 > 해커랭크' 카테고리의 다른 글
| [해커랭크] Jumping on the Clouds (0) | 2025.12.04 |
|---|---|
| [해커랭크] Non-Divisible Subset (0) | 2025.12.04 |
| [해커랭크] Cut the sticks (0) | 2025.10.11 |
| [해커랭크] Library Fine (0) | 2025.10.11 |
| [해커랭크] Sherlock and Squares (0) | 2025.10.11 |