Equivalent Strings
Time limit1sMemory limit1024 MB
Given pairs of commuting letters, decide whether string t can be reached from s by swapping adjacent commuting letters.
- Level
Medium6 of 10
- Topics
- String, Implementation
- Solved
- No attempts yet
Problem
Consider strings made of the first letters of the English alphabet. Some pairs of letters are called commuting. If two commuting letters stand next to each other in a string, they may be swapped.
You are given the commuting pairs and two strings and of equal length. Determine whether can be obtained from by performing any number of operations, where each operation swaps two adjacent commuting letters.
Input
The first line contains two integers and , the number of letters used and the number of commuting pairs (, ).
Each of the next lines contains two letters with no space between them, forming a commuting pair. Each pair appears at most once.
The next two lines contain the strings and . They have the same length () and consist of the first letters of the Latin alphabet.
Output
Print YES if can be obtained from using the described operations. Otherwise, print NO.