Word Pattern

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.
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
CodingNinjas
author
2y
Hashmap

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 ...read more

Help your peers!
Add answer anonymously...
MasterCard Full Stack Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter