• + 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;
    }