We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defrepeatedString(s,n):# Return 0 if there is not any "a" on the stringif"a"notins:return0# If the string is just an "a", return n itselfelif(len(s)==1):returnn# If there is an "a" in the string and is greater than 1else:# If "n" is a multiple of "len(s)"ifn%len(s)==0:# Multiply the number of a's in the original string by the# dvision of "n" by the length of "s"returns.count("a")*(n//len(s))# If "n" is not a multiple of "len(s)",else:# Multiply the number of a's in the original string by the# dvision of "n" by the length of "s", then you can sum it how many# a's there are in the rest of the imaginary stringreturn(s.count("a")*(n//len(s))) + (s[:n % len(s)].count("a"))if__name__=='__main__':result=repeatedString(input(),int(input()))print(result)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Repeated String
You are viewing a single comment's thread. Return to all comments →
PYTHON