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:string,n:number):number{constsLen=s.length;// use regex to calculate the count of a's in sconstaCount=(s.match(/a/g)||[]).length;// how many times is the lenght of s in nconstrepCount=Math.trunc(n/sLen);// how many characters needs to be added so the repeated string's length matches nconstremainder=n%sLen;// get a substring with a length equal to the remainder numberconstremainderSubstring=s.substring(0,remainder);// how many a's are present in the substringconstaCountInSubstring=(remainderSubstring.match(/a/g)||[]).length;// result is the count of a's in the times needed to be repeated + the ones in the remainder substringreturnrepCount*aCount+aCountInSubstring;}
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 →
Solution in TypeScript: