fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. bool haveCommonSubstring(const std::vector<std::string>& a, const std::vector<std::string>& b) {
  6. for (size_t i = 0; i < a.size(); i++) {
  7. const std::string& substringA = a[i];
  8. const std::string& substringB = b[i];
  9. bool foundCommon = false;
  10.  
  11. for (char c : substringA) {
  12. if (substringB.find(c) != std::string::npos) {
  13. foundCommon = true;
  14. break;
  15. }
  16. }
  17.  
  18. if (!foundCommon) {
  19. return false;
  20. }
  21. }
  22.  
  23. return true;
  24. }
  25.  
  26. int main() {
  27. std::vector<std::string> a = {"ab", "cd", "ef"};
  28. std::vector<std::string> b = {"af", "ee", "ef"};
  29.  
  30. std::cout << std::boolalpha << haveCommonSubstring(a, b) << std::endl; // Output: true
  31.  
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
false