Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.
Polycarp decided to store the hash of the password, generated by the following algorithm:
take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);
generate two random strings, consisting of lowercase Latin letters, s_1 and s_2 (any of these strings can be empty);
the resulting hash h = s_1 + p' + s_2, where addition is string concatenation.
For example, let the password p = "abacaba". Then p' can be equal to "aabcaab". Random strings s1 = "zyx" and s2 = "kjh". Then h = "zyxaabcaabkjh".
Note that no letters could be deleted or added to p to obtain p', only the order could be changed.
Now Polycarp asks you to help him to implement the password check module. Given the password p and the hash h, check that h can be the hash for the password p.
Your program should answer t independent test cases.
Input
The first line contains one integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a non-empty string p, consisting of lowercase Latin letters. The length of p does not exceed 100.
The second line of each test case contains a non-empty string h, consisting of lowercase Latin letters. The length of h does not exceed 100.
Output
For each test case print the answer to it — "YES" if the given hash h could be obtained from the given password p or "NO" otherwise.
Example
Input
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
Output
YES
YES
NO
YES
NO
Note
The first test case is explained in the statement.
In the second test case both s_1 and s_2 are empty and p'= "threetwoone" is p shuffled.
In the third test case the hash could not be obtained from the password.
In the fourth test case s_1= "n", s_2 is empty and p'= "one" is p shuffled (even thought it stayed the same).
In the fifth test case the hash could not be obtained from the password.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to read t test cases. For each, given p and h, decide if h can be a hash generated as:
p' is a permutation (i.e., multiset of characters same as p) of p.
s1 and s2 are arbitrary strings (possibly empty) of lowercase letters.
h = s1 + p' + s2.
So h must contain a contiguous substring that is a permutation of p, and the rest of h is arbitrary (prefix and suffix). We need to check if there exists a contiguous substring of h that, when considered, has exactly the same multiset of characters as p. That substring would be p', and the prefix and suffix would be s1 and s2.
Thus the problem reduces: given strings p and h, does there exist a substring of h of length |p| that is an anagram of p? Since h length up to 100, we can just check all possible substrings of length |p|. If any matches p in character counts, answer YES else NO.
But careful: p length could be greater than h length? In constraints: p length ≤ 100, h length ≤ 100. However, if |p| > |h|, then obviously NO. But also note p and h non-empty. So we need to handle that.
Hence for each test case:
Compute count array for p (size 26).
For i from 0 to len(h)-len(p) inclusive:
Compute count for substring h[i:i+len(p)], compare to p count.
If found, YES else NO.
That's O(t * |h| * 26) which is fine.
Edge cases:
The substring could be exactly at the beginning or end.
If len(p) > len(h), answer NO.
Check sample:
p="abacaba", len=7, h="zyxaabcaabkjh" length? It's 13? Actually "zyxaabcaabkjh" length: z y x a a b c a a b k j h = 13. We need substring of length 7 that is anagram. They gave "aabcaab" which is length 7? Actually "aabcaab" is 7 letters? a a b c a a b = 7. Yes it's present from index 3 to 9? "zyx aabcaab kjh". So it matches.
p="onetwothree", h="threetwoone". len(p)=11? onetwothree = 11? Actually "onetwothree" = o n e t w o t h r e e -> 11 letters? Wait count: o,n,e,t,w,o,t,h,r,e,e = 11. h="threetwoone" = t,h,r,e,e,t,w,o,o,n,e = 11. They said both s1,s2 empty and p' shuffled. So substring is entire h. That works.
p="one", h="zzonneyy". len(p)=3, h length 8? z z o n n e y y = 8. Check substrings of length 3: "zzo", "zon", "onn", "nne", "ney", "eyy"? none is anagram of "one"? "one" letters: o, n, e exactly one each. "zon": z,o,n - no e. "onn": o,n,n - duplicate n. "nne": n,n,e - missing o. "ney": n,e,y - no o. "eyy": e,y,y. So NO.
p="one", h="none". length p=3, h length=4. Substrings of length 3: "non", "one". "non" counts: n,o,n - not anagram of "one". "one": o,n,e - yes! So YES. s1 = "n", s2 empty. Good.
p="twenty", h="ten". length p=6, h length=3 -> NO.
All matches.
So solution straightforward.
But wait: There's nuance: p' is obtained by shuffling p, meaning any permutation, but it must be exactly the same multiset. So substring exactly length |p| and same character counts.
One thing: The strings s1 and s2 are random strings, could be anything. So the substring we find must be contiguous. That's it.
Thus algorithm as above.
Implementation in Python: read t, loop. For each, read p, h. If len(p) > len(h): print "NO". Else:
Compute p_count = [0]*26; for ch in p: increment.
For i in range(len(h)-len(p)+1):
temp = [0]*26
for j in range(len(p)): increment for h[i+j]
if temp == p_count: print "YES"; break
else: print "NO"
Edge Cases: might need to strip newline.
Complexities: O(10010026) trivial.
But there is also possibility of more efficient approach using sliding window: maintain counts for current window and update. That's also easy: start with initial window at i=0..len(p)-1, compute counts; then slide. But given constraints, not necessary.
Thus solution.
But wait: The problem statement says: "take the password p, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain p' (p' can still be equal to p);" So p' must be a permutation of p. That's fine.
Now let's verify the example output:
5 cases, matches.
Thus we can code.
We'll output "YES" or "NO".
Now, to ensure reading correctly: Input may contain blank lines? The sample has a blank line after the number? Actually the sample input:
5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten
There is no blank lines. So just read each test case with two lines.
Thus coding.
Now I'll produce final answer.
But there's a catch: The constraints say length of p and h up to 100. So we can even brute force.
So final solution is as described.
I'll provide a solution in Python.
Possible pitfalls: Using input() without strip; we can use .strip() but not necessary.
Implementation:
def solve():
import sys
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
p = next(it)
h = next(it)
if len(p) > len(h):
out.append("NO")
continue
p_count = [0]*26
for ch in p:
p_count[ord(ch)-97] += 1
n = len(p)
found = False
for i in range(len(h)-n+1):
temp = [0]*26
for j in range(n):
temp[ord(h[i+j])-97] += 1
if temp == p_count:
found = True
break
out.append("YES" if found else "NO")
sys.stdout.write("\n".join(out))
That's it.
Alternatively, we could use collections.Counter.
But let's produce final answer with explanation.
We should output the solution as function solve() and call it if name == "main".
So final response includes code and explanation.
Now produce answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
it = iter(data)
t = int(next(it))
out_lines = []
code
for _ in range(t):
p = next(it)
h = next(it)
# If the password is longer than the hash, impossible
if len(p) > len(h):
out_lines.append("NO")
continue
# Count characters in p
p_cnt = [0] * 26
for ch in p:
p_cnt[ord(ch) - ord('a')] += 1
n = len(p)
found = False
# Try every contiguous substring of length n
for start in range(len(h) - n + 1):
window_cnt = [0] * 26
for j in range(n):
window_cnt[ord(h[start + j]) - ord('a')] += 1
if window_cnt == p_cnt:
found = True
break
out_lines.append("YES" if found else "NO")
sys.stdout.write("\n".join(out_lines))