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):a_count=0string_size=len(s)ifstring_size>n:s=s[0:n]string_size=len(s)# Count how many "a" the string has while_count=0whilewhile_count<string_size:char_value=s[while_count]while_count+=1ifchar_value=="a":a_count+=1# Divide by how many times the string nned to repeat in order to have the "n" charactares# Mutliply by the qty of "a" we have in "a_count"ifstring_size<n:div_value=int(n/string_size)rest_value=n%string_sizea_count=a_count*div_value# This check how many "a" are left for the rest of string in the divisionifrest_value>0:rest_char_value=s[0:rest_value]rest_char_value_size=len(rest_char_value)while_count=0whilewhile_count<rest_char_value_size:char_value=s[while_count]while_count+=1ifchar_value=="a":a_count+=1else:rest_char_value=""returna_count
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 →