Substring Permutation
InterviewTime limit1sMemory limit512 MB
Given strings S and P, decide whether some permutation of P is a substring of some permutation of S.
- Level
Medium6 of 10
- Topics
- Hash map, Two pointers, Array
- Solved
- No attempts yet
Problem
Given two strings and , there are several ways to determine whether appears as a substring of . The simplest is to check directly whether equals any substring of . Since can have substrings of length , this approach takes time. A more sophisticated method uses the Knuth-Morris-Pratt (KMP) algorithm to solve the problem in .
This problem poses a similar challenge.
Given two strings and . Let be the set of all strings that are permutations of , and the set of all strings that are permutations of . Determine whether there exists a string and a string such that appears as a substring of .
For example, let and . Then , and . The string in appears as a substring of the string in , that is, . In this example you can also find other pairs that satisfy the requirement, such as , , , .
Input
The input consists of two lines. The first line contains a string (). The second line contains a string (). Both and consist only of lowercase English letters (a-z).
Output
If there exists a string and a string such that appears as a substring of , output “YES” on one line. Otherwise, output “NO”. Do not print the quotes.