DEV Community

Minh Hieu
Minh Hieu

Posted on

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)