title: Number Subsequences Ab String Repeated K Times
signature: func numberSubsequencesAbStringRepeatedKTimes(s string, k int) float64 {
test_code: package main
  
  import (
      "fmt"
      "reflect"
      "math"
      "sort"
  )
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  func validateSolution(actual float64, expected float64) bool {
      return math.Abs(actual - expected) < 1e-06
  }
  
  func driver(s string, k int, expected float64) string {
      if validateSolution(PLACEHOLDER_FN_NAME(s, k),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver("abbc", 
  	96, 9312.0)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver("abahk", 
  	7, 49.0)
      fmt.Printf("TEST-1...%s\n", result)
      
      result = driver("hugbabab", 
  	5, 75.0)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver("abadbc", 
  	60, 7260.0)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver("nkg9", 
  	8, 0.0)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver("jh7dab", 
  	41, 861.0)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver("abd", 
  	87, 3828.0)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver("aabb8yk", 
  	4, 40.0)
      fmt.Printf("TEST-7...%s\n", result)
      
      result = driver("1111", 
  	18, 0.0)
      fmt.Printf("TEST-8...%s\n", result)
      
      result = driver("PFXAhr", 
  	8, 0.0)
      fmt.Printf("TEST-9...%s\n", 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;
  }