fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool haveCommonLetters(string& str1,string& str2) {
  5. unordered_set<char> charSet;
  6. for (char c : str1) {
  7. charSet.insert(c);
  8. }
  9.  
  10. for (char c : str2) {
  11. if (charSet.find(c) != charSet.end()) {
  12. return true;
  13. }
  14. }
  15.  
  16. return false;
  17. }
  18.  
  19.  
  20. int main() {
  21. // your code goes here
  22. vector<string>a={"hello","hi"};
  23. vector<string>b={"world","bye"};
  24.  
  25. for(int i=0;i<a.size();i++){
  26. if(haveCommonLetters(a[i], b[i])){
  27. cout<<"YES\n";
  28. }
  29. else{
  30. cout<<"NO\n";
  31. }
  32. }
  33. return 0;
  34. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
YES
NO