• + 0 comments

    JavaScript Solution

    /*
     * Complete the 'repeatedString' function below.
     *
     * The function is expected to return a LONG_INTEGER.
     * The function accepts following parameters:
     *  1. STRING s
     *  2. LONG_INTEGER n
     */
    
    function repeatedString(s, n) {
        let countInOriginal = 0; 
        
        for(let c of s) {
            countInOriginal += c === 'a' ? 1 : 0;
        } 
        
        
        let fullRepeats = Math.floor(n / s.length); 
        let count = fullRepeats * countInOriginal; 
        
        let remaining = n % s.length; 
        
        for(let i = 0; i < remaining; i++) {
            count += s[i] === 'a' ? 1 : 0; 
        } 
        
        return count; 
    }