title: Number Subsequences Ab String Repeated K Times
signature: function number_subsequences_ab_string_repeated_k_times(s, k)
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;
  }
question_info.test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return math.abs(actual - expected) < 1e-06
  end
  
  function driver(s, k, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(s, k) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver("abbc", 96, 9312.0)
      print(string.format("TEST-0...%s",result))
  
      result = driver("abahk", 7, 49.0)
      print(string.format("TEST-1...%s",result))
  
      result = driver("hugbabab", 5, 75.0)
      print(string.format("TEST-2...%s",result))
  
      result = driver("abadbc", 60, 7260.0)
      print(string.format("TEST-3...%s",result))
  
      result = driver("nkg9", 8, 0.0)
      print(string.format("TEST-4...%s",result))
  
      result = driver("jh7dab", 41, 861.0)
      print(string.format("TEST-5...%s",result))
  
      result = driver("abd", 87, 3828.0)
      print(string.format("TEST-6...%s",result))
  
      result = driver("aabb8yk", 4, 40.0)
      print(string.format("TEST-7...%s",result))
  
      result = driver("1111", 18, 0.0)
      print(string.format("TEST-8...%s",result))
  
      result = driver("PFXAhr", 8, 0.0)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 9312.0, "inputs": {"s": "abbc", "k": 96}}, {"idx": 1, "outputs": 49.0, "inputs": {"s": "abahk", "k": 7}}, {"idx": 2, "outputs": 75.0, "inputs": {"s": "hugbabab", "k": 5}}, {"idx": 3, "outputs": 7260.0, "inputs": {"s": "abadbc", "k": 60}}, {"idx": 4, "outputs": 0.0, "inputs": {"s": "nkg9", "k": 8}}, {"idx": 5, "outputs": 861.0, "inputs": {"s": "jh7dab", "k": 41}}, {"idx": 6, "outputs": 3828.0, "inputs": {"s": "abd", "k": 87}}, {"idx": 7, "outputs": 40.0, "inputs": {"s": "aabb8yk", "k": 4}}, {"idx": 8, "outputs": 0.0, "inputs": {"s": "1111", "k": 18}}, {"idx": 9, "outputs": 0.0, "inputs": {"s": "PFXAhr", "k": 8}}]
question_info.extension: lua
question_info.entry_fn_name: number_subsequences_ab_string_repeated_k_times
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: lua __FILENAME__