[Interview Preparation Kit] Warm-up-4

이희진·2022년 11월 23일
0

Problem

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 ='abcac'
n = 10
The substring we consider is abcacabcac, the first 10 characters of the infinite string. There are 4 occurrences of a in the substring.

추가설명
문자열 s와 자연수 n이 주어졌을 때, n만큼 길이의 문자열을 문자열 s를 이어붙여서 만든다. 이때 a의 개수를 구하라.

My solution

#!/bin/python3

import math
import os
import random
import re
import sys

def repeatedString(s, n):
    x = s.count('a')
    sub = n%len(s)
    if sub > 0:
        xx = s[:sub].count('a')
    else:
        xx = 0
    count_a = (n // len(s))*x + xx
    return count_a 

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    n = int(input().strip())

    result = repeatedString(s, n)

    fptr.write(str(result) + '\n')

    fptr.close()

0개의 댓글