Sort by

recency

|

3729 Discussions

|

  • + 0 comments

    Solution in TypeScript:

    function repeatedString(s: string, n: number): number {
        const sLen = s.length;
        // use regex to calculate the count of a's in s
        const aCount = (s.match(/a/g) || []).length;
        // how many times is the lenght of s in n
        const repCount = Math.trunc(n / sLen);
        // how many characters needs to be added so the repeated string's length matches n
        const remainder = n % sLen;
        // get a substring with a length equal to the remainder number
        const remainderSubstring = s.substring(0, remainder);
        // how many a's are present in the substring
        const aCountInSubstring =  (remainderSubstring.match(/a/g) || []).length;
        // result is the count of a's in the times needed to be repeated + the ones in the remainder substring
        return repCount * aCount + aCountInSubstring;
    }
    
  • + 0 comments

    Easy python sol:

    def repeatedString(s, n):
        c = s.count("a")
        l = len(s)
        return c*(n//l) + s[:n%l].count("a")
    
  • + 0 comments

    Python solution

    def repeatedString(s, n):
        # Write your code here
        number_of_times_to_multiply = n // len(s)
    
        a_count = sum([1 if i == 'a' else 0 for i in s])
    
        res = a_count * number_of_times_to_multiply
    
        diff = n - (len(s) * number_of_times_to_multiply)
    
        new_s = s[:diff]
    
        a_count = sum([1 if i == 'a' else 0 for i in new_s])
        
        res = res + a_count
        
        return res
    
  • + 0 comments
    def repeated_string(s, n):
        size = len(s)
        return n // size * list(s).count("a") + list(s)[:n % size].count("a")
    
  • + 0 comments

    Shortes Python Solution without the use of loops and I would like to share this credit with victor_escoto

    def repeatedString(s, n):
        if len(s) == 1 and s == 'a':
            return n
        c = s.count('a')
        c *= n//len(s)
        c += s[:n%len(s)].count('a')
        return c