Sort by

recency

|

3730 Discussions

|

  • + 0 comments

    Here is my Java 7 solution:

        // To count a's in string s
        int aCount = 0;
        // To count a's in infinite string
        long Totalcount = 0;
    
        // Counting a's in string s
        for (int i=0; i<s.length(); i++) {
            char letter = s.charAt(i);
            if (letter == 'a') { aCount++; }
        }
    
        // Counting a's in infinite string
    
        // Check whether n is a multiple of s.length()
        if (n % s.length() == 0) {
            Totalcount = (n / s.length()) * aCount;
        } 
        else {
            // Calculate the nearest multiple of s.length() to n
            long nearestMultiple = n - (n%s.length());
            // Add a's count up to nearest multiple
            Totalcount = (nearestMultiple / s.length()) * aCount;
    
            // Add the remaining a's count in the remaining string
            for (int i=0; i<n%s.length(); i++) {
                char letter = s.charAt(i);
                if (letter == 'a') { Totalcount++; }
            }
        }
    
        return Totalcount;
    
  • + 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")