From 44bee6e8f3e074cb25aadefde4aa4642f1ba72fc Mon Sep 17 00:00:00 2001 From: ayham Date: Thu, 13 May 2021 12:13:46 +0300 Subject: [PATCH] solved hackerrank.two.strings Signed-off-by: ayham --- hackerrank.two.strings.cpp | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 hackerrank.two.strings.cpp diff --git a/hackerrank.two.strings.cpp b/hackerrank.two.strings.cpp new file mode 100644 index 0000000..b9d962b --- /dev/null +++ b/hackerrank.two.strings.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; + +int main() { + int p; + string a, b; + cin >> p; + while (p--) { + bool has_substring = false; + map repeats; + cin >> a >> b; + sort(a.begin(), a.end()); + sort(b.begin(), b.end()); + auto a_last = unique(a.begin(), a.end()); + a.resize(distance(a.begin(), a_last)); + auto b_last = unique(b.begin(), b.end()); + b.resize(distance(b.begin(), b_last)); + + for (auto i : a) { + if (repeats.find(i) == repeats.end()) repeats[i] = 1; + else repeats[i]++; + } + + for (auto i : b) { + if (repeats.find(i) == repeats.end()) repeats[i] = 1; + else repeats[i]++; + } + + for (auto it : repeats) { + if (it.second > 1) { + has_substring = true; + break; + } + } + + cout << ((has_substring) ? "YES" : "NO") << endl; + } + return 0; +}