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):# Count 'a's in one full instance of string 's'countInOneInstance=s.count('a')# Calculate how many full instances of 's' can fit in 'n'countInFullInstance=countInOneInstance*(n// len(s))# Count 'a's in the remaining part of the string (if any)countInPartInstance=0numFirstCharsToCount=n%len(s)foriinrange(numFirstCharsToCount):ifs[i]=='a':countInPartInstance+=1returncountInFullInstance+countInPartInstance
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 solution: