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.
// Complete the repeatedString function below.
//Consider input is aba 5: -->abaab-->expected op-->3
// first 3 characters is aba. According to 5, we can add only 2 char. So if we can count no.of a in initial array then can multiply with quo and add rem.-->2(length)*1(quo)+2(rem) there are 2 a in aba and rem 1(ab) . In rem only 1 a. totally 2+1=3
// for input aba 10: abaabaabaa, totally 10. 3*3 +1 rem . First aba there are 2 a. then 2*3(quo)=6 then only one a in rem(a) totally 7
static long repeatedString(String s, long n) {
char[] chrIn = s.toCharArray();
int i = 0;
long max = 0;
long quo = n / chrIn.length; // Needed to multiple with count of a
long rem = n % chrIn.length;// Needed to add with count of a
while (i < chrIn.length) {//will iterate it till inial array then we can multiply it with quo
if (chrIn[i] == 'a') {
max++;
}
i++;
}//This while loop returns 2 as aba has 2 'a'
i = 0;
max = max * quo;//2*1=2
while (i < rem) {// here the reminder is 2 so it will iterate it with 0,1
if (chrIn[i] == 'a') {
max++;
}
i++;
}
return max;
}
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 →