• + 0 comments

    java 8

    public static long repeatedString(String s, long n) {
        // Write your code here
    
        long count = 0;
        long div = n / s.length();
        long rem = n % s.length();
    
        if (n > s.length()) {
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == 'a') {
                    count++;
                }
            }
            if (div > 0) {
                count *= div;
            }
            if (rem > 0) {
                for (int i = 0; i < rem; i++) {
                    if (s.charAt(i) == 'a') {
                        count++;
                    }
                }
            }
        } else {
            for (int i = 0; i < n; i++) {
                if (s.charAt(i) == 'a') {
                    count++;
                }
            }
        }
        return count;
    }