Xsquare loves to play with strings a lot. Today, he has two strings S1 and S2 both consisting of lower case alphabets. Xsquare listed all subsequences of string S1 on a paper and all subsequences of string S2 on a separate paper. Xsquare wants to know whether there exists a string that is listed on both papers.
Xsquare thinks that this task is pretty boring and handed it to you. Please accomplish this task on his behalf.
Example:
Input: s1 = "phackerekarthj", s2 = "jhakckerearthp"
Output: Yes
Approach
C++
#include <bits/stdc++.h>using namespace std;void xsquareAndTwoStrings(string s1, string s2){int n = s1.size();int m = s2.size();map<char, int> mp;for (int i = 0; i < n; i++)mp[s1[i]]++;int flag = 0;for (int i = 0; i < m; i++){if (mp[s2[i]]){flag = 1;break;}}if (flag == 0)cout << "No\n";elsecout << "Yes\n";}int main(){string s1 = "phackerekarthj";string s2 = "jhakckerearthp";xsquareAndTwoStrings(s1, s2);return 0;}
No comments:
Post a Comment