Sort by

recency

|

3738 Discussions

|

  • + 0 comments

    Rust solution:

    fn repeated_string(s: &str, n: i64) -> i64 {
        let str_length = s.len();
        if str_length == 0 {
            return 0;
        }
        let a_count = s.bytes().filter(|&b| b == b'a').count();
    
        let modulo = n as usize % str_length;
        let remainder_count = s[..modulo].bytes().filter(|&b| b == b'a').count();
    
        let repeats = (n as usize) / str_length;
        (remainder_count + (a_count * repeats)) as i64
    }
    
  • + 0 comments

    Python3 Solution

    def repeatedString(s, n):
        fullCount = n // len(s)
        leftCount = n % len(s)
        aCount = sum(1 if char == "a" else 0 for char in s)
        
        res = aCount * fullCount
        
        for i in range(leftCount):
            if s[i] == "a":
                res += 1
            
        return res
    
  • + 0 comments

    One line Python solution:

    def repeatedString(s, n): return s.count('a') * (n // len(s)) + s[:n % len(s)].count('a')

  • + 0 comments

    Here is Repeated string solution in python, java, c++, c and javascript programming - https://programmingoneonone.com/hackerrank-repeated-string-problem-solution.html

  • + 0 comments
    if len(s) == 1:
        return n if s == 'a' else 0
    elif 'a' not in s:
        return 0
    else:
        a_in_s = s.count('a')
        repe = n//len(s)
        remain = n % len(s) 
    
        return a_in_s * repe + s[:remain].count('a')