-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathrepeated_string.py
More file actions
36 lines (30 loc) · 806 Bytes
/
repeated_string.py
File metadata and controls
36 lines (30 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/python3
"""
https://www.hackerrank.com/challenges/repeated-string/problem
"""
import math
import os
import random
import re
import sys
def repeatedString(s, n):
word_length = len(s)
a_count_in_word = 0
for char in s:
if char == 'a':
a_count_in_word += 1
# To reach the length of n, the word s at least needs to
# repeat (n // word_length) times.
repeated_times = n // word_length
total_a = repeated_times * a_count_in_word
for char in s[:n - (repeated_times * word_length)]:
if char == 'a':
total_a += 1
return total_a
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
s = input()
n = int(input())
result = repeatedString(s, n)
fptr.write(str(result) + '\n')
fptr.close()