Xsquare And Two Strings

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 s1string s2)
{
    int n = s1.size();
    int m = s2.size();
    map<charintmp;
    for (int i = 0i < ni++)
        mp[s1[i]]++;
    int flag = 0;
    for (int i = 0i < mi++)
    {
        if (mp[s2[i]])
        {
            flag = 1;
            break;
        }
    }
    if (flag == 0)
        cout << "No\n";
    else
        cout << "Yes\n";
}
int main()
{

    string s1 = "phackerekarthj";
    string s2 = "jhakckerearthp";

    xsquareAndTwoStrings(s1s2);

    return 0;
}


No comments:

Post a Comment