title: Number Subsequences Ab String Repeated K Times
signature: number_subsequences_ab_string_repeated_k_times <- function(s, k) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      abs(actual - expected) < 1e-06
  }
  
  driver <- function(s, k, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(s, k), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          "abbc", 96L, 
          9312.0
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          "abahk", 7L, 
          49.0
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          "hugbabab", 5L, 
          75.0
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          "abadbc", 60L, 
          7260.0
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          "nkg9", 8L, 
          0.0
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          "jh7dab", 41L, 
          861.0
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          "abd", 87L, 
          3828.0
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          "aabb8yk", 4L, 
          40.0
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          "1111", 18L, 
          0.0
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          "PFXAhr", 8L, 
          0.0
      )))
  
  }
  
  main()
source_py: def number_subsequences_ab_string_repeated_k_times(s, K):
      n = len(s)
      c1 = 0
      c2 = 0
      C = 0
      for i in range(n):
          if (s[i] == 'a'):
              c1 += 1
          if (s[i] == 'b'):
              c2 += 1
              C += c1
      return ((C * K) + ((((K * (K - 1)) / 2) * c1) * c2))
source_cpp: int number_subsequences_ab_string_repeated_k_times(string s, int K) {
    int n = s . length ( );
    int C, c1 = 0, c2 = 0;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( s [ i ] == 'a' ) c1 ++;
      if ( s [ i ] == 'b' ) {
        c2 ++;
        C += c1;
      }
    }
    return C * K + ( K * ( K - 1 ) / 2 ) * c1 * c2;
  }