Topics

Word Pattern

Easy
0/40
Average time to solve is 15m
Contributed by

Problem statement

You have been given two strings 'S' and ‘T’. Your task is to find if ‘S’ follows the same pattern as ‘T’.

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.
``````
Detailed explanation ( Input/output format, Notes, Images )
Constraints:
``````1 <= t <= 100
1 <= |S| <= 5000
1 <= |T| <= 5000

Time Limit: 1 second
``````
Sample Input 1:
``````1
ship ship ship ship
rrbb
``````
Sample Output 1:
``````No
``````
Explanation For Sample Output 1:
``````Here, string ‘S’ does not match the pattern with the string ‘T’ because different characters map the same word.
``````
Sample Input 2:
``````2
red blue blue red
ebbe
moon moon moon moon
aaaa
``````
Sample Output 2:
``````Yes
Yes
``````
Console