title: Number Subsequences Ab String Repeated K Times
signature: float numberSubsequencesAbStringRepeatedKTimes(string s, int k) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(float actual, float expected){
      return abs(actual - expected) < 1e-06;
  }
  
  string driver(string s, int k, float expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(s, k), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver("abbc", 96, 9312.0); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver("abahk", 7, 49.0); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver("hugbabab", 5, 75.0); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver("abadbc", 60, 7260.0); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver("nkg9", 8, 0.0); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver("jh7dab", 41, 861.0); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver("abd", 87, 3828.0); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver("aabb8yk", 4, 40.0); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver("1111", 18, 0.0); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver("PFXAhr", 8, 0.0); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
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;
  }