---
# System prepended metadata

title: 'Consecutive strings [6 kyu]'
tags: [C++, CodeWars]

---

# Consecutive strings [6 kyu]

[Consecutive strings](https://www.codewars.com/kata/56a5d994ac971f1ac500003e)
6 kyu

## Solution
```cpp=
/**
*** Author: R-CO
*** E-Mail: daniel1820kobe@gmail.com
*** Date: 2020-08-06
**/
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
#include <string>
#include <vector>

struct TestCaseStruct {
  struct Input {
    std::vector<std::string> strarr;
    int k;
  } input;
  std::string expected_output;
};

class LongestConsec {
 public:
  static std::string longestConsec(std::vector<std::string> &strarr, int k) {
    const auto n = strarr.size();
    if (n == 0 || n < k || k <= 0) {
      return "";
    }

    std::string output;
    size_t head = 0;
    size_t max_sum = 0;
    size_t current_sum = 0;

    for (auto i = 0; i < k; ++i) {
      current_sum += strarr[i].size();
    }
    max_sum = current_sum;

    const size_t kMaxHead = n - k;
    for (auto i = 1; i <= kMaxHead; ++i) {
      current_sum =
          current_sum - strarr[i - 1].size() + strarr[i - 1 + k].size();
      if (current_sum > max_sum) {
        max_sum = current_sum;
        head = i;
      }
    }

    const size_t kTail = head - 1 + k;
    for (auto i = head; i <= kTail; ++i) {
      output += strarr[i];
    }

    return output;
  }
};

int main(int argc, char *argv[]) {
  /*
    std::vector<std::string> arr = {"zone", "abigail", "theta", "form", "libe",
    "zas", "theta", "abigail"}; dotest(arr, 2, "abigailtheta"); arr =
    {"ejjjjmmtthh", "zxxuueeg", "aanlljrrrxx", "dqqqaaabbb",
    "oocccffuucccjjjkkkjyyyeehh"}; dotest(arr, 1, "oocccffuucccjjjkkkjyyyeehh");
   */
  TestCaseStruct test_cases[] = {
      {{{"zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"},
        2},
       "abigailtheta"},
      {{{"ejjjjmmtthh", "zxxuueeg", "aanlljrrrxx", "dqqqaaabbb",
         "oocccffuucccjjjkkkjyyyeehh"},
        1},
       "oocccffuucccjjjkkkjyyyeehh"}};

  LongestConsec solution;
  for (auto &test_case : test_cases) {
    if (solution.longestConsec(test_case.input.strarr, test_case.input.k) ==
        test_case.expected_output) {
      cout << "test case \"" << test_case.expected_output << "\" is pass."
           << std::endl;
    } else {
      cout << "test case \"" << test_case.expected_output << "\" is fail."
           << std::endl;
    }
  }

  return EXIT_SUCCESS;
}

```

## Result

![](https://i.imgur.com/iSg4WoB.png)


###### tags: `CodeWars` `C++`
