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.
functionrepeatedString(s,n){// Write your code hereconstsLength=s.length;constaCountInS=s.split('a').length-1;// Count occurrences of 'a' in s// Calculate the number of full repetitions of s in the first n charactersconstfullRepetitions=Math.floor(n/sLength);// Calculate the number of 'a' in the full repetitionsconstaCountInFullRepetitions=aCountInS*fullRepetitions;// Calculate the remaining characters to considerconstremaining=n%sLength;// Count the occurrences of 'a' in the remaining part of the stringletaCountInRemainder=0;for(leti=0;i<remaining;i++){if(s[i]==='a'){aCountInRemainder++;}}returnaCountInFullRepetitions+aCountInRemainder;}
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 →
JavaScript