Menu Close

Write a function to return true if s2 contains the permutation of s1.

Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string’s permutations is the substring of the second string.

Input: s1 = "ab" s2 = "eidbaooo"
Output:True
Explanation: s2 contains one permutation of s1 ("ba").

Input: s1= "ab" s2 = "eidboaoo"
Output: False
from itertools import permutations

s1=input('Enter string s1 : ')
s2=input('Enter string s2 : ')

Slist=list(s1)
permi=permutations(Slist)

temp=[]
for i in list(permi):
	temp.append(''.join(i))

flag=0
for i in temp:
	if i in s2:
		flag=1
		break

print(False) if flag==0 else print(True)

Input_1:
Enter string s1 : tweet
Enter string s2 : sweet

Output:
False


Input_2:
Enter string s1 : weed
Enter string s2 : dewe

Output:
True



Input_3:
Enter string s1 : fcuk
Enter string s2 : fuckthecode

Output:
True


Input_4:
Enter string s1 : ab
Enter string s2 : eidbaooo

Output:
True


Input_5:
Enter string s1 : ab
Enter string s2 : eidboaoo

Output:
False



Illustration of the Output

Executed using terminal(Ubuntu) Linux Python3

More Q