title: Number Subsequences Ab String Repeated K Times
signature: class Solution {
  
      public float NumberSubsequencesAbStringRepeatedKTimes(string s, int k) {
test_code: using System;
  using System.Numerics;
  using System.Diagnostics;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Security.Cryptography;
  using System.Web.UI;
  using System.Web.Script.Serialization;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  namespace EvaluatePrediction {
      class TestRunner {
          public bool ValidateSolution(float actual, float expected){
              return Math.Abs(actual - expected) < 1e-06f;
          }
  
          public string driver(string s, int k, float expected){
              try{
                PLACEHOLDER_CLS_NAME solution_cls = new PLACEHOLDER_CLS_NAME();
                float pred_result = solution_cls.PLACEHOLDER_FN_NAME(s, k); 
                if (ValidateSolution(
                      pred_result,
                      expected)){
                  return "PASSED";
                }
                return "FAILED";
              } 
              catch (Exception exception_obj)
              {
                return exception_obj.GetType().FullName;
              }
          }
          static void Main(string[] args) {
              TestRunner runner = new TestRunner();
              String result;
              result = runner.driver(
                  "abbc", 96, 
                  9312.0f
              );
              Console.WriteLine(string.Format(
                  "TEST-0...{0}",
                  result)
              );result = runner.driver(
                  "abahk", 7, 
                  49.0f
              );
              Console.WriteLine(string.Format(
                  "TEST-1...{0}",
                  result)
              );result = runner.driver(
                  "hugbabab", 5, 
                  75.0f
              );
              Console.WriteLine(string.Format(
                  "TEST-2...{0}",
                  result)
              );result = runner.driver(
                  "abadbc", 60, 
                  7260.0f
              );
              Console.WriteLine(string.Format(
                  "TEST-3...{0}",
                  result)
              );result = runner.driver(
                  "nkg9", 8, 
                  0.0f
              );
              Console.WriteLine(string.Format(
                  "TEST-4...{0}",
                  result)
              );result = runner.driver(
                  "jh7dab", 41, 
                  861.0f
              );
              Console.WriteLine(string.Format(
                  "TEST-5...{0}",
                  result)
              );result = runner.driver(
                  "abd", 87, 
                  3828.0f
              );
              Console.WriteLine(string.Format(
                  "TEST-6...{0}",
                  result)
              );result = runner.driver(
                  "aabb8yk", 4, 
                  40.0f
              );
              Console.WriteLine(string.Format(
                  "TEST-7...{0}",
                  result)
              );result = runner.driver(
                  "1111", 18, 
                  0.0f
              );
              Console.WriteLine(string.Format(
                  "TEST-8...{0}",
                  result)
              );result = runner.driver(
                  "PFXAhr", 8, 
                  0.0f
              );
              Console.WriteLine(string.Format(
                  "TEST-9...{0}",
                  result)
              );
          }
      }
  }
  
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;
  }