Last Updated: 24 Dec, 2020
##### Word Pattern
Easy
Problem statement

#### Here follow means a full match, i.e. there is a bijection between a letter of βTβ and a non-empty word of βSβ.

##### For Example:
``````If the given string is S = "lion cow cow lion" and T = βwccwβ, then the string βSβ follows the same pattern as string βTβ.
``````
##### Note:
``````'Tβ contains only lowercase English letters.

βSβ contains only lowercase English letters and spaces.

βSβ does not contain any trailing or leading spaces. All words in βSβ are separated by a single space.
``````
##### Input Format:
``````The first line contains an integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first line of each test case contains a string βSβ.

The second line of each test case contains a string βTβ.
``````
##### Output Format:
``````For each test case, the only line of output will print βYesβ if βSβ follows the same pattern as βTβ. Else print βNoβ.

Print the output of each test case in a separate line.
``````
##### Note:
``````You are not required to print the expected output, it has already been taken care of. Just implement the function.
``````
##### Constraints:
``````1 <= t <= 100
1 <= |S| <= 5000
1 <= |T| <= 5000

Time Limit: 1 second
``````
Approaches

## 01Approach

We will use two hashmaps βcharToWordβ and βwordToCharβ to track which character of βTβ maps to which word of βSβ and which word of βSβ maps to which character of βTβ,  respectively.

Here is the algorithm:

1. We initialise two hashmaps βcharToWordβ and βwordToCharβ.
2. We scan each character-word pair
1. If the character is not present in βcharToWordβ
1. If the word is already present in βwordToCharβ
1. Return βNoβ. Since it has been mapped with some other character before.
2. Else
1. Map the character of βTβ to the word of βSβ.
2. Map the word of βSβ to the character of βTβ.
2. Else
1. If the current word is not equal to the word that character maps to in βcharToWordβ
1. Return βNoβ.
3. Finally, return βYesβ.