DEV Community

Minh Hieu
Minh Hieu

Posted on

2 2

Repeated String Explain - Solution | Javascript

function repeatedString(s, n) {
  const regex = new RegExp(/a/gi);
  const match = s.match(regex) || [];
  const length = match.length;
  if (length) {
    const ratio = Math.floor(n / s.length);
    const remain = n - (ratio * s.length);
    let x = 0;
    for(let i = 0; i < remain; i++) {
      if (s[i] === 'a') x++;
    }
    return (ratio * length) + x;
  }
  return 0;
}


repeatedString('aba', 10);

Problem

Top comments (0)

AWS Industries LIVE! Stream

Watch AWS Industries LIVE!

New tech. Real solutions. See what’s possible on Industries LIVE! with AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay