Sort by

recency

|

3691 Discussions

|

  • + 0 comments

    JS solution :

    function repeatedString(s, n) {
        return ([...s.matchAll(/a/g)].length * Math.floor(n / s.length)) + ([...s.substring(0, (n % s.length)).matchAll(/a/g)].length);
    }
    
  • + 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

    Java 15

    class Result {
    
        private static final IntPredicate isA = c -> c == 'a';
    
        public static long repeatedString(String s, long n) {
          long times = n / s.length();
          long remainingChars = n % s.length();
          long count = s.chars().filter(isA).count();
          count*=times;  
          count += s.substring(0, (int) remainingChars).chars().filter(isA).count();  
          return count;
        }
    
    }
    
  • + 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;
    }
    
  • + 0 comments

    Using JS

    function repeatedString(s, n) {
        if(s.length == 1){
            return n;
        }else{
            
            let count = 0;
            while(s.length < n){
                s = s + s;
            }
            let newString = s.substr(0, n);
            
            for (let partString of newString) {
                if (partString === 'a') {
                    count++;
                }
            }
    
            return count;
            }
       
    }