Alien language

Little Jhool considers Jaadu to be a very close friend of his. But, he ends up having some misunderstanding with him a lot of times, because Jaadu's English isn't perfect, and Little Jhool sucks at the language Jaadu speaks. So, he's in a fix - since he knows that Jaadu has got magical powers, he asks him to help so as to clear all the issues they end up having with their friendship.

Now, Jaadu can only focus on one task, so to fix these language issues he comes up with a magical way out, but someone needs to do the rest of it; this is where Little Jhool has asked for your help.

Little Jhool says a word, and then Jaadu says another word. If any sub-string of the word said by Jaadu is a sub-string of the word said by Little Jhool, the output should be "YES", else "NO". (Without the quotes.)

Example:

Input: s = "hackerearth", p = "hacker"
Output: YES

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void alienLanguage(string sstring p)
{
    int f[26] = {0}, f1[26] = {0};
    int n = s.size(), m = p.size();
    for (int i = 0i < ni++)
        f[s[i] - 'a']++;
    for (int i = 0i < mi++)
        f1[p[i] - 'a']++;
    int flag = 0;
    for (int i = 0i < 26i++)
    {
        if (f[i] > 0 && f1[i] > 0)
        {
            flag = 1;
        }
    }
    if (flag == 0)
        cout << "NO\n";
    else
        cout << "YES\n";
}
int main()
{

    string s = "hackerearth"p = "hacker";

    alienLanguage(sp);

    return 0;
}


No comments:

Post a Comment