a-needle-in-the-haystack-1

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  

SAMPLE INPUT
3
hack
indiahacks
code
eddy
coder
iamredoc

SAMPLE OUTPUT
YES
NO
YES

Source: hackerearth | Difficulty: MEDIUM
Tags: Ad-hoc


Solutions:
n = int(input())
i=0
result = []
while i < n:
	pattern=input()
	text=input()
	reversetext =text[::-1]
	if pattern in text or pattern in reversetext:
		result.append('YES')
	else:
		result.append('NO')
	i=i+1
for te in result:
	print(te)
	


from itertools import permutations
n = int(input())

for x in range(n):
	pattern = input()
	string = input()
	reverse = pattern[::-1]
	
	if pattern in string or reverse in string:
		print("YES")
	else:
		print("NO")
	
	#print perms


t=int(input())
while t >0:
	t-=1
	pattern = input()
	txt_string = input()
	tmp = pattern[::-1]
	if pattern in txt_string or tmp in txt_string:
		print("YES")
	else:
		print("NO")


T=int(input())

for _ in range(0,T):
	pat=input()
	string=input()

	freqPattern=[0]*26
	freqString=[0]*26

	for char in pat:
		freqPattern[ord(char)-ord('a')]+=1

	for char in string[:len(pat)-1]:
		freqString[ord(char)-ord('a')]+=1

	for i in range(len(pat)-1,len(string)):

		freqString[ord(string[i])-ord('a')]+=1
		flag=1
		for j in range(0,26):
			if freqString[j]!=freqPattern[j]:
				flag=0
				break

		if(flag):
			break;
		freqString[ord(string[i-len(pat)+1])-ord('a')]-=1

	if(flag):
		print("YES")
	else:
		print("NO")


for T in range(int(input())):
	pat = str(input())
	txt = str(input())
	patr = pat[::-1]
	if pat in txt:
		print("YES")
	else:
		if patr in txt:
			print("YES")
		else:
			print("NO")
	
	


if __name__=='__main__':
	T = int(input())
	for i in range(0,T):
		string1 = input()
		check = input()
		string = string1[::-1]
		if string in check or string1 in check:
			print("YES")
		else:
			print("NO")


t=int(input())
while t >0:
	t-=1
	pattern = input()
	txt_string = input()
	tmp = pattern[::-1]
	if pattern in txt_string or tmp in txt_string:
		print("YES")
	else:
		print("NO")



t=int(input())
while t >0:
	t-=1
	pattern = input()
	txt_string = input()
	tmp = pattern[::-1]
	if pattern in txt_string or tmp in txt_string:
		print("YES")
	else:
		print("NO")


A = ord("a")
for _ in range(int(input())):
	p = 26 * [0]
	t = input().strip()
	lp = len(t)
	for i in t:
		p[ord(i)-A] += 1
	c = 26 * [0]
	t = list(map(ord, input().strip()))
	lt = len(t)
	t += [A]
	for i in t[:lp]:
		c[i-A] += 1
	ans = "NO"  
	for i in range(lt-lp+1):
		if c == p:
			ans = "YES"
			break
		c[t[i]-A] -= 1
		c[t[i+lp]-A] += 1
	print(ans)


n=int(input().strip(" "))
while(n>0):
	n-=1
	s=input()
	st=input()
	temp=s
	if s in st:
		print("YES")
		continue
	cnt=0
	flag=0
	for i in range(0,len(st)):
		if st[i] in s:
			cnt+=1
			s=s.replace(st[i],'',1)
		else:
			cnt=0
			s=temp
		#print cnt
		if cnt==len(temp):
			print("YES")
			flag=1
			break
	if(flag==0):
		print("NO")


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
t=int(input())
count = 0
while(count<t):
	pat = input()
	txt = input()
	if(pat in txt) or (pat[::-1] in txt):
		print("YES")
	else:
		print("NO")
	count = count + 1	


for testcases in range(int(input())):
	# problem solution
	answer = False
	pattern = input()
	text = input()
	pattern_length = len(pattern)
	text_length = len(text)
	if text_length < pattern_length:
		print('NO')
		continue
	pats = [0]*26
	txts = [0]*26
	for x in pattern:
		pats[ord(x) - 97] += 1
	for x in range(pattern_length):
		txts[ord(text[x]) - 97] += 1
	for x in range(pattern_length, text_length):
		if pats == txts:
			answer = True
			break
		txts[ord(text[x]) - 97] += 1
		txts[ord(text[x - pattern_length]) - 97] -= 1
	if pats == txts:
		answer = True
	if answer:
		print('YES')
	else:
		print('NO')


t = eval(input())
while t:
	p = input()
	ts = input()
	if p in ts or p[::-1] in ts:print('YES')
	else:print('NO')
	t -= 1


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''

class text:
	def __init__(self):
		self.user_text=""
		

	def exists(self,intext,utext):
		
		self.user_text=utext
		if intext in utext:
			return "YES"
		else:
			intext=intext[::-1]
			if intext in utext:
				return "YES"
			else:
				return "NO"			
		
_cases=eval(input())
mytext=text()
pat=[]
txt=[]
for i in range(_cases):
	pat.append(input().lower())
	txt.append(input().lower())
for i in range(_cases):
	#mytext=text()
	print(mytext.exists(pat[i],txt[i]))


b=[]               
a=[]
s=[]
t=""
x=eval(input())
if x>100 or x<1:
	print("Invalid Test Case")
for i in range(0,x):
	a.append(input())
	b.append(input())
	
	s.append(a[i][::-1])
	
	if s[i] in b[i]:
		print("YES")
		
	elif a[i] in b[i]:
		print("YES")
	else :
		print("NO")


def needle():
	str1 = input()
	str2 = input()

	if (str1 in str2[::-1]) or (str1 in str2) or (str1[::-1] in str2):
		print ("YES")
	else:
		print ("NO")

	return
def main():
	t = int(input())

	for i in range(t):
		needle()
if __name__ == '__main__':
	main()


import fileinput
import sys
from itertools import permutations

a=[]
for l in fileinput.input():
	a.append(l)

n = int(a[0][:-1])
for i in range(1,2*n,2):
	x=a[i][:-1]
	if(i==2*n-1):
		y=a[i+1]
	else:	
		y=a[i+1][:-1]
	c=0
	if(x in y):
		print("YES")
		c=1
	elif(c==0):
		''.join(sorted(x))
		''.join(sorted(y))
		if x in y:
			print("YES")
			c=1
			break
	if(c==0) :
		print("NO")