A Needle in the Haystack
Practice
3.7 (48 votes)
Ad Hoc
Algorithms
Approved
Hash maps
Medium
Open
Problem
35% Success 15774 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Our hacker, Little Stuart lately has been fascinated by ancient puzzles. One day going through some really old books he finds something scribbled on the corner of a page. Now Little Stuart believes that the scribbled text is more mysterious than it originally looks, so he decides to find every occurrence of all the permutations of the scribbled text in the entire book. Since this is a huge task, Little Stuart needs your help, he needs you to only figure out if any permutation of the scribbled text exists in the given text string, so he can save time and analyze only those text strings where a valid permutation is present.

Input:
First line contains number of test cases T.Each test case contains two lines ,first line contains pattern and next line contains a text string. All characters in both the strings are in lowercase only [a-z].

Output:
For each test case print "YES" or "NO" (quotes for clarity) depending on whether any permutation of the pattern exists in the text string.

Constraints:
1 ≤ T ≤ 100
1 ≤ |Pattern| ≤ 1000
1 ≤ |Text String| ≤ 100000

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
17 votes
Tags:
SortingHashmapImplementationHash TablesData StructuresBasics of Hash TablesHashingMathObservation
Points:30
19 votes
Tags:
Data StructuresHash MapsHash TablesMedium
Points:30
11 votes
Tags:
AlgorithmsApprovedMathMediumOpen