Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 25, 2016 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/43f23e82ee04076c7496 to your computer and use it in GitHub Desktop.
Save jianminchen/43f23e82ee04076c7496 to your computer and use it in GitHub Desktop.
Two string - C++ - code is very readable -
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int MAXN = 128;
int count1[MAXN], count2[MAXN];
string s1, s2;
void getCount(string s, int arr[]) {
for (int i = 0; i < s.length(); i++) {
arr[s[i]]++;
}
}
bool hasCommon() {
for (int i = 'a'; i <= 'z'; i++) {
if (count1[i] && count2[i]) {
return true;
}
}
return false;
}
int main() {
int nCases;
cin >> nCases;
while (nCases--) {
memset(count1, 0, sizeof(count1));
memset(count2, 0, sizeof(count2));
cin >> s1 >> s2;
getCount(s1, count1);
getCount(s2, count2);
if (hasCommon()) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment