/*
Uprość poniższy kod, korzystając z algorytmów biblioteki standardowej.
*/

#include <algorithm>
#include <cassert>
#include <optional>
#include <string>

#if 1
std::optional<std::string> bar(const std::string& input) {
  bool all_lower = true;
  for(int i = 0; i < input.size(); ++i) {
    if (isupper(input[i])) {
      all_lower = false;
    }
  }
  if (!all_lower) {
    return std::nullopt;
  }
  const std::vector<char> vowels = {'a', 'e', 'i', 'o', 'u', 'y'};
  int num_of_vowels = 0;
  for (int i = 0; i < input.size(); ++i) {
    for(auto v: vowels) {
      if (input[i] == v) {
        num_of_vowels++;
        break;
      }
    }
  }
  std::string output;
  const auto size = input.size();
  for (int i = 1; i <= num_of_vowels; ++i) {
     output.push_back(input[size-i]);
  }
  return output;
}

#else

/* Rozwiązanie: */

std::optional<std::string> bar(const std::string& input) {
  if (std::any_of(input.begin(), input.end(), [](auto ch) { return isupper(ch); })) {
    return std::nullopt;
  }
  const std::array<char, 6> vowels = {'a', 'e', 'i', 'o', 'u', 'y'};
  const int num_of_vowels = std::count_if(input.begin(), input.end(), [&] (auto ch) {
    //return std::binary_search(vowels.begin(), vowels.end(), ch);
    return std::find(vowels.begin(), vowels.end(), ch) != vowels.end();
  });

  std::string output;
  std::copy_n(input.rbegin(), num_of_vowels, std::back_inserter(output));
  return output;
}
#endif

int main() {
  auto result = bar("abcdefghij");
  assert(*result == "jih");
  result = bar("Abcdef");
  assert(!result.has_value());
}
