Sort by

recency

|

3704 Discussions

|

  • + 0 comments

    public static long repeatedString(String s, long n) { // Write your code here long strLength = s.length();

    // Count occurrences of 'a' in the original string
    long countInOriginal = s.chars().filter(ch -> ch == 'a').count();
    
    // Calculate full repetitions of `s` in `n` characters
    long fullRepetitions = n / strLength;
    
    // Calculate remaining characters
    long remainder = n % strLength;
    
    // Count occurrences of 'a' in the remainder substring
    long countInRemainder = s.substring(0, (int) remainder).chars().filter(ch -> ch == 'a').count();
    
    // Total occurrences of 'a'
    return (fullRepetitions * countInOriginal) + countInRemainder;
    }
    
  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/Vh5davsSkfA

    long ccount(string s) {
        return count(s.begin(), s.end(), 'a');
    }
    
    long repeatedString(string s, long n) {
        return (n / s.size()) * ccount(s) + ccount(s.substr(0, n % s.size()));
    }
    
  • + 0 comments

    Python

    def repeatedString(string: str, substring_size: int)-> int:
        # This code can be simplified, but maintainability wins
        # How many characters are in the repeating string
        size: int = len(string)
        # If the number of times the string repeats has remainder, account it
        last_position: int = substring_size % size
        # Number of times the string repeats entirely
        full_repeats: int = substring_size // size
        
        return string.count('a') * full_repeats + string[0: last_position].count('a')
    
  • + 0 comments
    def repeatedString(s: str, n: int) -> int:
        return s.count('a') * (n // len(s)) + s[:n % len(s)].count('a')
    
  • + 0 comments

    Python solution

    def repeatedString(s, n):
        a_count = 0
        for c in s:
            if c=="a":
                a_count+=1
        
        repeated = a_count*(n//len(s))
        mod = n%len(s)
        for c in s[:mod]:
            if c=="a":
                repeated+=1
        return repeated